Struts2 Jquery+Ajax提交数据到后台

//UpdateAction.java



package com.abin.hospital.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.abin.hospital.bean.UserBean;
import com.abin.hospital.singleton.UpdateSingleton;
import com.abin.hospital.singleton.UserSingleton;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UpdateAction extends ActionSupport{
private static final UpdateSingleton updateSinglenton=UpdateSingleton.getInstance();
private String result;

public String update(){
ActionContext context=ActionContext.getContext();
HttpServletRequest request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
String username=request.getParameter("username");
String password=request.getParameter("password");
String password1=request.getParameter("password1");
String password2=request.getParameter("password2");
UserBean bean=null;
try {
bean=this.updateSinglenton.findUserByUserID(username);
} catch (Exception e) {
e.printStackTrace();
}

if(bean==null||bean.getUserid()==null){
result="notexist";
return "json";
}

if(bean.getUserid()!=null&&!bean.getPassword().equals(password)){
result="failure";
return "json";
}

if(bean.getUserid()!=null&&bean.getPassword().equals(password)){
int end=0;
try {
end=this.updateSinglenton.updateUserByUserID(bean,password1);
} catch (Exception e) {
e.printStackTrace();
}
if(end==1){
result="success";
return "json";
}else{
result="updatefailure";
return "json";
}

}

return null;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}
}




//struts-update.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>
<package name="userupdate" extends="json-default" namespace="/user" >

<action name="update" method="update" class="com.abin.hospital.action.UpdateAction">
<result name="json" type="json"></result>
</action>

</package>


</struts>





//update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Young ABIN</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<script type="text/javascript" src="../js/jquery-1.7.min.js"></script>
<script type="text/javascript">
function Validate() {
if($("#user").val()==""){
alert("用户名不能为空!");
$("#user").focus();
return false;
}
if($("#oldpass").val()==""){
alert("旧密码不能为空");
$("#oldpass").focus();
return false;
}
if($("#newpass1").val()==""){
alert("新密码不能为空");
$("newpass1").focus();
return false;
}
if($("#newpass2").val()==""){
alert("新确认密码不能为空");
$("newpass2").focus();
return false;
}
if($("#newpass1").val()!=$("#newpass2").val()){
alert("两次填写的新密码不一样,请您重新填写");
$("newpass1").focus();
return false();
}

var username=$("input[name='username']").val();
// alert("用户名="+username);
var password=$("input[name='password']").val();
// alert("password="+password);
var password11=$("input[name='password11']").val();
// alert("password1="+password11);
var password22=$("input[name='password22']").val();
alert("password2="+password22);

$.ajax({
url : "../user/update.action",
type : "post",
data : ({username : username ,password : password,password1 : password11,password2 : password22}),
success : function(data){
if(data.result=="notexist"){
alert("该用户不存在,请您先注册");
}else if(data.result=="success"){
// alert("修改密码成功");
document.write("修改密码成功")
document.write("<br/>");
window.location.href="../login.jsp";
}else if(data.result=="failure"){
alert("旧密码不正确");
}else if(data.result=="updatefailure"){
alert("用户密码修改失败,请您重新修改");
}
}

});

}
</script>
<body >
用户名:
<input id="user" name="username" type="text" />
<br />
旧密码:
<input id="oldpass" name="password" type="password" />
<br />
密码:
<input id="newpass1" name="password11" type="password" />
<br />
确认密码:
<input id="newpass2" name="password22" type="password" />
<br />
<input value="提交" type="submit" onClick="Validate()"/>

<input value="重置" type="reset" />
<br />

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值