Struts2+Jquery+Json集成

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>
posted @ 2014-07-29 13:17  楪夕  阅读(314)  评论(0编辑  收藏  举报