[Struts2应用开发] JSON的应用
生活随笔
收集整理的這篇文章主要介紹了
[Struts2应用开发] JSON的应用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在日常的WEB應(yīng)用開發(fā)過程中,前端常會涉及AJAX,而前臺與后臺的交互常用的數(shù)據(jù)格式就是JSON。
Struts2中使用JSON的方法方法如下:
Action:
如果action中的某些屬性不需要在json里面出現(xiàn),可以通過annotation :@JSON(serialize=false)來標(biāo)記改屬性不需返回至JSON。
注意,如果某屬性沒有g(shù)et方法,則該屬性亦不會在返回JSON串里面出現(xiàn)。
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;} @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 --><!-- ================== --><!-- 添加 --><action name="add*" class="{1}Action" method="add{1}"><result name="success" type="redirectAction">show{1}</result></action><!-- 修改跳轉(zhuǎn) --><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>
轉(zhuǎn)載于:https://www.cnblogs.com/enixyu/archive/2012/09/19/2693412.html
總結(jié)
以上是生活随笔為你收集整理的[Struts2应用开发] JSON的应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 港澳通行证 多少钱啊?
- 下一篇: 园林景观石多少钱一吨?