一、概述
这是基于Struts2框架写的一个员工管理系统,可以进行员工的增删改查操作,还有登录功能。
二、实现
话不多说,先来几个运行界面,下面上代码:
用户登录:
注册,也就是新增员工信息,这里写的直接新增员工信息后,员工可以直接登录,你可以改成单独分一个管理人员登录表:
登录成功之后,员工信息列表:
然后进行修改操作,修改页面和注册页面一样,但是会根据你要修改的id自动填充信息,删除就是直接删除。
上代码:
1、目录结构:
2、LoginAction.java
package com.hnpi.action;
import java.util.List;
import java.util.Map;
import com.hnpi.bean.User;
import com.hnpi.service.UserService;
import com.hnpi.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String login() {
UserService userService = new UserServiceImpl();
List<User> users = userService.selectAllUser();
if (username != null && !"".equals(username) && password != null && !"".equals(password)) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getUsername().equals(username) && users.get(i).getPassword().equals(password)) {
// 登录成功 保留登录信息
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("user", username);
return "success";
}
}
return "fail";
} else {
return "fail";
}
}
public String register() {
User user = new User(0, username, password, realname, sex, birthday, nation, education, post, phone, address,
email);
UserService userService = new UserServiceImpl();
if (userService.addUser(user)) {
return "success";
} else
return "fail";
}
private int id;
private String username;
private String password;
private String realname;
private String sex;
private String birthday;
private String nation;
private String education;
private String post;
private String phone;
private String address;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
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 getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
3、UserAction.java
package com.hnpi.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.hnpi.bean.User;
import com.hnpi.service.UserService;
import com.hnpi.service.impl.UserServiceImpl;
public class UserAction {
/**
* 员工信息列表
*
* @return
*/
public String userList() {
// 将获取的数据传递至userlist.jsp页面
UserService userService = new UserServiceImpl();
List<User> users = userService.selectAllUser();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("users", users);
return "success";
}
/**
* 根据id查询图书 直接跳转到准备更新图书页面
*
* @return
*/
public String selectUserById() {
UserService userService = new UserServiceImpl();
User user = userService.selectUserById(id);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("userById", user);
return "success";
}
/**
* 更新员工信息
*
* @return
*/
public String updateUser() {
User user = new User(id, username, password, realname, sex, birthday, nation, education, post, phone, address,
email);
UserService userService = new UserServiceImpl();
if (userService.updateUser(user)) {
return "success";
} else
return "fail";
}
/**
* 删除员工信息
*
* @param id
* @return
*/
public String deleteById() {
UserService userService = new UserServiceImpl();
if (userService.delUser(id)) {
return "success";
} else
return "fail";
}
private int id;
private String username;
private String password;
private String realname;
private String sex;
private String birthday;
private String nation;
private String education;
private String post;
private String phone;
private String address;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
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 getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
4、User.java
package com.hnpi.bean;
public class User {
private int id;
private String username;
private String password;
private String realname;
private String sex;
private String birthday;
private String nation;
private String education;
private String post;
private String phone;
private String address;
private String email;
public int getId