日期:2014-05-16  浏览次数:20460 次

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

一、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+"\"}");//反斜杠转义
		}
	}

?

?

1 楼 lewkinglove 2012-03-31  
话说, 你的this.getResponse().getWriter()这个方法是自己实现的还是继承父类的? 父类的名字是?