浅谈struts2和json的集成

该例子以非常简单的形式描述了struts2和json的集成开发。。和大家共同学习。。
我用到的是struts2.2.3的jar包。
除了导入struts2的jar包外,还引入struts2-json-plugin.jar


[color=red]jsonexample.java[/color]

package com.lysf.json;

import java.util.hashmap;
import java.util.map;

import org.apache.struts2.json.annotations.json;

import com.opensymphony.xwork2.action;

public class jsonexample {
private string field1 = "str";
private int[] ints = {10, 20};
private map map = new hashmap();
private string customname = "custom";

//'transient' fields are not serialized
private transient string field2;

//fields without getter method are not serialized
private string field3;

public string execute() {
map.put("john", "galt");
return action.success;
}

public string getfield1() {
return field1;
}

public void setfield1(string field1) {
this.field1 = field1;
}

public int[] getints() {
return ints;
}

public void setints(int[] ints) {
this.ints = ints;
}

public map getmap() {
return map;
}

public void setmap(map map) {
this.map = map;
}

@json(name="newname")
public string getcustomname() {
return this.customname;
}
}

[color=red]struts.xml[/color]

<package name="example" extends="json-default">
<action name="jsonexample" class="com.lysf.json.jsonexample">
<result type="json"/>
</action>
</package>

[color=red]web.xml[/color]

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


最后得到的结果为:

{"newname":"custom","field1":"str","ints":[10,20],"map":{"john":"galt"}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值