你好,游客 登录 注册 搜索
背景:
阅读新闻

Struts2返回JSON对象的方法总结

[日期:2012-09-16] 来源:Linux社区  作者:kingxss [字体:大 中 小]

三. Struts2零配置使用方法,使用Maven构件:

3.1) 建立一个webapp,这里还是采用Maven构建,构建过程参考: 使用Eclipse构建Maven的SpringMVC项目 http://www.linuxidc.com/Linux/2012-09/70638.htm。

3.2) 添加Struts2的依赖、struts2零配置依赖和struts2的json依赖:

  1. <dependencies>  
  2.     <!-- struts2核心依赖 -->  
  3.     <dependency>  
  4.         <groupId>org.apache.struts</groupId>  
  5.         <artifactId>struts2-core</artifactId>  
  6.         <version>2.3.4</version>  
  7.         <type>jar</type>  
  8.         <scope>compile</scope>  
  9.     </dependency>  
  10.     <!-- struts2零配置依赖 -->  
  11.     <dependency>  
  12.         <groupId>org.apache.struts</groupId>  
  13.         <artifactId>struts2-convention-plugin</artifactId>  
  14.         <version>2.3.4</version>  
  15.         <type>jar</type>  
  16.         <scope>compile</scope>  
  17.     </dependency>  
  18.     <!-- struts2的json依赖 -->  
  19.     <dependency>  
  20.         <groupId>org.apache.struts</groupId>  
  21.         <artifactId>struts2-json-plugin</artifactId>  
  22.         <version>2.3.4</version>  
  23.         <type>jar</type>  
  24.         <scope>compile</scope>  
  25.     </dependency>  
  26. </dependencies>  

经过测试,上面的依赖包间没有出现版本兼容的bug,不仅仅因为他们是同一个版本,更加得益于Maven的自动构建方式。

3.3) 配置web.xml,启用Struts2:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.        
  7.     <filter>    
  8.         <filter-name>StrutsPrepareAndExecuteFilter </filter-name>    
  9.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class>  
  10.         <init-param>  
  11.             <param-name>config</param-name>  
  12.             <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>  
  13.         </init-param>    
  14.     </filter>  
  15.     <filter-mapping>  
  16.         <filter-name>StrutsPrepareAndExecuteFilter</filter-name>  
  17.         <url-pattern>/*</url-pattern>  
  18.     </filter-mapping>  
  19. </web-app>  

3.4)配置struts.xml,设置一些基本常量和应用:

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC   
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <package name="base" extends="json-default,struts-default">  
  8.         <!-- 这里可以设置一些全局的返回值映射关系等 -->  
  9.     </package>  
  10.        
  11.     <constant name="struts.action.extension" value="" />  
  12.     <constant name="struts.ui.theme" value="simple" />  
  13.     <constant name="struts.i18n.encoding" value="utf-8" />  
  14.     <constant name="struts.multipart.maxSize" value="1073741824"/>  
  15.     <constant name="struts.devMode" value="false"/>  
  16. </struts>  

3.5)编写和配置Action。由并未指定Convention进行设置,所以对于Convention插件而言,默认的它会把所有类名以Action结尾的java类当成Action处理:

  1. package watson.action;   
  2.   
  3. import java.util.HashMap;   
  4. import java.util.Map;   
  5.   
  6. import org.apache.struts2.convention.annotation.Action;   
  7. import org.apache.struts2.convention.annotation.Namespace;   
  8. import org.apache.struts2.convention.annotation.ParentPackage;   
  9. import org.apache.struts2.convention.annotation.Result;   
  10. import org.apache.struts2.convention.annotation.Results;   
  11.   
  12. @ParentPackage("base")   
  13. @Namespace("/watson")   
  14. @Results({   
  15.     @Result(name = "json",type="json", params={"root","msg"})   
  16. })   
  17. public class JsonAction {   
  18.        
  19.     @Action(value="json")   
  20.     public String json() {   
  21.         msg = new HashMap<String, Object>();   
  22.         msg.put("flag", "success");   
  23.            
  24.         Map<String, String> user = new HashMap<String, String>();   
  25.         user.put("name", "张三");   
  26.         user.put("age", "34");   
  27.         msg.put("user", user);   
  28.         return "json";   
  29.     }   
  30.        
  31.     //==================================   
  32.     private Map<String, Object> msg;   
  33.   
  34.     public Map<String, Object> getMsg() {   
  35.         return msg;   
  36.     }   
  37.   
  38. }  

3.6)部署项目,启动容器,浏览器地址栏中输入:http://localhost:7070/Struts2foo/watson/json。等到结果如下:

{"flag":"success","user":{"age":"34","name":"张三"}}  

从上面结果可知在启用了零配置以后,只是少了在xml中的配置,改为在每个action中用annotation进行注解。这里删除上面在xml中的配置,将下面的代码写入到上面的JsonAction的上部:

  1. @ParentPackage("base")   
  2. @Namespace("/watson")   
  3. @Results({   
  4.     @Result(name = "json",type="json", params={"root","msg"})   
  5. })  

root就相当xml配置中的参数配置。

linux
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/6/3 18:24:47
感谢啊!确实必须设定编码,再获取PrintWriter