1.maven依賴
<!-- json與struts2配置文件相關的依賴 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
<exclusions>
<exclusion>
<artifactId>commons-lang</artifactId>
<groupId>commons-lang</groupId>
</exclusion>
</exclusions>
</dependency>
2.struts2配置文件
<package name="default" namespace="/" extends="json-default,struts-default">
<action name="*_*" method="{2}" class="com.wzxy.nc.controller.{1}Controller">
<result name="json" type="json">
<param name="root">dataMap</param>
</result>
</action>
</package>
3.UserController
protected final String JSON = "json";
protected Map<String,Object> dataMap;
public Map<String, Object> getDataMap() {
return dataMap;
}
public void setDataMap(Map<String, Object> dataMap) {
this.dataMap = dataMap;
}
public String insert(){
dataMap = new HashMap<String,Object>();
dataMap.put("code",0);
dataMap.put("msg","操做成功");
return JSON;
}