zoukankan      html  css  js  c++  java
  • 【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>
  • 相关阅读:

    IT人的素质 & 设计杂谈
    结构化思维思维的结构
    [WM].NET CF下如何提高应用程序的性能 【转载】
    无题
    [WM]谁抢走了应用程序的性能? 【转载】
    繁体编码文本文件转换为简体编码的工具
    生成VB多行字符串常量的工具
    跟我一步一步开发自己的Openfire插件
    cnblogs博文浏览[推荐、Top、评论、关注、收藏]利器代码片段
  • 原文地址:https://www.cnblogs.com/qiudeqing/p/3226941.html
Copyright © 2011-2022 走看看