当前位置: 代码迷 >> Ajax >> Action中直接回到Ajax请求值的方法
  详细解决方案

Action中直接回到Ajax请求值的方法

热度:253   发布时间:2012-09-22 21:54:54.0
Action中直接返回Ajax请求值的方法

在实际的项目中,可能存在这样的情况:我们要通过Ajax访问Action中的某个方法,然后返回一个特定的数值给Ajax,而不是将方法处理结果对应的页面返回给Ajax;这是我们可以用以下的办法来处理:

?

1、Action 中的方法返回类型为:void,如:

public void getProgressRate() throws UnknownException,IOException 

?

2、struts.xlm配置文件中也不需要配置result,如:

<action name="getProgressRate" class="impToolProductAction" method="getProgressRate">
</action>

?

3、在Action中将处理结果返回Ajax的代码如下:

HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.getWriter().write(progressRate);// progressRate 是变量

?

4、通过这种方法在Ajax中就可以获取 progressRate 值了。

  相关解决方案