【struts2】【4】通过ajax向struts2 action请求json

 

  1. 拷贝对应版本的struts2-json-plugin.jar到WEB-INF/lib下
  2. 为需要返回json数据的action配置json result。有两种方式可以达到这个目的
    1. 在普通package中添加json result声明。
    2. package继承json-default
  3. 编写action所需的类
  4. 配置action。设置返回类型为json
  5. 在前台请求配置的action。得到对应action的json格式字符串

 

JsonAction.java

 

 1 package action;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import com.opensymphony.xwork2.ActionSupport;
 7 
 8 public class JsonAction extends ActionSupport{
 9     
10     private List<String> users;
11     
12     @Override
13     public String execute()
14     {
15         users = new ArrayList<String>();
16         users.add("qiu deqing");
17         users.add("jobx");
18         users.add("bill");
19         return SUCCESS;
20     }
21 
22     public List<String> getUsers() {
23         return users;
24     }
25 
26     public void setUsers(List<String> users) {
27         this.users = users;
28     }
29     
30     
31 
32 }

 

 

struts.xml

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7 
 8     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 9     <constant name="struts.devMode" value="true" />
10 
11     <package name="default" namespace="/" extends="json-default">
12 
13     
14         <action name="userList" class="action.JsonAction">
15             <result type="json">
16             </result>
17         </action>
18     
19 
20         <action name="index">
21             <result>/index.jsp</result>
22         </action>
23 
24     </package>
25 
26 
27 
28 </struts>

 

index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE html>
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'index.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21     
22     <script>
23     
24         function listUsers()
25         {
26             var xhr;
27             if (window.XMLHttpRequest)
28             {
29                 xhr = new XMLHttpRequest();
30             } else
31             {
32                 xhr = new ActiveXObject("Microsoft.XMLHTTP");
33             }
34             xhr.open("GET", "userList", true);
35             xhr.onreadystatechange = function() {
36                 var ready = xhr.readyState;
37                 if (ready == 4)
38                 {
39                     var status = xhr.status;
40                     if (status >= 200 && status < 300)
41                     {
42                         alert(xhr.responseText);
43                     }
44                 }
45             }; 
46             
47             xhr.send();
48             
49         } // end listUsers()
50         
51         window.onload = function()
52         {
53             var listButton = document.getElementById("listUsers");
54             listButton.onclick = listUsers;
55             
56         };
57     
58     </script>
59     
60   </head>
61   
62   <body>
63     This is my JSP page. <br>
64     <button id="listUsers">list users</button>
65     
66   </body>
67 </html>

 

posted @ 2013-07-31 08:11  我的百科全书  阅读(295)  评论(0编辑  收藏  举报