配置中的参数含义:
root参数用于指定要序列化的根对象,如果省去这一配置,表示要序列化action中的所有属性
ignoreHierarchy 为false时表示要序列化根对象的所有基类
excludeProperties表示排除的序列化的属性
includeProperties表示哪些属性被序列化
Action配置:
<!-- jsonplugin的使用配置 -->
<!-- package要继承json-default 以加载json插件 -->
<action name="jsonAct" class="cn.enjoylife.prac.action.JsonAction">
<result type="json">
<!-- root参数用于指定要序列化的根对象,如果省去这一配置,表示要序列化action中的所有属性 -->
<param name="root">map</param>
<!-- ignoreHierarchy 为false时表示要序列化根对象的所有基类 -->
<param name="ignoreHierarchy">false</param>
<!-- 排除hello属性,使其不被序列化 -->
<param name="excludeProperties">hello</param>
</result>
</action>
JsonAction.java:
public class JsonAction extends ActionSupport {
private static final long serialVersionUID = 3870310687165227565L;
private int[] ints = { 100, 200 };
private Map<String, Object> map = new HashMap<String, Object>();
private String hello = "helloWorld";
private String username = "evan";//没有getter方法,不会被序列化
private String password = "pwd";
private String validateCode = "123456";
public int[] getInts() {
return ints;
}
public void setInts(int[] ints) {
this.ints = ints;
}
@JSON(name="newMap")//重新命名
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
public void setUsername(String username) {
this.username = username;
}
@JSON(serialize=true)//这是默认值,可以省去
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@JSON(serialize=false)//serialize参数为false,不会被序列化
public String getValidateCode() {
return validateCode;
}
public void setValidateCode(String validateCode) {
this.validateCode = validateCode;
}
@Override
public String execute() throws Exception {
map.put("info", "今天的雨真大啊!");
return SUCCESS;
}
}
使用jquery操作返回的json串:
$(document).ready(function(){
$.getJSON(
"./jsonAct.action",
function(data){
alert(data.info);
}
);
});
Annotation:
- package com.zheshou.action;
- import javax.annotation.Resource;
- import org.apache.struts2.convention.annotation.Action;
- import org.apache.struts2.convention.annotation.Namespace;
- import org.apache.struts2.convention.annotation.ParentPackage;
- import org.apache.struts2.convention.annotation.Result;
- import com.opensymphony.xwork2.ActionSupport;
- import com.zheshou.model.Dept;
- import com.zheshou.model.User;
- import com.zheshou.service.DeptService;
- import com.alibaba.fastjson.JSON;
- @ParentPackage(value="json-default")
- @Namespace(value="/")
- public class SelectDeptAction extends ActionSupport {//2.ActionSupport必须有这个才行.但是可以不用Execute了
- private DeptService deptService;
- private String beginTime;
- private String endTime;
- private String Emps;
- private String result;
- public DeptService getDeptService() {
- return deptService;
- }
- @Resource(name="deptService")
- public void setDeptService(DeptService deptService) {
- this.deptService = deptService;
- }
- public String getBeginTime() {
- return beginTime;
- }
- public void setBeginTime(String beginTime) {
- this.beginTime = beginTime;
- }
- public String getEndTime() {
- return endTime;
- }
- public void setEndTime(String endTime) {
- this.endTime = endTime;
- }
- public String getEmps() {
- return Emps;
- }
- public void setEmps(String emps) {
- Emps = emps;
- }
- public String getResult() {
- return result;
- }
- public void setResult(String result) {
- this.result = result;
- }
- @Action(value="selectdept"
- ,results={@Result(
- type="json"
- ,params={"contentType","text/html","root","result"}//root<会带外引号>
- //includeProperties Result不带字符引号
- //excludeProperties 返回所有的属性
- )}
- )
- public String execute(){
- //result= JSON.toJSONString(user).replaceAll("\"", "'");
- result=deptService.Select(Emps);
- System.out.print(result);
- return SUCCESS;
- }
- }