l 登录
l 条件查询员工
l 查看员工明细
l 下载简历
l 添加员工(上传简历)
l 修改员工
l 删除员工
导包
l struts2
l c3p0
l mysql
l dbutils
l beanutils
建包
l cn.itcast.user.domain
l cn.itcast.user.dao
l cn.itcast.user.service
l cn.itcast.user.web.action
主页
让主页转发到/login/login.jsp
CREATE DATABASE empmng;
#创建用户,指定为只能本地访问,不能远程访问
create user empmng@localhost identified by '123';
#授权
grant all on empmng.* to empmng@localhost;
CREATE TABLE S_User(
uid CHAR(32) PRIMARY KEY,
username VARCHAR(50),
loginname VARCHAR(50),
loginpass VARCHAR(50),
gender VARCHAR(10),
birthday VARCHAR(50),
education VARCHAR(20),
cellphone VARCHAR(50),
hobby VARCHAR(20),
filepath VARCHAR(500),
filename VARCHAR(100),
remark VARCHAR(500)
);
com.mysql.jdbc.Driver
jdbc:mysql:///struts2-javaee16
root
1234
3
30
5
3
对应t_user表来创建User类。
package cn.itcast.user.domain;
import java.io.File;
public class User {
private String uid;
private String username;
private String loginname;
private String loginpass;
private String gender;
private String birthday;
private String education;
private String cellphone;
private String hobby;
private String filepath;
private String filename;
private String remark;
//upload 字段在数据库中并没有列与之对应
// 接受表单提交的是否有简历的参数
private String upload;
public String getUpload() {
return upload;
}
public void setUpload(String upload) {
this.upload = upload;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getLoginpass() {
return loginpass;
}
public void setLoginpass(String loginpass) {
this.loginpass = loginpass;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
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 getCellphone() {
return cellphone;
}
public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
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;
}
@Override
public String toString() {
return "User [uid=" + uid + ", username=" + username + ", loginname="
+ loginname + ", loginpass=" + loginpass + ", gender=" + gender
+ ", birthday=" + birthday + ", education=" + education
+ ", cellphone=" + cellphone + ", hobby=" + hobby
+ ", filepath=" + filepath + ", filename=" + filename
+ ", remark=" + remark + "]";
}
}
package cn.itcast.user.dao;
import java.util.List;
import cn.itcast.user.domain.User;
public interface UserDao {
//根据登录名称获得用户对象
User getUserByLoginName(String loginName);
// 根据查询条件获得用户列表
List getUserByCondition(User u);
//保存用户对象
void saveUser(User u);
//根据userid 获得user对象
User getUserByUid(String id);
}
package cn.itcast.user.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import cn.itcast.user.domain.User;
public class UserDaoImpl implements UserDao {
private static QueryRunner qr = new QueryRunner(new ComboPooledDataSource());
public User getUserByLoginName(String loginName) {
//1 书写sql语句
String sql = " select * from tab_user where loginname = ? ";
//2 调用runner的 query方法
try {
User u = qr.query(sql, new BeanHandler(User.class), loginName);
//3 返回
return u;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("查询用户失败!");
}
}
public List getUserByCondition(User u) {
String sql = " select * from tab_user where 1=1";
//准备放置参数的集合
List
UserAction:
package cn.itcast.user.web.action;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import cn.itcast.user.dao.UserDao;
import cn.itcast.user.dao.UserDaoImpl;
import cn.itcast.user.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
import com.sun.xml.internal.ws.util.ReadOnlyPropertyException;
public class UserAction extends ActionSupport implements ModelDriven {
private UserDao ud = new UserDaoImpl();
private User u = new User();
//处理用户登录请求
public String login(){
//1 校验
//2 根据登录名调用Dao,获得用户对象
User user = ud.getUserByLoginName(u.getLoginname());
//没有获得=> 添加错误消息,提示用户名不存在
if(user == null){
addActionError("用户名不存在!");
return "login";
}
//3 比对密码
if(!user.getLoginpass().equals(u.getLoginpass())){
//比对失败=>添加错误,提示密码不正确
addActionError("密码不正确!");
return "login";
}
//4 将User对象放入session域作为登录标识
Map sessionScope = ActionContext.getContext().getSession();
sessionScope.put("user", user);
//5 重定向到成功页面
return "home";
}
//-----------------------------------------------------------------------------------------
//查询用户列表
public String list(){
// 1 调用Dao的查询列表方法
List list = ud.getUserByCondition(u);
//2 将列表放入request域
Map requestScope = (Map) ActionContext.getContext()
.get("request");
requestScope.put("list", list);
//3 转发到list页面
return "list";
}
//---------------------------------------------------------------
//查看用户详情
public String view(){
//1 查询用户根据id
User user = ud.getUserByUid(u.getUid());
//2 将user对象放入值栈栈顶
ValueStack vs = ActionContext.getContext().getValueStack();
vs.push(user);
//3转发到查看页面
return "view";
/*//2放入request域
//3转发
*/
}
//------------------------------------------------------------------------
private String filePath;
private String fileName ;
public String download(){
//1 根据id查找用户
User user = ud.getUserByUid(u.getUid());
//2 拿到用户的文件路径,文件名称
filePath = user.getFilepath();
fileName = user.getFilename();
//3 返回结果
return "download";
}
//filePath => /upload/23b798f5-0fc9-4add-9af9-fe8581a458ae
public InputStream getDoc(){
//1 获得servletcontext对象
ServletContext sc = ServletActionContext.getServletContext();
//2根据路径获得流
return sc.getResourceAsStream(filePath);
}
public String getFileName(){
try {
return URLEncoder.encode(fileName, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new RuntimeException("不可楞!");
}
}
//----------------------------------------------------------------------------------------
private File upload2;
private String upload2FileName;
public String getUpload2FileName() {
return upload2FileName;
}
public void setUpload2FileName(String upload2FileName) {
this.upload2FileName = upload2FileName;
}
public File getUpload2() {
return upload2;
}
public void setUpload2(File upload2) {
this.upload2 = upload2;
}
public String add(){
String uuid = UUID.randomUUID().toString();
if(upload2 != null){
// 1 上传的文件转存
//1> 找到upload文件夹 绝对路径
String dirPath = ServletActionContext.getServletContext().getRealPath("/upload");
//2>生成 文件名称(uuid)
//3> 转存
File targetFile = new File(dirPath+"/"+uuid);
upload2.renameTo(targetFile);
// 2 将转存的路径 以及 文件的原始名称封装到User对象
u.setFilepath("/upload/"+uuid);
u.setFilename(upload2FileName);
}
// 3 使用随机字符串设置user的id
u.setUid(uuid);
// 4 调用dao保存user
ud.saveUser(u);
// 5 重定向到用户列表
return "rlist";
}
//-----------------------------------------------------------------------------------------
//专门校验login的
public void validateLogin(){
//数据校验
if(u.getLoginname() == null || u.getLoginname().trim().equals("")){
addFieldError("loginname", "用户名不能为空!");
}
if(u.getLoginpass() == null || u.getLoginpass().trim().equals("")){
addFieldError("loginpass", "密码不能为空!");
}
}
public User getModel() {
return u;
}
}
package cn.itcast.user.web.interceptor;
import java.util.Map;
import cn.itcast.user.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class LoginInterceptor extends MethodFilterInterceptor {
@Override
//凡是进入拦截器,都需要有登录状态才能放行
protected String doIntercept(ActionInvocation invocation) throws Exception {
//1 获得session
Map sessionScope = ActionContext.getContext().getSession();
//2 从session找出User对象
User u = (User) sessionScope.get("user");
//3 判断User对象是否存在
if(u==null){
//不存在=>没有登录=>转发登录页面
return "login";
}
//存在=>登录了=>放行
return invocation.invoke();
}
}
login
/user/error1.jsp
/login/login.jsp
/login/home.jsp
/user/list.jsp
/user/view.jsp
UserAction_list
/
application/msword
doc
attachment;filename="${fileName}"
1024
/login/login.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
请登录
用户名:
密码:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
用户名:
超级管理员
添加用户
登录名:
密码:
用户姓名:
性别:
学历:
出生日期:
电话:
兴趣爱好:
简历资料:
备注:
上传附件错误
<%@ page language="java" contentType="text/html; charset=UTF-8"%>