Struts2+Jquery+Json整合

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   os   檔案   io   

1.建立web項目,引入相關jar包。包括struts2核心包、commons相關包、Json相關包(這些jar包都可以在struts2的lib檔案夾中找到)

  

2.引入jquery-1.8.2.min.js

3.在web.xml中配置struts2的"核心控制器"

 1 <!-- 配置struts2的核心控制器 --> 2     <filter> 3         <filter-name>struts2</filter-name> 4         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 5     </filter> 6  7     <filter-mapping> 8         <filter-name>struts2</filter-name> 9         <url-pattern>/*</url-pattern>10     </filter-mapping>

3.ajax請求頁面(index.jsp):

 1 <head> 2 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 3 <title>Insert title here</title> 4 <!-- 引入jquery.js檔案 --> 5 <script type="text/javascript" src="js/jquery-1.8.2.min.js" charset="utf-8"></script> 6 <script type="text/javascript"> 7     function btn(){ 8         $(‘#btn‘).bind(‘click‘, function(){ 9             $.ajax({10                 type:‘post‘,11                 url:‘json_executeAjax.action‘,12                 data:{13                     name : $(‘#userName‘).val()14                 },15                 dataType:‘json‘,16                 success:function(data){17                     console.info(data);18                 },19                 error:function(){20                     alert(‘系統異常,請聯絡管理員‘);21                 }22             });        23         });24     }25     //載入完畢,調用函數26     $(document).ready(function(){27         btn();28     });    29     30 </script>31 32 </head>33 <body>34     <input type="text" name="userName" id="userName"/>35     <input type="button" value="ajax請求" id="btn"/>36 </body>

4.action:

 1 package com.xyy.action; 2  3 import java.util.HashMap; 4 import java.util.Map; 5 import net.sf.json.JSONObject; 6 import com.opensymphony.xwork2.Action; 7  8 public class JsonAction { 9     10     private String name;11     //儲存返回結果,並在struts.xml中配置12     private String result;13 14     public String getResult() {15         return result;16     }17 18     public void setResult(String result) {19         this.result = result;20     }21 22     public String getName() {23         return name;24     }25 26     public void setName(String name) {27         this.name = name;28     }29     30     public String executeAjax(){31         Map<String, Object> map = new HashMap<String, Object>();32         map.put("name", name);33         //將Map轉換成json對象34         JSONObject json =  JSONObject.fromObject(map);35         result = json.toString();36         return Action.SUCCESS;37     }38     39 }

5.在struts.xml中配置action:

 1 <struts> 2     <package name="struts2" extends="struts-default,json-default"> 3         <action name="json_*" class="com.xyy.action.JsonAction" method="{1}"> 4             <!-- 傳回型別為Json,需要繼承json-default,name預設是success --> 5             <result type="json"> 6              7                  <!-- root的值對應要返回的值的屬性 -->   8                 <!-- 這裡的result值即是 對應action中的 result -->   9                 <param name="root">result</param>10                 11             </result>12         </action>13     </package>14 </struts>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.