环境配置
- 数据库:
#用户表
CREATE TABLE S_User(
userID INT NOT NULL AUTO_INCREMENT, #主键ID
userName VARCHAR(50) NULL, #用户姓名
logonName VARCHAR(50) NULL, #登录名
logonPwd VARCHAR(50) NULL, #密码#
sex VARCHAR(10) NULL, #性别(例如:男,女)
birthday VARCHAR(50) NULL, #出生日期
education VARCHAR(20) NULL, #学历(例如:研究生、本科、专科、高中)
telephone VARCHAR(50) NULL, #电话
interest VARCHAR(20) NULL, #兴趣爱好(例如:体育、旅游、逛街)
path VARCHAR(500) NULL, #上传路径(path路径)
filename VARCHAR(100) NULL, #上传文件名称(文件名)
remark VARCHAR(500) NULL, #备注
PRIMARY KEY (userID)
)
#初始化数据:默认用户名和密码是admin
INSERT INTO s_user (userID,userName,logonName,logonPwd) VALUES (1,'超级管理员','admin','admin')
- 写User类
public class User implements Serializable {
private Integer userID;
private String userName;
private String logonName;
private String logonPwd;
private String sex;
private String birthday;
private String education;
private String telephone;
private String interest;
private String path;
private String filename;
private String remark;
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Integer getUserID() {
return userID;
}
public void setUserID(Integer userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getLogonName() {
return logonName;
}
public void setLogonName(String logonName) {
this.logonName = logonName;
}
public String getLogonPwd() {
return logonPwd;
}
public void setLogonPwd(String logonPwd) {
this.logonPwd = logonPwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
- 写后台接口
- 导包
- 写用户服务功能
- 完成后台功能
UserService.java
public class UserService implements IUserService {
private IUserDao dao = new UserDaoImpl();
@Override
public User login(String logonName, String logonPwd) {
return dao.selectUserByInfo(logonName,logonPwd);
}
@Override
public int saveUser(User user) {
return dao.addUser(user);
}
@Override
public int modifyUser(User user) {
return dao.updateUser(user);
}
@Override
public int removeUser(Integer userID) {
return dao.deleteUser(userID);
}
@Override
public List<User> findAllUser() {
return dao.selectUserById();
}
@Override
public List<User> findUserByCondition(String userName, String sex, String education, String isUpload) {
return dao.selectUserByCondition(userName,sex,education,isUpload);
}
@Override
public User findUserById(Integer userID) {
return dao.findUserById(userID);
}
}
UserDaoImpl.java
public class UserDaoImpl implements IUserDao {
private QueryRunner qr = new QueryRunner(JNDIUtil.getDataSource());
public User selectUserByInfo(String logonName, String logonPwd) {
try{
return qr.query("select * from S_User where logonName=? and logonPwd=? ", new BeanHandler<User>(User.class),logonName,logonPwd);
}catch(Exception e){
throw new RuntimeException(e);
}
}
public int addUser(User user) {
try{
return qr.update("insert into S_User(userID,userName,logonName,logonPwd,sex,birthday,education,telephone,interest,path,filename,remark)values(?,?,?,?,?,?,?,?,?,?,?,?)",
user.getUserID(),user.getUserName(),user.getLogonName(),user.getLogonPwd(),user.getSex(),user.getBirthday(),user.getEducation(),user.getTelephone(),
user.getInterest(),user.getPath(),user.getFilename(),user.getRemark());
}catch(Exception e){
throw new RuntimeException(e);
}
}
public int updateUser(User user) {
try{
return qr.update("update S_User set userName=?,logonName=?,logonPwd=?,sex=?,birthday=?,education=?,telephone=?,interest=?,path=?,filename=?,remark=? where userID = ?",
user.getUserName(),user.getLogonName(),user.getLogonPwd(),user.getSex(),user.getBirthday(),user.getEducation(),user.getTelephone(),
user.getInterest(),user.getPath(),user.getFilename(),user.getRemark(),user.getUserID());
}catch(Exception e){
throw new RuntimeException(e);
}
}
public int deleteUser(Integer userid) {
try{
return qr.update("delete from S_User where userID = ?",userid);
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Override
public List<User> selectUserByCondition(String userName, String sex, String education, String isUpload) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<User> selectUserById() {
try{
return qr.query("select * from S_User ", new BeanListHandler<User>(User.class));
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Override
public User findUserById(Integer userID) {
try{
return qr.query("select * from S_User where userID = ? ", new BeanHandler<User>(User.class),userID);
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
用户登录功能
- 更改页面信息
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<script type="text/javascript">
function ini(){
document.form1.logonName.focus();
}
</script>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link href="${pageContext.request.contextPath}/css/Style.css" rel="stylesheet" type="text/css">
</head>
<body onload="ini()">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<table width="452" height="290" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#FFFFFF">
<table width="452" height="290" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="74">
<img src="${pageContext.request.contextPath}/images/logintitle.gif">
</td>
</tr>
<tr>
<td align="center" valign="bottom" background="${pageContext.request.contextPath}/images/loginbg.gif">
<s:form action="login" id="loginAction_home" name="form1" target="_parent">
<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr align="center">
<td height="30" colspan="2" style="border-bottom: 1px dotted #cccccc">
<strong style="font-size: 14px;">请登录</strong>
</td>
</tr>
<tr>
<td height="30" nowrap>
<font color="000F60"><strong>用户名:</strong> </font>
</td>
<td>
<s:textfield name="logonName" id="logonName" cssClass="text" cssStyle="width: 160px;"></s:textfield>
</td>
</tr>
<tr>
<td height="30" nowrap>
<strong><font color="000F60">密码: </font> </strong>
</td>
<td>
<s:password name="logonPwd" cssClass="text" cssStyle="width: 160px;"></s:password>
</td>
</tr>
<tr>
<td height="30" nowrap colspan="2">
<strong><font color="red"><s:actionerror/></font> </strong>
</td>
</tr>
<tr>
<td height="30">
</td>
<td>
<s:submit value="登录" cssClass="buttoninput"></s:submit>
<s:reset value="重置" cssClass="buttoninput"></s:reset>
</td>
</tr>
</table>
</s:form>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="30" align="center">
</td>
</tr>
<tr>
<td height="23" align="center"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
- 修改struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="p1" extends="struts-default">
<action name="login" class="com.lwb.action.UserAction" method="login">
<result>/login/home.jsp</result>
<result name="input">/login/login.jsp</result>
</action>
</package>
</struts>
- 创建动作类
public class UserAction extends ActionSupport implements ModelDriven<User> {
private IUserService ius = new UserService();
private User user = new User();
//模型驱动
public User getModel() {
return user;
}
//用户登录
public String login(){
User dbuser = ius.login(user.getLogonName(), user.getLogonPwd());
//判断查找到用户没有
if(dbuser==null){
addActionError("用户名或者密码不匹配!!");
return "input";
}
//登录成功
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("user", dbuser);
return SUCCESS;
}
public IUserService getIus() {
return ius;
}
public void setIus(IUserService ius) {
this.ius = ius;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
用户添加功能
- 修改添加页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<HTML>
<HEAD>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<LINK href="${pageContext.request.contextPath}/css/Style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/public.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/check.js"></script>
<!-- 日期插件,使用jquery -->
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery-1.4.2.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/jquery/jquery.datepick.css" type="text/css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery.datepick.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery.datepick-zh-CN.js"></script>
</HEAD>
<script type="text/javascript">
$(document).ready(function(){
//使用class属性处理 'yy-mm-dd' 设置格式"yyyy/mm/dd"
$('#birthday').datepick({dateFormat: 'yy-mm-dd'});
});
</script>
<body>
<s:form action="add" namespace="/user" enctype="multipart/form-data">
<table cellSpacing="1" cellPadding="5" width="100%" align="center" bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
<tr>
<td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4"height="26">
<STRONG>添加用户</STRONG>
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
登录名:
</td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<s:textfield name="logonName" cssClass="bg"></s:textfield>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
密码:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:password name="logonPwd"></s:password>
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">
用户姓名:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:textfield name="userName" cssClass="bg"></s:textfield>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
性别:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:radio name="sex" list="{'男','女'}" value="'男'"></s:radio>
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">
学历:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:select name="education" list="{'研究生','本科','专科','高中','幼儿园'}" headerKey="" headerValue="---选择学历---"></s:select>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
出生日期:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:textfield name="birthday" size="20" readonly="readonly" id="birthday"></s:textfield>
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">
电话:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:textfield name="telephone"></s:textfield>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
兴趣爱好:
</td>
<td class="ta_01" bgColor="#ffffff" colSpan="3">
<s:checkboxlist name="interest" list="{'看电影','旅游','健身','购物','睡觉'}"></s:checkboxlist>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
简历资料:
</td>
<td class="ta_01" bgColor="#ffffff" colSpan="3">
<s:file name="upload" size="30"></s:file>
</td>
</tr>
<TR>
<TD class="ta_01" align="center" bgColor="#f5fafe">
备注:
</TD>
<TD class="ta_01" bgColor="#ffffff" colSpan="3">
<s:textarea name="remark" cols="30" rows="3" cssStyle="WIDTH: 96%"></s:textarea>
</TD>
</TR>
<TR>
<td align="center" colSpan="4" class="sep1">
<img src="${pageContext.request.contextPath}/images/shim.gif">
</td>
</TR>
<tr>
<td class="ta_01" style="WIDTH: 100%" align="center"
bgColor="#f5fafe" colSpan="4">
<button type="submit" id="userAction_save_do_submit" name="submit" value="确定" class="button_ok">
确定
</button>
<FONT face="宋体"> </FONT>
<button type="reset" value="重置" class="button_cancel">重置</button>
<FONT face="宋体"> </FONT>
<INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/>
<span id="Label1"></span>
</td>
</tr>
</table>
</s:form>
</body>
</HTML>
- 配置struts.xml
<!-- 添加用户 -->
<action name="add" class="com.lwb.action.UserAction" method="add">
<!-- 配置上传文件的扩展名限制-->
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">.doc,.docx</param>
</interceptor-ref>
<result>/user/list.jsp</result>
<result name="input">/user/add.jsp</result>
</action>
- 添加动作方法
//用户添加
//保存文件的file
private File upload;
//文件名
private String uploadFileName;
public String add()throws Exception{
//1.文件保存的路径
String filePath = ServletActionContext.getServletContext().getRealPath("/files");
String dir = generateChildPath(filePath);
//2.生成带有随机性的文件名:
String fileName = TokenHelper.generateGUID()+"_"+uploadFileName;//DSK32JJKSDF2LKDSFSDFJK_uploadFileName;
//3.把user模型中缺少的属性,填充进去
user.setPath(dir);
user.setFilename(fileName);//保存的文件名必须是带有GUID的文件名。下载的时候还要用呢
System.out.println(new File(filePath+File.separator+dir,fileName));
//4.上传文件操作
upload.renameTo(new File(filePath+File.separator+dir,fileName));
//5.保存用户
int res = ius.saveUser(user);
if(res > 0){
return SUCCESS;
}
return null;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
//生成以yyyy-MM-dd的格式名称文件夹
private String generateChildPath(String filePath){
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dir = format.format(date);
File file = new File(filePath,dir);
if(!file.exists()){
file.mkdirs();
}
return dir;
}
查询所有用户
- 更改页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<HTML>
<HEAD>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${pageContext.request.contextPath}/css/Style.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="${pageContext.request.contextPath}/js/public.js"></script>
<script type="text/javascript">
function addUser(){
window.location.href = "${pageContext.request.contextPath}/user/add.jsp";
}
</script>
</HEAD>
<body>
<br>
<s:form action="findUserByCondition" namespace="/user">
<table cellSpacing="1" cellPadding="0" width="100%" align="center" bgColor="#f5fafe" border="0">
<TBODY>
<tr>
<td class="ta_01" align="center" bgColor="#afd1f3">
<strong>查 询 条 件</strong>
</td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td height="22" align="center" bgColor="#f5fafe" class="ta_01">
用户姓名
</td>
<td class="ta_01" bgColor="#ffffff">
<s:textfield name="userName" size="15" cssClass="bg"></s:textfield>
</td>
<td height="22" align="center" bgColor="#f5fafe" class="ta_01">
性别:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:select list="{'男','女'}" name="gender" headerKey="" headerValue="---请选择---"/>
</td>
</tr>
<tr>
<td height="22" align="center" bgColor="#f5fafe" class="ta_01">
学历:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:select list="{'研究生','本科','专科','高中','幼儿园'}" name="education" headerKey="" headerValue="---请选择---"/>
</td>
<td height="22" align="center" bgColor="#f5fafe" class="ta_01">
是否上传简历
</td>
<td class="ta_01" bgColor="#ffffff">
<s:select list="#{'true':'有','false':'无'}" name="isUpload" headerKey="" headerValue="---请选择---"></s:select>
</td>
</tr>
<tr>
<td width="100" height="22" align="center" bgColor="#f5fafe"
class="ta_01">
</td>
<td class="ta_01" bgColor="#ffffff">
<font face="宋体" color="red"> </font>
</td>
<td align="right" bgColor="#ffffff" class="ta_01"><br><br></td>
<td align="right" bgColor="#ffffff" class="ta_01">
<button type="submit" id="search" name="search" value="查询" class="button_view">
查询
</button>
<input type="reset" name="reset" value="重置" class="button_view"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="ta_01" align="center" bgColor="#afd1f3">
<strong>用 户 列 表</strong>
</TD>
</tr>
<tr>
<td class="ta_01" align="right">
<button type="button" id="add" name="add" value="添加" class="button_add" onclick="addUser()">添加</button>
</td>
</tr>
<tr>
<td class="ta_01" align="center" bgColor="#f5fafe">
<table cellspacing="0" cellpadding="1" rules="all" bordercolor="gray" border="1" id="DataGrid1" style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
<tr style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">
<td align="center" width="18%">登录名</td>
<td align="center" width="17%">用户姓名</td>
<td align="center" width="8%">性别</td>
<td align="center" width="23%">联系电话</td>
<td width="11%" align="center">学历</td>
<td width="7%" align="center">编辑</td>
<td width="7%" align="center">查看</td>
<td width="7%" align="center">删除</td>
</tr>
<s:iterator value="users" var="user">
<tr onmouseover="this.style.backgroundColor = 'white'" onmouseout="this.style.backgroundColor = '#F5FAFE';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="18%">
<s:property value="#user.logonName" />
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">
<s:property value="#user.userName" />
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="8%">
<s:property value="#user.gender" />
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="23%">
<s:property value="#user.telephone" />
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
<s:property value="#user.education" />
</td>
<td align="center" style="HEIGHT: 22px">
<s:a action="editUI" namespace="/user">
<s:param name="userID" value="#user.userID"></s:param>
<img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand">
</s:a>
</td>
<td align="center" style="HEIGHT: 22px">
<s:a action="findUserById" namespace="/user">
<s:param name="userID" value="#user.userID"></s:param>
<img src="${pageContext.request.contextPath}/images/button_view.gif" border="0" style="CURSOR: hand">
</s:a>
</td>
<td align="center" style="HEIGHT: 22px">
<s:a action="delete" namespace="/user">
<s:param name="userID" value="#user.userID"></s:param>
<img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
</s:a>
</td>
</tr>
</s:iterator>
</table>
</td>
</tr>
</TBODY>
</table>
</s:form>
</body>
</HTML>
- 配置struts.xml
<!-- 查询所有用户 -->
<action name="findAll" class="com.lwb.action.UserAction" method="findAll">
<result>/user/list.jsp</result>
</action>
- 写动作类
//用户查询
private List<User> users;
public String findAll(){
users = ius.findAllUser();
return SUCCESS;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
查询用户详细信息功能
- 更改页面信息
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<HTML>
<HEAD>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<LINK href="${pageContext.request.contextPath}/css/Style.css" type="text/css" rel="stylesheet">
<script language="javascript" src="${pageContext.request.contextPath}/js/public.js"></script>
</HEAD>
<body>
<form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/user/userAction_save.do" method="post" enctype="multipart/form-data">
<table cellSpacing="1" cellPadding="5" width="100%" align="center" bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
<tr>
<td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4"
height="26">
<strong><STRONG>查看用户</STRONG>
</strong>
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
登录名:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:property value="logonName" />
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">
用户姓名:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:property value="userName" />
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
性别:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:property value="sex" />
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">
学历:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:property value="education" />
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
出生日期:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:property value="birthday" />
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">
电话:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:property value="telephone" />
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
兴趣爱好:
</td>
<td class="ta_01" bgColor="#ffffff" colSpan="3">
<s:property value="interest" />
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
简历资料:
</td>
<td class="ta_01" bgColor="#ffffff" colSpan="3">
<s:url action="download" namespace="/user" var="url">
<s:param name="userID" value="userID"></s:param>
</s:url>
<a href="#" onclick="openWindow('<s:property value="#url" />','700','400')" class="cl_01">
<s:property value="filename.substring(filename.indexOf('_')+1)"/>
</a>
</td>
</tr>
<TR>
<TD class="ta_01" align="center" bgColor="#f5fafe">
备注:
</TD>
<TD class="ta_01" bgColor="#ffffff" colSpan="3">
<s:property value="remark" />
</TD>
</TR>
<TR>
<td align="center" colSpan="4" class="sep1">
<img src="${pageContext.request.contextPath}/images/shim.gif">
</td>
</TR>
<TR>
<td class="ta_01" style="WIDTH: 100%" align="right" bgColor="#f5fafe" colSpan="4">
<FONT face="宋体"> </FONT>
<INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/>
<span id="Label1"></span>
</td>
</TR>
</table>
</form>
</body>
</HTML>
- 配置struts.xml
<!-- 查看用户详细信息 -->
<action name="findUserById" class="com.lwb.action.UserAction" method="findUserById">
<result>/user/view.jsp</result>
</action>
- 写动作方法
//查询用户详细信息
public String findUserById(){
user = ius.findUserById(user.getUserID());
//将user压入ValueStack栈顶
ValueStack vs = ActionContext.getContext().getValueStack();
vs.push(user);
return SUCCESS;
}
文件下载
- 配置struts.xml
<!-- 开启静态方法调用 注意:如果要是使用静态方法的话一定要配置静态方法调用-->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<!-- 文件下载 -->
<action name="download" class="com.lwb.action.UserAction" method="download">
<result type="stream">
<!-- 给stream的结果类型注入参数:1.流的信息 2.告知浏览器以下载的方式打开 3.告知浏览器响应正文的MIME类型 -->
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${@java.net.URLEncoder@encode(oldFileName,"UTF-8")}</param>
<param name="contentType">application/octet-stream</param>
</result>
</action>
- 写动作类
//文件下载
private InputStream inputStream;
private String oldFileName;//原始文件名
public String download() throws FileNotFoundException{
//1.获取用户的信息
User dbuser = ius.findUserById(user.getUserID());
//2.文件存放的路径
String filepath = ServletActionContext.getServletContext().getRealPath("/files");
//给原始文件名赋值
oldFileName = dbuser.getFilename().substring(dbuser.getFilename().indexOf("_")+1);
//System.out.println(dbuser.getFilename());
//3.给字节输入流赋值(文件存放的路径+分隔符+文件的地址+分隔符+文件名)
inputStream = new FileInputStream(filepath+File.separator+dbuser.getPath()+File.separator+dbuser.getFilename());
//4.返回成功
return SUCCESS;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getOldFileName() {
return oldFileName;
}
public void setOldFileName(String oldFileName) {
this.oldFileName = oldFileName;
}
删除功能
- 配置struts.xml
<!-- 删除用户 -->
<action name="delete" class="com.lwb.action.UserAction" method="delete">
<!-- 删除成功之后,从新获取用户的列表,使用的重定向到一个动作 -->
<result type="redirectAction">findAll</result>
</action>
- 写动作类
//删除用户
public String delete()throws Exception{
int res = ius.removeUser(user.getUserID());
if(res > 0){
return SUCCESS;
}
return null;
}
修改功能
- 更改页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<HTML>
<HEAD>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<LINK href="${pageContext.request.contextPath}/css/Style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/public.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/check.js"></script>
<!-- 日期插件,使用jquery -->
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery-1.4.2.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/jquery/jquery.datepick.css" type="text/css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery.datepick.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery.datepick-zh-CN.js"></script>
</HEAD>
<script type="text/javascript">
$(document).ready(function(){
//使用class属性处理 'yy-mm-dd' 设置格式"yyyy/mm/dd"
$('#birthday').datepick({dateFormat: 'yy-mm-dd'});
});
</script>
<body>
<s:form action="edit" namespace="/user" enctype="multipart/form-data">
<%--修改的时候,必须要告诉服务器,修改哪个用户 --%>
<input type="hidden" name="userID" value="<s:property value="userID"/>" />
<table cellSpacing="1" cellPadding="5" width="100%" align="center" bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
<tr>
<td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4"height="26">
<STRONG>添加用户</STRONG>
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
登录名:
</td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<s:textfield name="logonName" cssClass="bg"></s:textfield>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
密码:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:password name="logonPwd" showPassword="true"></s:password>
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">
用户姓名:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:textfield name="userName" cssClass="bg"></s:textfield>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
性别:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:radio name="sex" list="{'男','女'}" value="'男'"></s:radio>
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">
学历:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:select name="education" list="{'研究生','本科','专科','高中','幼儿园'}" headerKey="" headerValue="---选择学历---"></s:select>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
出生日期:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:textfield name="birthday" size="20" readonly="readonly" id="birthday"></s:textfield>
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">
电话:
</td>
<td class="ta_01" bgColor="#ffffff">
<s:textfield name="telephone"></s:textfield>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
兴趣爱好:
</td>
<td class="ta_01" bgColor="#ffffff" colSpan="3">
<s:checkboxlist name="interest" list="{'看电影','旅游','健身','购物','睡觉'}" value="hobby.split(', ')"></s:checkboxlist>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">
简历资料:
</td>
<td class="ta_01" bgColor="#ffffff" colSpan="3">
<s:url action="download" namespace="/user" var="url">
<s:param name="userID" value="userID"></s:param>
</s:url>
<a href="#" onclick="openWindow('<s:property value="#url" />','700','400')" class="cl_01">
<s:property value="filename.substring(filename.indexOf('_')+1)"/>
</a>
<div id="mydiv" style="display:none">
<s:file name="upload" size="30"></s:file>
</div>
<input type="button" value="重新上传" onclick="showfile()" id="showbtn" />
<input type="button" value="取消上传" onclick="cancelfile()" id="cancelbtn" style="display:none"/>
<script type="text/javascript">
function showfile(){
var mydiv = document.getElementById("mydiv");
var showbtn = document.getElementById("showbtn");
var cancelbtn = document.getElementById("cancelbtn");
mydiv.style.display='inline';
showbtn.style.display = 'none';
cancelbtn.style.display = 'inline';
}
function cancelfile(){
var mydiv = document.getElementById("mydiv");
var showbtn = document.getElementById("showbtn");
var cancelbtn = document.getElementById("cancelbtn");
mydiv.style.display='none';
showbtn.style.display = 'inline';
cancelbtn.style.display = 'none';
}
</script>
</td>
</tr>
<TR>
<TD class="ta_01" align="center" bgColor="#f5fafe">
备注:
</TD>
<TD class="ta_01" bgColor="#ffffff" colSpan="3">
<s:textarea name="remark" cols="30" rows="3" cssStyle="WIDTH: 96%"></s:textarea>
</TD>
</TR>
<TR>
<td align="center" colSpan="4" class="sep1">
<img src="${pageContext.request.contextPath}/images/shim.gif">
</td>
</TR>
<tr>
<td class="ta_01" style="WIDTH: 100%" align="center"
bgColor="#f5fafe" colSpan="4">
<button type="submit" id="userAction_save_do_submit" name="submit" value="确定" class="button_ok">
确定
</button>
<FONT face="宋体"> </FONT>
<button type="reset" value="重置" class="button_cancel">重置</button>
<FONT face="宋体"> </FONT>
<INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/>
<span id="Label1"></span>
</td>
</tr>
</table>
</s:form>
</body>
</HTML>
- 配置struts.xml
<!-- 显示编辑用户页面 -->
<action name="editUI" class="com.lwb.action.UserAction" method="editUI">
<!-- 配置结果视图 -->
<result>/user/edit.jsp</result>
</action>
<!-- 编辑用户 -->
<action name="edit" class="com.lwb.action.UserAction" method="edit">
<!-- 配置结果视图,当编辑成功之后,回到显示列表 -->
<result type="redirectAction">findAll</result>
<!-- 编辑失败,回显数据 -->
<result name="input">/user/edit.jsp</result>
</action>
- 写动作类
//编辑用户
public String edit(){
//1.判断用户是否重新选择了文件
if(upload == null){
//没有选择文件。我们就用原来的
User dbUser = ius.findUserById(user.getUserID());//根据用户id把用户先查出来
//由于没有选择文件,user模型中的filename和path属性都是null,我们需要用查出来的用户里面的值替换
user.setFilename(dbUser.getFilename());
user.setPath(dbUser.getPath());
int res = ius.modifyUser(user);
if(res > 0){
return SUCCESS;
}
}else{
//用户重新选择了文件
//1.文件保存的路径
String filePath = ServletActionContext.getServletContext().getRealPath("/files");
String dir = generateChildPath(filePath);
//2.生成带有随机性的文件名:
String fileName = TokenHelper.generateGUID()+"_"+uploadFileName;//DSK32JJKSDF2LKDSFSDFJK_uploadFileName;
//3.把user模型中缺少的属性,填充进去
user.setPath(dir);
user.setFilename(fileName);//保存的文件名必须是带有GUID的文件名。下载的时候还要用呢
//4.上传文件操作
upload.renameTo(new File(filePath+File.separator+dir,fileName));
//5.保存用户
int res = ius.modifyUser(user);
if(res > 0){
return SUCCESS;
}
}
return null;
}
//显示编辑用户的动作方法
public String editUI() throws Exception{
//根据userID获取user对象
user = ius.findUserById(user.getUserID());
//把user对象压入栈顶
ValueStack vs = ActionContext.getContext().getValueStack();
vs.push(user);
return SUCCESS;
}
//删除用户
public String delete()throws Exception{
int res = ius.removeUser(user.getUserID());
if(res > 0){
return SUCCESS;
}
return null;
}
检查登陆的拦截器
- 配置struts.xml
<!-- 自定义拦截器的配置 -->
<package name="mydefault" extends="struts-default" abstract="true">
<interceptors>
<interceptor name="CheckLoginInterceptor" class="com.lwb.interceptor.CheckLoginInterceptor"></interceptor>
<interceptor-stack name="myDefaultStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="CheckLoginInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 重置默认拦截器栈 -->
<default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>
<!-- 全局结果视图 -->
<global-results>
<result name="login">/login/login.jsp</result>
</global-results>
</package>
- 写自定义拦截器
protected String doIntercept(ActionInvocation invocation) throws Exception {
//1.获取session对象
HttpSession session = ServletActionContext.getRequest().getSession();
//2.在session域中找user对象
User user = (User)session.getAttribute("user");
//3.没有 前往登录页面
if(user == null){
return "login";
}
//4.有 放行
return invocation.invoke();
}
总结
就是复习一下Struts2的基础知识和一些简单的应用。