当前位置: 代码迷 >> Ajax >> struts2中实施query中的ajax
  详细解决方案

struts2中实施query中的ajax

热度:318   发布时间:2012-11-23 00:03:29.0
struts2中执行query中的ajax
1.页面代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>jQuery与Struts2的整合</title>
    <meta http-equiv="content-type" content="text/html; charset=GBK">

   
    <style type="text/css">

    body{font-size:13px}
    .divFrame{width:250px;border:solid 1px #666}
    .divFrame .divTitle{padding:5px;background-color:#eee}
    .divFrame .divContent{padding:8px}
    .divFrame .divContent .clsShow{font-size:14px}
    .btn{border:#666 1px solid;padding:2px;width:50px;
    filter:progid:DXImageTransform.Microsoft
    .Gradient(GradientType=0,StartColorStr=#ffffff,
    EndColorStr=#ECE9D8);}
    form{padding:0px;margin:0px}
    }
    </style>
    <script type="text/javascript" src="js/jquery-1.2.6.js"></script>

    <script type="text/javascript">
   
    $(document).ready(function(){
    $("#Button1").click(function(){
    alert("button");
    var name=$("#mname").val();
    var pass=$("#mpass").val();
    $.ajax({
    url:"login.action",
    method:"post",
    dataType:"html",
    data:{"name":encodeURI(name),"password":encodeURI(pass)},
    success:function(strValue){
    alert(strValue)
    }
    })
    })
    })
    </script>
  </head>
 
  <body>
    <div class="divFrame">

    <div class="divTitle">用户登录
    </div>
    <div class="divContent">

       
        <div id="divTip"></div>

        <div id="box">

            <form id="myForm">

               名称:<input type="text" id="mname" name="name"/><br/>

               密码:<input type="password" id="mpass" name="password"/><br/>

           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
               <input id="Button1" type="button" class="btn" value="登录"/>
               &nbsp;
               <input id="Button2" type="reset" class="btn" value="取消"/>
            </form>
        </div>
    </div>
    </div>
    <br/>
    本网页显示的是通过$.ajax()方法向struts2.action传递数据,<br/>其中action中execute()方法返回为空,<br/>并通过【ServletActionContext.getResponse().getWriter().print(result);】 方法将数据传到jQuery中。
  </body>

</html>
2.action
package com.test.user;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport {

private String name;
private String password;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String loginCheck() throws Exception {
System.out.println("name="+name+" pass="+password);
boolean result=false;
if("111".equals(name)&& "222".equals(password)){
result=true;
}

ServletActionContext.getResponse().getWriter().print(result);

return null;
}


}
3.配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="user" extends="struts-default,json-default">
<action name="login" class="com.test.user.Login" method="loginCheck">
<result type="json"></result>
</action>
</package>
</struts>   
注意1.加入struts2-json-plugin-2.2.1.jar包,
2.继承json-default
  相关解决方案