Struts2中使用AJAX

一、使用HttpServletResponse用流的形式输出

在Action中直接使用HttpServletResponse的getWriter()方法使用PrintWriter输出;

public class AjaxPrizeKindsAction extends ActionSupport {
	private Logger logger = Logger.getLogger(AjaxPrizeKindsAction.class);
	private IPrizeKindInfoService prizeKindInfoService;

	@Override
	public String execute() throws Exception {
		logger.debug("enter into AjaxPrizeKindsAction");
		List<PrizeKindInfoVO> list = prizeKindInfoService.findAllByCondition();
		
		if(list != null && list.size() > 0 ){
			Object[] temp = list.toArray();
			JSONArray jsonArray = JSONArray.fromObject(temp);
			
			String json = jsonArray.toString();
			WebUtil.printOut(json, "text/html", "UTF-8");
		}else{
			String json = null;
			WebUtil.printOut(json, "text/html", "UTF-8");
		}
		return null;
	}
	public void setPrizeKindInfoService(IPrizeKindInfoService prizeKindInfoService) {
		this.prizeKindInfoService = prizeKindInfoService;
	}
}

WebUtil工具类:

public class WebUtil {
   /**
	 * 页面输出
	 * @param res         输入内容
	 * @param contentType 内容格式,可空
	 * @param encoding    编码格式,可空
	 * @throws Exception 
	 */
	public static void printOut(String res, String contentType, String encoding) throws IOException{ 
		PrintWriter out = null;
		if(contentType == null || contentType.trim().equals("")){
			contentType = "text/html";
		}
		if(encoding == null || encoding.trim().equals("")) {
			encoding = "UTF-8";
		}
		logger.debug("contentType: " +contentType + " | encoding: " + encoding);
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType(contentType);
			response.setCharacterEncoding(encoding);
			response.getWriter();
			out = response.getWriter();
			out.println(res);
		}catch (IOException e) {
			logger.error("IOException in printOut of BaseAction:", e);
			throw e;
		}
		finally {
			if(out != null) {
				out.flush();
				out.close();
			}
		}
	}
}

在JSP中的AJAX请求:

$.post(url, params, function (result,textStatus){
			if(textStatus == 'success'){
				if(result != null){
				//处理result 
			}
}

struts.xml中的配置:

<!-- ajax -->
    <package name="ajax" extends="struts-default" namespace="/ajax">
	    <action name="" class="com.test.controller.AjaxPrizeKindsAction"></action>
    </package>

这样的做法是借助工具类自定义返回流的形式,比较复杂,并且跟struts2没有多大关系,struts2支持一种

stream类型的Ruselt,这种类型可以直接向客户端浏览器生成二进制响应,文本响应。

二、使用stram类型的Result实现AJAX

public class AjaxPrizeKindsAction extends ActionSupport {
	private Logger logger = Logger.getLogger(AjaxPrizeKindsAction.class);
    private InputStream inputStream;    

	@Override
	public String execute() throws Exception {
		logger.debug("enter into AjaxPrizeKindsAction");
		    String a = "{\"a\":\"c\",\"sd\":\"ds\"}";
			inputStream = new ByteArrayInputStream(a.toString().getBytes());
		    return SUCCESS;
	}

    public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
   
}

xml中的配置

<action name="ajaxprizekinds" class="com.shcredit.controller.prize.prizeactivity.prizeactdet.AjaxPrizeKindsAction">
			<result name="success" type="stream">
				<!-- 指定下载文件的文件类型 -->
				<param name="contentType">text/html</param>
				<param name="inputName">inputStream</param>
			</result>
</action>

 

转载于:https://my.oschina.net/u/3697923/blog/1611926

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值