Json在Struts中的轉換與傳遞

來源:互聯網
上載者:User

標籤:c   style   class   blog   java   a   

  本文主要探討普通資料如何快速轉換為Json資料,一共討論2種方法:

  首相準備頁面和實體類:

  頁面:     

  <body>
    <div id="topLoginDiv">
      使用者名稱:
      <input name="user.name" id="loginName" />
      &nbsp;密碼:
      <input name="user.password" id="loginPassword" />


      <label class="ui-green">
      <input type="button" name="loginButton" value="登入" onclick="doLogin();" />
      </label>
    </div>
    <div id="demo" style="display: inline"></div>
  </body>

 

  實體類:

  

  package bean;

  public class User {

  private int id;
  private String userName;
  private String password;
  ......省略Get和Set方法

  }

  方法一:使用JSON轉換包進行JSON資料的轉換

  第一步,引入相關相關包

  

  第二步:頁面提交及回呼函數處理結果。

  

  <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資料
  if(data.flag){
  $(‘#topLoginDiv‘).html("");
  $(‘#demo‘).html("目前使用者:"+data.user.userName+"     "+data.msg);
  }else{
  $(‘#demo‘).html(data.msg);
  }
  });

  }
  </script>

  第三步:Struts2跳轉到Action中進行JSON的轉換《關鍵步驟》

  

  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資料的轉換

  第一步:引入包

  此種方法只需要在使用Struts2所需包的基礎上引入下面這一個包即可:

  第二步:頁面提交及回呼函數處理結果。參考方法一中的第二步。

  第三步:配置Action

   <package name="json_default" namespace="/" extends="json-default">//注意此處的extends配置

    <action name="user_*" class="Action.userAction" method="{1}">
      <result type="json">//此處指明類型
        <!-- 參數root指定要序列化得根對象 -->
        <!-- 預設將序列化當前Action中所有有傳回值的getter方法的值 -->
        <param name="root">list</param>
        <!-- 參數includeProperties指定要序列化根對象中的哪些屬性,多個屬性以逗號隔開-->
        <param name="includeProperties">msg,flag,user,user.userName</param>
        <!-- 參數excludeProperties指定要從根對象中排除的屬性,排除屬性將不被序列化-->
        <param name="excludeProperties">user.password</param>
        <!-- 參數excludeNullProperties指定是否序列化值為空白的屬性-->
        <param name="excludeNullProperties">true</param>
      </result>
    </action>
  </package>

  第四步:Struts2跳轉到Action中進行JSON的轉換《關鍵步驟》

  

  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設定檔進行資料轉換

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.