这篇我们写关于个人信息的修改模块。
我们先把这个应用的登录进入后的首页先写出来。很简单,就是一些html代码标签等。
有一个main.jsp,是由好几个jsp页面组合而成
about.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="个人管理系统"/></title>
</head>
<body bgcolor="#AABBCCDD">
</body>
</html>
left.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="个人管理系统"/></title>
</head>
<body bgcolor="#CCCCFF">
<table>
<tr align="center">
<td>
<img src="../images/top.jpg" alt="正常" height="100" width="200">
</td>
</tr>
<tr>
<td>
<img src="../images/a.jpg" alt="长城" width="200">
</td>
</tr>
</table>
</body>
</html>
toop.jsp:其中,获取登录用户的用户名。然后显示在页面上。
<%@page import="JavaBean.UserNameBean"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="个人管理系统"/></title>
</head>
<body bgcolor="#CCDDEE">
<%
String loginname=null;
ArrayList login=(ArrayList)session.getAttribute("userName");
if(login==null||login.size()==0){
loginname="蜗牛";
}else{
for(int i=login.size()-1;i>=0;i--){
UserNameBean nm=(UserNameBean)login.get(i);
loginname=nm.getUserName();
}
}
%>
<table width="100%" align="right" bgcolor="blue">
<tr height="10" bgcolor="gray" align="center">
<td><a href="http://localhost:8080/ch04/personMessage/lookMessage.jsp" target="main">个人信息管理</a></td>
<td><a href="http://localhost:8080/ch04/friendManager/lookFriends.jsp" target="main">通讯录管理</a></td>
<td><a href="http://localhost:8080/ch04/dateTimeManager/lookDay.jsp" target="main">日程安排管理</a></td>
<td><a href="http://localhost:8080/ch04/fileManager/lookFile.jsp" target="main">个人文件管理</a></td>
<td><a href="http://localhost:8080/ch04/login/index.jsp" target="_top">退出主页面</a></td>
<td>欢迎<%=loginname%>使用本系统!</td>
</tr>
</table>
</body>
</html>
top.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="个人管理系统"/></title>
</head>
<body bgcolor="#CCCCFF">
<table width="100%" align="center">
<tr align="center">
<td align="right">
<img src="../images/cc.gif" alt="为之则易,不为则难!" height="80">
</td>
<td align="left">
<h1><font color="blue">欢迎使用个人信息管理平台</font></h1>
</td>
</tr>
</table>
</body>
</html>
最后,是我们的main.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="个人信息管理系统"/></title>
</head>
<frameset cols="20%,*" framespacing="0" border="no" frameborder="0">
<frame src="../mainFrame/left.jsp" name="left" scrolling="no">
<frameset rows="20%,10%,*">
<frame src="../mainFrame/top.jsp" name="top" scrolling="no">
<frame src="../mainFrame/toop.jsp" name="toop" scrolling="no">
<frame src="../mainFrame/about.jsp" name="main">
</frameset>
</frameset>
</html>
接下来。写一个个人信息显示的jsp-------------lookMessage.jsp...获取个人信息 ,然后一一显示在页面上。
<%@page import="JavaBean.MyMessBean"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="个人信息管理系统"></s:text></title>
</head>
<body bgcolor="gray">
<hr noshade/>
<s:div align="center">
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<tr>
<td width="33%">
<s:a href="http://localhost:8080/ch04/personMessage/updateMessage.jsp">修改个人信息</s:a>
</td>
<td width="33%">
<s:text name="查看个人信息"></s:text>
</td>
<td width="33%">
<s:a href="http://localhost:8080/ch04/personMessage/updatePass.jsp">修改个人密码</s:a>
</td>
</tr>
</table>
</s:div>
<hr noshade/>
<table border="5" cellspacing="0" cellpadding="0" bgcolor="#95BDFF" width="60%" align="center">
<%
ArrayList MyMessage=(ArrayList)session.getAttribute("MyMess");
if(MyMessage==null||MyMessage.size()==0){
response.sendRedirect("http://localhost:8084/ch04/login/index.jsp");
}else{
for(int i=MyMessage.size()-1;i>=0;i--){
MyMessBean mess=(MyMessBean)MyMessage.get(i);
%>
<tr>
<td height="30"><s:text name="用户姓名"></s:text></td>
<td><%=mess.getName()%></td>
</tr>
<tr>
<td height="30"><s:text name="用户性别"></s:text></td>
<td><%=mess.getSex()%></td>
</tr>
<tr>
<td height="30"><s:text name="出生日期"></s:text></td>
<td><%=mess.getBirth()%></td>
</tr>
<tr>
<td height="30"><s:text name="用户民族"></s:text></td>
<td><%=mess.getNation()%></td>
</tr>
<tr>
<td height="30"><s:text name="用户学历"></s:text></td>
<td><%=mess.getEdu()%></td>
</tr>
<tr>
<td height="30"><s:text name="用户职称"></s:text></td>
<td><%=mess.getWork()%></td>
</tr>
<tr>
<td height="30"><s:text name="用户电话"></s:text></td>
<td><%=mess.getPhone()%></td>
</tr>
<tr>
<td height="30"><s:text name="家庭住址"></s:text></td>
<td><%=mess.getPlace()%></td>
</tr>
<tr>
<td height="30"><s:text name="邮箱地址"></s:text></td>
<td><%=mess.getEmail()%></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
个人信息修改的UpdateMessAction.java..先是用DB类的returnLogin()方法返回登录的用户名,然后通过updateMess()更新个人的信息。
package edu.personManager.Action;
import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
public class UpdateMessAction extends ActionSupport implements ServletRequestAware {
private String name;
private String sex;
private String birth;
private String nation;
private String edu;
private String work;
private String phone;
private String place;
private String email;
private String userName;
private HttpServletRequest request;
private String message="ERROR";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getEdu() {
return edu;
}
public void setEdu(String edu) {
this.edu = edu;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setServletRequest(HttpServletRequest hsr) {
request=hsr;
}
// public void message(String msg){
// int type=JOptionPane.YES_NO_OPTION;
// String title="信息提示";
// JOptionPane.showMessageDialog(null,msg,title,type);
// }
@Override
public void validate(){
if(getName()==null||getName().length()==0){
addFieldError("name","用户姓名不允许为空!");
}
if(getSex()==null||getSex().length()==0){
addFieldError("sex","用户性别不允许为空!");
}
// if(!(getSex().equals("男"))||!(getSex().equals("女"))){
// addFieldError("sex","用户性别符合标准!");
// }
if(getBirth()==null||getBirth().length()==0){
addFieldError("birth","用户生日不允许为空!");
}else{
if(getBirth().length()!=10){
addFieldError("birth","用户生日格式为'yyyy-mm-dd'!");
}else{
String an=this.getBirth().substring(4, 5);
String bn=this.getBirth().substring(7, 8);
if(!(an.equals("-"))||!(bn.equals("-"))){
addFieldError("birth","用户生日格式为'yyyy-mm-dd'!");
}
}
}
if(getNation()==null||getNation().length()==0){
addFieldError("nation","用户民族不允许为空!");
}
if(getEdu()==null||getEdu().length()==0){
addFieldError("edu","用户学历不允许为空!");
}
if(getWork()==null||getWork().length()==0){
addFieldError("work","用户工作不允许为空!");
}
if(getPhone()==null||getPhone().length()==0){
addFieldError("phone","用户电话不允许为空!");
}
if(getPlace()==null||getPlace().length()==0){
addFieldError("place","用户地址不允许为空!");
}
if(getEmail()==null||getEmail().length()==0){
addFieldError("email","用户email不允许为空!");
}
}
public String execute() throws Exception {
DB mysql=new DB();
userName=mysql.returnLogin(request);
String mess=mysql.updateMess(request, userName, this.getName(), this.getSex(), this.getBirth(), this.getNation(), this.getEdu(), this.getWork(), this.getPhone(), this.getPlace(), this.getEmail());
if(mess.equals("ok")){
message="SUCCESS";
}
return message;
}
}
updateMessage.jsp
<%@page import="JavaBean.MyMessBean"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="个人信息管理系统->修改信息"></s:text></title>
</head>
<body bgcolor="gray">
<hr noshade/>
<s:div align="center">
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<tr>
<td width="33%">
<s:text name="修改个人信息"></s:text>
</td>
<td width="33%">
<s:a href="http://localhost:8080/ch04/personMessage/lookMessage.jsp">查看个人信息</s:a>
</td>
<td width="33%">
<s:a href="http://localhost:8080/ch04/personMessage/updatePass.jsp">修改个人密码</s:a>
</td>
</tr>
</table>
</s:div>
<hr noshade/>
<s:form action="upMessAction" method="post">
<table border="5" cellspacing="0" cellpadding="0" bgcolor="#95BDFF" width="60%" align="center">
<%
ArrayList MyMessage=(ArrayList)session.getAttribute("MyMess");
if(MyMessage==null||MyMessage.size()==0){
response.sendRedirect("http://localhost:8084/ch04/login/index.jsp");
}else{
for(int i=MyMessage.size()-1;i>=0;i--){
MyMessBean mess=(MyMessBean)MyMessage.get(i);
%>
<tr>
<td height="30"><s:text name="用户姓名"></s:text></td>
<td><input type="text" name="name" value="<%=mess.getName()%>"/></td>
</tr>
<tr>
<td height="30"><s:text name="用户性别"></s:text></td>
<td><input type="text" name="sex" value="<%=mess.getSex()%>"/></td>
</tr>
<tr>
<td height="30"><s:text name="出生日期"></s:text></td>
<td><input type="text" name="birth" value="<%=mess.getBirth()%>"/></td>
</tr>
<tr>
<td height="30"><s:text name="用户民族"></s:text></td>
<td><input type="text" name="nation" value="<%=mess.getNation()%>"/></td>
</tr>
<tr>
<td height="30"><s:text name="用户学历"></s:text></td>
<td><input type="text" name="edu" value="<%=mess.getEdu()%>"/></td>
</tr>
<tr>
<td height="30"><s:text name="用户职称"></s:text></td>
<td><input type="text" name="work" value="<%=mess.getWork()%>"/></td>
</tr>
<tr>
<td height="30"><s:text name="用户电话"></s:text></td>
<td><input type="text" name="phone" value="<%=mess.getPhone()%>"/></td>
</tr>
<tr>
<td height="30"><s:text name="家庭住址"></s:text></td>
<td><input type="text" name="place" value="<%=mess.getPlace()%>"/></td>
</tr>
<tr>
<td height="30"><s:text name="邮箱地址"></s:text></td>
<td><input type="text" name="email" value="<%=mess.getEmail()%>"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="确 定"/>
<input type="reset" value="还 原"/>
</td>
</tr>
<%
}
}
%>
</table>
</s:form>
</body>
</html>
下面是修改密码的action————————————————UpdatePassAction.java
package edu.personManager.Action;
import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;
import org.apache.struts2.interceptor.ServletRequestAware;
public class UpdatePassAction extends ActionSupport implements ServletRequestAware{
private String password1;
private String password2;
private String userName;
private HttpServletRequest request;
private String message="ERROR";
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public void setServletRequest(HttpServletRequest hsr) {
request=hsr;
}
public void message(String msg){
int type=JOptionPane.YES_NO_OPTION;
String title="信息提示";
JOptionPane.showMessageDialog(null,msg,title,type);
}
public void validate(){
if(!(password1.equals(password2))){
message("两次密码不同!");
addFieldError("password2","两次密码不同!");
}
}
public String execute() throws Exception {
DB mysql=new DB();
userName=mysql.returnLogin(request);
String pass=mysql.updatePass(request, userName, this.getPassword1());
if(pass.equals("ok")){
message="SUCCESS";
}
return message;
}
}
updatepass.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="JavaBean.UserNameBean"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="个人信息管理系统->修改密码"></s:text></title>
</head>
<body bgcolor="gray">
<hr noshade/>
<s:div align="center">
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<tr>
<td width="33%">
<s:a href="http://localhost:8080/ch04/personMessage/updateMessage.jsp">修改个人信息</s:a>
</td>
<td width="33%">
<s:a href="http://localhost:8080/ch04/personMessage/lookMessage.jsp">查看个人信息</s:a>
</td>
<td width="33%">
<s:text name="修改个人密码"></s:text>
</td>
</tr>
</table>
</s:div>
<hr noshade/>
<s:form action="upPassAction" method="post">
<table border="5" cellspacing="0" cellpadding="0" bgcolor="#95BDFF" width="60%" align="center">
<%
ArrayList login=(ArrayList)session.getAttribute("userName");
if(login==null||login.size()==0){
response.sendRedirect("http://localhost:8080/ch04/login/index.jsp");
}else{
for(int i=login.size()-1;i>=0;i--){
UserNameBean nm=(UserNameBean)login.get(i);
%>
<tr>
<td height="30"><s:text name="用户密码"></s:text></td>
<td><input type="text" name="password1" value="<%=nm.getPassword()%>"/></td>
</tr>
<tr>
<td height="30"><s:text name="重复密码"></s:text></td>
<td><input type="text" name="password2" value="<%=nm.getPassword()%>"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="确 定" size="12"/>
<input type="reset" value="清 除" size="12"/>
</td>
</tr>
<%
}
}
%>
</table>
</s:form>
</body>
</html>
这就是个人信息的修改模块。代码阅读起来应该是没什么难度。