action有3种方式处理Ajax请求:
(1)跟servletAPI耦合的方式:借助response流获取out来完成;
(2)借助Stream结果类型,跟servlet API解耦合方式;
1-给result指定type=stream <result type="stream"> <param name="contentType">test/html;charset=utf-8</param> <param name="inputName">is</param> </result>
2-action中:is = new ByteArrayInputStream(str.getBytes("utf-8"));
(3)struts内置的json插件;
1-.导入:struts2-json-plugin-2.3.20.jar 2-后台:直接给action定义属性,然后给属性赋值即可 public String testjson(){ this.p = new Person(); p.setId(2); p.setName("zhangsan"); return SUCCESS; } 3-配置: 1-package----继承json-default包 2-action---result---type:json 1-result中不需要写相应的资源url
具体实例:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'testajax.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="jquery-1.9.1.js"></script> <script type="text/javascript"> $(function(){ $("#b1").click(function(){ $.post('testajax','',function(data){ alert(data); },'text'); }); $("#b2").click(function(){ $.post('testajax1','',function(data){ alert(data.name); },'json'); }); $("#b3").click(function(){ $.post('json/testajax3','',function(data){ alert(data.name); alert("p.id="+data.p.id+",p.name"+data.p.name); },'json'); }); }); </script> </head> <body> <button id="b1">耦合servletapi</button><br> <button id="b2">stream结果类型</button><br> <button id="b3">struts2内置json</button><br> </body> </html> Struts.xml配置: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myInter" class="com.interceptors.MyInter"/> <interceptor-stack name="abc"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="tokenSession"></interceptor-ref> <interceptor-ref name="myInter"></interceptor-ref> </interceptor-stack> <action name="testajax" class="com.handler.AjaxAction" method="testajax"> </action> <action name="testajax1" class="com.handler.AjaxAction" method="testajax1"> <result type="stream"> <param name="contentType">test/html;charset=utf-8</param> <param name="inputName">inputStream</param> </result> </action> </package> <package name="json" namespace="/json" extends="json-default"> <action name="testajax3" class="com.handler.AjaxAction" method="testjson"> <result type="json"></result> </action> </package> </struts> action域: package com.handler; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.bean.Person; import com.google.gson.Gson; import com.opensymphony.xwork2.ActionSupport; public class AjaxAction extends ActionSupport { public String testajax()throws Exception{ HttpServletResponse response = ServletActionContext.getResponse(); PrintWriter out = response.getWriter(); out.print("hello"); out.flush(); out.close(); return NONE; } private InputStream inputStream; public InputStream getInputStream() { return inputStream; } public String testajax1()throws Exception{ Person p = new Person(); p.setId(1); p.setName("李四"); Gson g = new Gson(); String str = g.toJson(p); inputStream = new ByteArrayInputStream(str.getBytes("utf-8")); return SUCCESS; } private Person p; public Person getP() { return p; } public void setP(Person p) { this.p = p; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String testjson(){ this.p = new Person(); p.setId(2); p.setName("zhangsan"); this.name = "abc"; return SUCCESS; } } 运行结果: