日期:2014-05-16  浏览次数:20703 次

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>