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/>

     
<input id="Button1" type="button" class="btn" value="登录"/>
 
<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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值