本文主要探討普通數(shù)據(jù)如何快速轉(zhuǎn)換為Json數(shù)據(jù),一共討論2種方法:
首相準備頁面和實體類:
頁面:
<body> <div id="topLoginDiv"> 用戶名: <input name="user.name" id="loginName" /> 密碼: <input name="user.password" id="loginPassword" /> <label class="ui-green"> <input type="button" name="loginButton" value="登錄" onclick="doLogin();" /> </label> </div> <div id="demo" ></div></body>
實體類:
package bean;public class User {private int id;private String userName;private String password;......省略Get和Set方法}方法一:使用JSON轉(zhuǎn)換包進行JSON數(shù)據(jù)的轉(zhuǎn)換
第一步,引入相關(guān)相關(guān)包
  
第二步:頁面提交及回調(diào)函數(shù)處理結(jié)果。
<script type="text/javascript">function doLogin(){var name = $('#loginName').val();var password = $('#loginPassword').val();var data1 ={'user.userName':name,'user.password':password};$.getJSON('user_login.action',data1,function(data){//此處須用$.getJSON來處理JSON數(shù)據(jù)if(data.flag){$('#topLoginDiv').html("");$('#demo').html("當前用戶:"+data.user.userName+"   "+data.msg);}else{$('#demo').html(data.msg);}});}</script>第三步:Struts2跳轉(zhuǎn)到Action中進行JSON的轉(zhuǎn)換《關(guān)鍵步驟》
private User user=new User();private boolean flag;private String msg;......省略Get和Set方法public String login() throws IOException{if(user.getUserName().equals("admin")&&user.getPassword().equals("123456")){msg="登陸成功";flag=true;}else{msg="登錄失敗,用戶名或密碼錯誤!";flag=false;}Map<String,Object> list = new HashMap<String,Object>();//此處的Map不用get和Set方法list.put("flag", flag);list.put("msg",msg);if(flag){list.put("user",user);}ServletActionContext.getResponse().setCharacterEncoding("UTF-8");ServletActionContext.getResponse().getWriter().print(JSONObject.fromObject(list));return null;//此處返回值為NULL,不需要再回到ACTION配置中進行處理}方法二:使用Struts2配置Action進行JSON數(shù)據(jù)的轉(zhuǎn)換
第一步:引入包
此種方法只需要在使用Struts2所需包的基礎(chǔ)上引入下面這一個包即可:
第二步:頁面提交及回調(diào)函數(shù)處理結(jié)果。參考方法一中的第二步。
第三步:配置Action
<package name="json_default" namespace="/" extends="json-default">//注意此處的extends配置    <action name="user_*" class="Action.userAction" method="{1}">      <result type="json">//此處指明類型        <!-- 參數(shù)root指定要序列化得根對象 -->        <!-- 默認將序列化當前Action中所有有返回值的getter方法的值 -->        <param name="root">list</param>        <!-- 參數(shù)includeProperties指定要序列化根對象中的哪些屬性,多個屬性以逗號隔開-->        <param name="includeProperties">msg,flag,user,user.userName</param>        <!-- 參數(shù)excludeProperties指定要從根對象中排除的屬性,排除屬性將不被序列化-->        <param name="excludeProperties">user.password</param>        <!-- 參數(shù)excludeNullProperties指定是否序列化值為空的屬性-->        <param name="excludeNullProperties">true</param>      </result>    </action></package>第四步:Struts2跳轉(zhuǎn)到Action中進行JSON的轉(zhuǎn)換《關(guān)鍵步驟》
private User user=new User();private boolean flag;private String msg;private Map<String,Object> list=null;//需要為MAP準備get和Set方法..................省略Get和Set方法public String login() throws IOException{if(user.getUserName().equals("admin")&&user.getPassword().equals("123456")){msg="登陸成功";flag=true;}else{msg="登錄失敗,用戶名或密碼錯誤!";flag=false;}list= new HashMap<String,Object>();list.put("flag", flag);list.put("msg",msg);if(flag){list.put("user",user);}return "success";//返回值為success確保能跳進Action配置文件進行數(shù)據(jù)轉(zhuǎn)換以上這篇Json在Struts中的轉(zhuǎn)換與傳遞方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答