六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 139|回复: 0

struts2的Action方法中,直接输出js代码

[复制链接]

升级  6.67%

58

主题

58

主题

58

主题

举人

Rank: 3Rank: 3

积分
220
 楼主| 发表于 2013-2-7 20:20:39 | 显示全部楼层 |阅读模式
一、Action直接输入js代码
struts2的Action方法,可以没有返回值,void,然后直接out.write("<script>js代码</script>")
out.println("<script>js代码</script>")
 
/** * 单个修改价格 * @return * @author mengxianjun * @throws Exception  * @date 2011-4-25 上午09:30:53 */public void updatePrice() throws Exception{TCommissionId tCommissionId = new TCommissionId();tCommissionId.setProductNum(productNum);tCommissionId.setTypeNum(typeNum);commission = priceStrategyService.queryTCommissionById(tCommissionId);/*按价格修改,else按比率修改*/if( "prices".equals(updateType) ){fastPriceOrRatio = DoubleConvert.getDoubleByFastPrice(fastPriceOrRatio);Double fastCommission = DoubleConvert.getDoubleByFastCommission((money[0])-fastPriceOrRatio);commission.setFastCommission(fastCommission);commission.setFastPrice(fastPriceOrRatio);commission.setAlterDate(new Date());//修改时间commission.setEmployeeId(getEmpID());//处理工号priceStrategyService.updateTCommission(commission);}else{Double fastPrice = DoubleConvert.getDoubleByFastPrice((money[0])*fastPriceOrRatio);//通过比率算出实际价格Double fastCommission = DoubleConvert.getDoubleByFastCommission((money[0])-fastPrice);//面值-实际价格,得到佣金commission.setFastCommission(fastCommission);commission.setFastPrice(fastPrice);commission.setAlterDate(new Date());//修改时间commission.setEmployeeId(getEmpID());//处理工号priceStrategyService.updateTCommission(commission);}this.getResponse().setContentType("text/html;charset=UTF-8");this.getResponse().setCharacterEncoding("UTF-8");PrintWriter out = this.getResponse().getWriter();out.println("<script>alert('修改成功!');history.go(-2);window.location.reload(true);</script>");out.flush();out.close();return null;} 
二、AJAX提交,Struts2的Action返回json数据
/** * 验证业务密码--ajax方式 * @author mengxianjun * @throws IOException  * @date 2011-6-13 下午01:33:03 */public void checkPwd2() throws IOException{PrintWriter out = this.getResponse().getWriter();String vp = "false";if( valiPass(password2) ){vp = "true";out.write("{\"msg\":\""+vp+"\"}");//反斜杠转义}else{vp = "false";out.write("{\"msg\":\""+vp+"\"}");//反斜杠转义}} 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表