在日常的WEB应用开发过程中,前端常会涉及AJAX,而前台与后台的交互常用的数据格式就是JSON。
Struts2中使用JSON的方法方法如下:
Action:
如果action中的某些属性不需要在json里面出现,可以通过annotation :@JSON(serialize=false)来标记改属性不需返回至JSON。
注意,如果某属性没有get方法,则该属性亦不会在返回JSON串里面出现。
import java.util.List;import org.apache.struts2.json.annotations.JSON; import com.opensymphony.xwork2.Action;public class UsersAction extends BaseAction implements Action {private String userID;private String userName;private String userPwd;private String employeeName;private String userIDs;private UsersService usersService;private String queryString;private ListuserList;@JSON(serialize=false)public List getUserList() {return userList;}public void setUserList(List userList) {this.userList = userList;}public String getUserID() {return userID;}public void setUserID(String userID) {this.userID = userID;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserPwd() {return userPwd;}public void setUserPwd(String userPwd) {this.userPwd = userPwd;}public String getEmployeeName() {return employeeName;}public void setEmployeeName(String employeeName) {this.employeeName = employeeName;}public String getUserIDs() {return userIDs;}public void setUserIDs(String userIDs) {this.userIDs = userIDs;}@JSON(serialize=false)public UsersService getUsersService() {return usersService;}public void setUsersService(UsersService usersService) {this.usersService = usersService;}public String getQueryString() {return queryString;}public void setQueryString(String queryString) {this.queryString = queryString;}public String editUsers(){LS_PT_MST_USERS entity = usersService.get(Integer.parseInt(this.userID));this.userName = entity.getUSERNAME();this.userPwd = entity.getUSERPWD();this.employeeName = entity.getEMPLOYEENAME();return SUCCESS;} @Overridepublic String execute() throws Exception {// TODO Auto-generated method stubreturn null;}}
struts.xml
在配置文件中,package必须继承json-default。在需要返回json的action定义中,result type=json。
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="ptsystem" extends="json-default"><action name="add*" class="{1}Action" method="add{1}"><result name="success" type="redirectAction">show{1}result>action><action name="edit*" class="{1}Action" method="edit{1}"><result type="json">result>action><action name="update*" class="{1}Action" method="update{1}"><result name="success" type="chain">show{1}result>action><action name="delete*" class="{1}Action" method="delete{1}"><result name="success" type="chain">show{1}result>action><action name="show*" class="{1}Action" method="show{1}"><result name="success">/setting/{1}List.jspresult>action><action name="query*" class="{1}Action" method="query{1}"><result name="success">/setting/{1}List.jspresult>action>package><constant name="struts.multipart.parser" value="jakarta">constantconstant><constant name="struts.i18n.encoding" value="gbk" /> struts>