[Struts2应用开发] JSON的应用
在日常的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 List<LS_PT_MST_USERS> userList; @JSON(serialize=false) public List<LS_PT_MST_USERS> getUserList() { return userList; } public void setUserList(List<LS_PT_MST_USERS> 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; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return 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 --> <!-- ================== --> <!-- 添加 --> <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.jsp</result> </action> <!-- 搜索 --> <action name="query*" class="{1}Action" method="query{1}"> <result name="success">/setting/{1}List.jsp</result> </action> </package> <!-- 上传附件定义 --> <constant name="struts.multipart.parser" value="jakarta">constant</constant> <constant name="struts.i18n.encoding" value="gbk" /> </struts>