zoukankan      html  css  js  c++  java
  • struts2 AJAX

    添加json 包:json-lib-2.1-jdk15.jar

    在jsp页面中,加入jquery库jquery-1.4.2.js

    1. JSP 

    代码
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    int increasement = 10;
    %>
    <%@taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
      
    <head>
        
    <base href="<%=basePath%>">
        
    <script type="text/javascript" src="js/frame/jquery-1.4.2.js"></script> 
        
    <script type="text/javascript" src="js/util.js"></script> 
        
    <link rel="stylesheet" href="css/baseType.css" type="text/css"></link>
        
    <script language="javascript">
               
    function getMsg(){
                  $.ajax({
                     url:
    'json/returnMsg.action'
                     type:
    'post',
                     dataType:
    'json'
                     success:
    function(data){ 
                        $(
    "#result").html(data.message);
                     }
                  });
                }
            
               
    function getUser(){ 
                  $(
    "# result ").html("");  
                  $.ajax({
                     url:
    'json/returnUser.action'
                     type:
    'post',
                     dataType:
    'json'
                     success:
    function(data){ 
                        $(
    "#result").append("用户ID:"+data.userInfo.userId+"")
                                    .append(
    "用户名:"+data.userInfo.userName+"")
                                    .append(
    "密码:"+data.userInfo.password+"");
                     }
                  });
                }
            
               
    function getUserList(){ 
                  $(
    "# result ").html("");   
                  $.ajax({
                     url:
    'json/returnList.action'
                     type:
    'post',
                     dataType:
    'json'
                     success:
    function(data){ 
                       $.each(data.userInfosList,
    function(i,value){   
                          $(
    "#result").append(""+(i+1)+"个用户")
                                      .append(
    "用户名:"+value.userName+"")
                                      .append(
    "密码:"+value.password+"");
                       })
                     }
                  });
                }
            
    </script>
      
    </head>
    <body>
    <div id="result"></div>
    <input type="button" value="获得单个消息" onclick="getMsg()"/>
    <input type="button" value="获得用户信息" onclick="getUser()"/>
    <input type="button" value="获得用户列表" onclick="getUserList()"/>

    </body>
    </html>

    2. struts.xml 配置

      

    代码
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
    >

    <struts>

        
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
        
    <constant name="struts.devMode" value="true" />
        
    <constant name= "struts.multipart.maxSize" value="5242880" />
        
    <constant name="struts.custom.i18n.resources" value="messageResource"/>

        
    <package name="default" namespace="/json" extends="json-default">
            
    <action name="returnMsg" class="com.carel.production.presentation.TestAction" method="returnMsg">   
                
    <result  name="success" type="json">
                    
    <param name="includeProperties">
                        message
                    
    </param>    
                
    </result>   
            
    </action> 
            
    <action name="returnUser" 
                    class
    ="com.carel.production.presentation.TestAction" method="returnUser">
                
    <result  name="success" type="json">
                    
    <param name="includeProperties">
                        userInfo\.userId,userInfo\.userName,userInfo\.password
                    
    </param>    
                
    </result>   
            
    </action>
            
    <action name="returnList" class="com.carel.production.presentation.TestAction"
                method
    ="returnList">   
                
    <result  name="success" type="json">
                    
    <param name="includeProperties">
                        userInfosList\[\d+\]\.userName,userInfosList\[\d+\]\.password
                    
    </param>
                
    </result>   
            
    </action>
        
    </package>
    </struts>

    3. Action(java pojo)

      

    代码
    package com.carel.production.presentation;

    import java.util.ArrayList;
    import java.util.List;

    import com.opensymphony.xwork2.ActionSupport;

    public class TestAction extends ActionSupport {
        
    private String message;                //使用json返回单个值
        private UserInfo userInfo;              //使用json返回对象
        private List userInfosList;     //使用josn返回List对象
        
       
    /*返回单个值*/   
       
    public String returnMsg(){   
            
    this.message = "成功返回单个值";  
            
    return SUCCESS; 
       }  
       
    /*返回UserInfo对象*/   
       
    public String returnUser(){ 
           userInfo 
    = new UserInfo();
           userInfo.setUserId(
    10000);
           userInfo.setUserName(
    "刘栋");
           userInfo.setPassword(
    "123456");
           
    return SUCCESS; 
       } 
      
    /*返回List对象*/   
       
    public String returnList(){   
           userInfosList 
    = new ArrayList<UserInfo>(); 
           UserInfo u1 
    = new UserInfo(); 
           u1.setUserId(
    10000);   
           u1.setUserName(
    "张三"); 
           u1.setPassword(
    "111111");
           UserInfo u2 
    = new UserInfo();
           u2.setUserId(
    10001);   
           u2.setUserName(
    "李四");   
           u2.setPassword(
    "222222"); 
           userInfosList.add(u1); 
           userInfosList.add(u2);  
           
    return SUCCESS;   
       }
    public String getMessage() {
        
    return message;
    }
    public void setMessage(String message) {
        
    this.message = message;
    }
    public UserInfo getUserInfo() {
        
    return userInfo;
    }
    public void setUserInfo(UserInfo userInfo) {
        
    this.userInfo = userInfo;
    }
    public List getUserInfosList() {
        
    return userInfosList;
    }
    public void setUserInfosList(List userInfosList) {
        
    this.userInfosList = userInfosList;
    }   
    }

     

    代码
    package com.carel.production.presentation;

    import java.io.Serializable;

    public class UserInfo implements Serializable {   
        
    private int userId;  
        
    private String userName; 
        
    private String password;
        
    public String getPassword() {
            
    return password;
        }
        
    public void setPassword(String password) {
            
    this.password = password;
        }
        
    public int getUserId() {
            
    return userId;
        }
        
    public void setUserId(int userId) {
            
    this.userId = userId;
        }
        
    public String getUserName() {
            
    return userName;
        }
        
    public void setUserName(String userName) {
            
    this.userName = userName;
        } 
    }
  • 相关阅读:

    转:在自己的工具条中使用ArcGIS Engine提供的命令和工具
    配置Subversion Apache TortoiseSVN
    vc++2005移除自定义向导目录
    NetAdvantage2006 For ASP.NET2.0印象
    三天了!今天终于成功安装ArcIMS9.2!
    vss 去除源代码管理
    转:设计模式趣谈
    转一篇:有关项目报告
    演练:使用 Visual Studio Team Test 进行单元测试
  • 原文地址:https://www.cnblogs.com/kevinge/p/1880456.html
Copyright © 2011-2022 走看看