相关配置文件
CommonManager.xml
<bean id="updatePasswordAction" class="cn.itsouce.pss.web.UpdatePasswordAction"
scope="prototype">
<property name="employeeService" ref="employeeService" />
</bean>
struts.xml
<!-- 处理修改密码 -->
<action name="updatePassword_*" class="updatePasswordAction" method="{1}">
<result>/WEB-INF/jsps/updatePassword.jsp</result>
</action>
后台Action处理的代码
package com.liuzm.mypss.web;
import org.apache.struts2.ServletActionContext;
import com.liuzm.mypss.domain.Employee;
import com.liuzm.mypss.service.IEmployeeService;
import com.liuzm.mypss.utils.MD5Encrypt;
public class UpdatePasswordAction extends BaseAction {
private IEmployeeService employeeService;
private String oldPassword;
private String newPassword;
public void setEmployeeService(IEmployeeService employeeService) {
this.employeeService = employeeService;
}
@Override
public String execute() throws Exception {
System.out.println("employeeService====="+employeeService);
return SUCCESS;
}
public String update() throws Exception {
// 从session中获取用户
Employee employee = (Employee) ServletActionContext.getRequest()
.getSession().getAttribute(LOGIN_USER);
oldPassword = MD5Encrypt.INSTANCE.encrypt(oldPassword);
if (employee.getPassword().equals(oldPassword)) {// 旧密码输入正确
newPassword = MD5Encrypt.INSTANCE.encrypt(newPassword);
employee.setPassword(newPassword);
employeeService.update(employee);
// 销毁HttpSession里面的登录用户
ServletActionContext.getRequest().getSession()
.removeAttribute(LOGIN_USER);
return LOGIN;
}else {
addActionError("旧密码输入错误!");
return SUCCESS;
}
}
public String getOldPassword() {
return oldPassword;
}
public void setOldPassword(String oldPassword) {
this.oldPassword = oldPassword;
}
public String getNewPassword() {
return newPassword;
}
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
}
前台页面表单和相关js验证
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link href="css/index.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" media="screen"
href="js/validate/css/screen.css" />
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/validate/jquery.validate.js" type="text/javascript"></script>
<script src="js/validate/messages_cn.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
$().ready(function() {
$("#updatePasswordForm").validate({
rules: {
oldPassword: {
required: true,
minlength: 5
},
newPassword: {
required: true,
minlength: 5,
equalNotTo:"#oldPassword"
},
confirmPassword: {
required: true,
minlength: 5,
equalTo: "#newPassword"
}
},
messages:{
newPassword: {
equalNotTo:"新密码和旧密码不应该相同"
},
confirmPassword:{
equalTo: "确认密码和新密码不一致"
}
}
});
});
//-->
</script>
<title>成都蓝源进销存系统(教学版)-系统主页</title>
</head>
<body>
<%@ include file="/WEB-INF/views/message.jsp" %>
<s:form id="updatePasswordForm" action="updatePassword_update" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#FFFFFF">
<td width="22%" height="30" align="center">旧密码:</td>
<td width="29%">
<s:textfield id="oldPassword" name="oldPassword" />
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="22%" height="30" align="center">新密码:</td>
<td width="29%">
<s:textfield id="newPassword" name="newPassword" />
</tr>
<tr bgcolor="#FFFFFF">
<td width="22%" height="30" align="center">确认密码:</td>
<td width="29%">
<s:textfield name="confirmPassword" />
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="22%" height="30" align="center"></td>
<td width="29%">
<s:submit value="修改密码" />
</td>
</tr>
</table>
</s:form>
<!--"content-right"end-->
</body>
</html>