Struts2整合json

struts2整合json,需要一些jar包

如图所示:

       

这里介绍3中方法整合json

方法一

在视图资源中输出JSON数据

Action处理完用户请求后,将数据存放在某一位置,如request中,并返回视图,然后Struts将跳转至该视图资源,在该视图中,我们需要做的是将数据从存放位置中取出,然后将其转换为JSON字符串,输出在视图中。

 1     /**
 2      * 测试通过action以视图方式返回JSON数据
 3      */
 4     public String testByJSP() {
 5         User user = new User();
 6         user.setId("1");
 7         user.setName("JSONActionJSP中文会乱码吗?");
 8         user.setPassword("123");
 9         JSONObject jsonObject = new JSONObject();
10         jsonObject.accumulate("user", user);
11         jsonObject.accumulate("success", true);
12         // 这里在request对象中放了一个data,所以struts的result配置中不能有type="redirect"        ServletActionContext.getRequest().setAttribute("data",jsonObject.toString());
13         return SUCCESS;
14     };
View Code

struts.xml的配置

1 <package name="default" extends="struts-default" namespace="/">        
2         <action name="testByJSP"
3                 class="com.scitc.test.web.UserAction" 
4                 method="testByJSP">
5             <result name="success">/actionJSP.jsp</result>
6         </action>
7     </package>
View Code

方法二

在Action中以Struts2的方式输出JSON数据

 1 // 将会被Struts2序列化为JSON字符串的对象
 2     private Map<String, Object> dataMap;
 3     public UserAction() {
 4         dataMap = new HashMap<String, Object>();
 5     }
 6     /**
 7      * Struts2序列化指定属性时,必须有该属性的getter方法,实际上,如果没有属性,而只有getter方法也是可以的
 8      */
 9     public Map<String, Object> getDataMap() {
10         return dataMap;
11     }    
12     /**
13      * 测试通过action以Struts2默认方式返回JSON数据
14      */
15     public String testByAction() {
16         // dataMap中的数据将会被Struts2转换成JSON字符串,所以这里要先清空其中的数据
17         dataMap.clear();
18         User user = new User();
19         user.setId("2");
20         user.setName("JSONActionStruts2中文会乱码吗?");
21         user.setPassword("123");
22         // 放入一个是否操作成功的标识
23         dataMap.put("success", true);
24         dataMap.put("user", user);
25         return SUCCESS;
26     }    
View Code

Struts.xml配置

 1  <package name="json" extends="json-default" namespace="/">
 2         <action name="testByAction"
 3                 class="com.scitc.test.web.UserAction" 
 4                 method="testByAction">
 5             <result type="json">
 6                 <param name="root">dataMap</param>
 7                 <!--注意不加下面的配置,ie会提示下载请求的连接资源-->
 8                 <param name="contentType">text/html</param>
 9             </result>
10         </action>
11     </package>
View Code

方法三

在Action中以传统方式输出JSON数据

这一点跟传统的Servlet的处理方式基本上一模一样,代码如下

 1 /**
 2      * 通过action是以传统方式返回JSON数据
 3      */
 4     public void doAction() throws IOException {
 5         HttpServletResponse resp = ServletActionContext.getResponse();
 6         resp.setContentType("text/html");
 7         OutputStream out = resp.getOutputStream();
 8         // 将要被返回到客户端的对象
 9         User user = new User();
10         user.setId("3");
11         user.setName("JSONActionGeneral中文会乱码吗?");
12         user.setPassword("123");
13         JSONObject json = new JSONObject();
14         json.accumulate("user", user);
15         json.accumulate("success", true);
16         out.write(json.toString().getBytes());
17         out.close();
18     }
View Code

Struts.xml配置

1     <package name="default" extends="struts-default" namespace="/">
2         <action name="testJSONFromActionByGeneral"
3                 class="com.scitc.test.web.UserAction" 
4                 method="doAction">
5                 <!-- 由于是传统请求,所以没有与之对应的<result/>结果页面 ,即只有<action>请求 -->
6         </action>
7     </package>
View Code

这三个方法中,只有方法二<package>要继承"json-default"

三个方法的效果分别如图:

原文地址:https://www.cnblogs.com/ylfeiu/p/3969035.html