最近在做struts+ajax+json项目,项目中使用了struts2-json-plugin-2.2.3.jar(一款json转化插件),研究了一段时间,今天来总结一下具体的使用过程。
使用struts2-json-plugin-2.2.3.jar需要理解以下几点:1、struts2-json-plugin-2.2.3.jar就是一个将对象属性转化成json的东东,2、要转化的对象中必须要有get打头的函数才可以(所以,如果使用struts2-json-plugin-2.2.3.jar,尽量避免在要转化的对象中将不想转化json的的函数使用get*打头),不多说了,下面看配置:
1、我的程序目录结构,由于这是一个struts插件,所以必须要有struts才可以。
2、我的java文件:
StudentEntity.java
[java] view plaincopy
- import java.io.Serializable;
- public class StudentEntity implements Serializable {
- private String stuName;
- private String stuAge;
- private String stuSex;
- public String getStuAge() {
- return stuAge;
- }
- public void setStuAge(String stuAge) {
- this.stuAge = stuAge;
- }
- public String getStuName() {
- return stuName;
- }
- public void setStuName(String stuName) {
- this.stuName = stuName;
- }
- public String getStuSex() {
- return stuSex;
- }
- public void setStuSex(String stuSex) {
- this.stuSex = stuSex;
- }
- }
TeacherEntity.java
[java] view plaincopy
- import java.io.Serializable;
- public class TeacherEntity implements Serializable {
- private String teacName;
- private String teacAge;
- private String teacSex;
- public String getTeacAge() {
- return teacAge;
- }
- public void setTeacAge(String teacAge) {
- this.teacAge = teacAge;
- }
- public String getTeacName() {
- return teacName;
- }
- public void setTeacName(String teacName) {
- this.teacName = teacName;
- }
- public String getTeacSex() {
- return teacSex;
- }
- public void setTeacSex(String teacSex) {
- this.teacSex = teacSex;
- }
- }
StrIndex.java(这是一个action,命名不规范)
[java] view plaincopy
- import java.util.Date;
- import org.apache.struts2.json.annotations.JSON;
- import com.opensymphony.xwork2.ActionSupport;
- public class StrIndex extends ActionSupport {
- private TeacherEntity teacher=new TeacherEntity();
- private StudentEntity student=new StudentEntity();
- private Date nowd=new Date();
- public String toIndex(){
- teacher.setTeacName("张三");
- teacher.setTeacAge("100");
- teacher.setTeacSex("男男");
- student.setStuName("李老师");
- return SUCCESS;
- }
- // 是否转换该对象
- // @JSON(serialize=true)
- // @JSON(name="newName")
- public StudentEntity getStudent() {
- return student;
- }
- public void setStudent(StudentEntity student) {
- this.student = student;
- }
- public TeacherEntity getTeacher() {
- return teacher;
- }
- public void setTeacher(TeacherEntity teacher) {
- this.teacher = teacher;
- }
- @JSON(format="yyyy-MM-dd")
- public Date getNowd() {
- return nowd;
- }
- public void setNowd(Date nowd) {
- this.nowd = nowd;
- }
- }
紧接着是我的struts配置文件:
[html] view plaincopy
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="test-default" extends="json-default">
- <action name="ind" class="StrIndex" method="toIndex">
- <result name="success" type="json">
- </result>
- </action>
- </package>
- </struts>
3、简单配置说明
在struts中使用struts2-json-plugin-2.2.3.jar需要将要返回json的action配置在
[html] view plaincopy
- <package name="test-default" extends="json-default">
- ...
- ...
- </package>
中,注意extends="json-default"继承的是“json-default" 我们正常配置struts是使用‘struts-default’,所以需要将返回json的action和普通action分开使用两个package进行配置
[html] view plaincopy
- <package name="test-default" extends="json-default">
- ...
- ...
- </package>
- <package name="struts-default" extends="struts-default">
- ...
- ...
- </package>
在json package中的acton配置
[html] view plaincopy
- <result name="success" type="json"></result>
便可以成功的转化json,结果如
[plain] view plaincopy
- {"nowd":"2012-12-27T22:23:13","student":
{"stuAge":null,"stuName":"李老师","stuSex":null},"teacher":
{"teacAge":"100","teacName":"张三","teacSex":"男男"}}
4、拦截器配置
因为json插件的原理是将action中所有以get打头的函数全部转化,包括bena中的bean(如本案例所示,会将action中
student中的name以及age等全部转化),如果bean太多这会造成巨大浪费,使用拦截器可以根据需要将bean转化成json。
4.1、配置什么属性转化什么属性,如下配置
[html] view plaincopy
- <result name="success" type="json">
- <param name="includeProperties">
- teacher\.teacName,teacher\.teacAge
- </param>
- </result>
在result中配置param标签,同时为param标签指定includeProperties属性,通过以上配置可以让json插件只转化param配置的属性。结果如
[plain] view plaincopy
- {"teacher":{"teacAge":"100","teacName":"张三"}}
4.2、只返回指定属性的值,如下配置
[html] view plaincopy
- <result name="success" type="json">
- <param name="root">
- teacher.teacName
- </param>
- </result>
在result中配置param标签,同时为param标签指定root属性,通过以上配置可以让json插件只转化param配置的属性,如
[plain] view plaincopy
- "张三"
4.3、不想转化某些对象、属性
[html] view plaincopy
- <result name="success" type="json">
- <param name="excludeProperties">
- teacher\.teacName
- </param>
- </result>
在result中配置param标签,同时为param标签指定excludeProperties属性,通过以上配置可以让json插件不转化param配置的属性,如
[plain] view plaincopy
- {"nowd":"2012-12-27T22:34:00","student":
{"stuAge":null,"stuName":"李老师","stuSex":null},"teacher":
{"teacAge":"100","teacSex":"男男"}}
5、json注解使用
我目前所知道的json插件提供四种注解
serialize:设置是否序列化该属性
[java] view plaincopy
- @JSON(serialize=true)
- public StudentEntity getStudent() {
- return student;
- }
deserialize:设置是否反序列化该属性。
[java] view plaincopy
- @JSON(deserialize=true)
- public StudentEntity getStudent() {
- return student;
- }
format:设置用于格式化输出、解析日期表单域的格式。例如"yyyy-MM-dd‘T‘HH:mm:ss"。
[java] view plaincopy
- @JSON(format="yyyy-MM-dd")
- public Date getNowd() {
- return nowd;
- }
name:可改变序列后的属性明
[java] view plaincopy
- @JSON(name="newName")
- public StudentEntity getStudent() {
- return student;
- }
原文地址:http://blog.csdn.net/sys8090/article/details/8445430