📄 userbean.java
字号:
package com.jsfabc.jsh.view.bean;
import com.jsfabc.jsh.model.bo.Department;
import com.jsfabc.jsh.model.bo.Person;
import com.jsfabc.jsh.model.bo.Role;
import com.jsfabc.jsh.model.service.UserService;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.application.FacesMessage;
import com.jsfabc.jsh.model.exception.UserNotExistException;
import com.jsfabc.jsh.model.exception.DbException;
import com.jsfabc.jsh.utils.MessageFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class UserBean {
//用于登录的用户名
private String userName;
//用于登录的密码
private String password;
//用户真实姓名
private String personName;
//用户所属部门
private Department department;
//用户电子邮件
private String email;
//用户电话
private String telephone;
//用户的权限
private List permissions;
//所有部门信息
private List departmentList;
//用户列表
private List userList;
//角色列表
private List roleList;
//用于分配用户角色
//选择的部门Id
private Integer departmentId;
private String personId;
//用户服务
private UserService userService;
//构造函数
public UserBean() {
}
/////登录
// 用户名的取得与设置
public String getUserName() { return userName; }
public void setUserName(String newValue) {
this.userName = newValue;
}
// 密码的取得与设置
public String getPassword() { return password; }
public void setPassword(String newValue) {
this.password = newValue;
}
// 用户真实姓名的取得与设置
public String getPersonName() { return personName; }
public void setPersonName(String newValue) {
this.personName = newValue;
}
public Department getDepartment() {
return this.department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getEmail() {
return this.email;
}
public void setEmail(String newValue) {
this.email = newValue;
}
public String getTelephone() {
return this.telephone;
}
public void setTelephone(String newValue) {
this.telephone = newValue;
}
public Integer getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Integer newValue){
this.departmentId = newValue;
}
public String getPersonId(){
return this.personId;
}
public void setPersonId(String newValue){
this.personId=newValue;
}
//常用列表
//所有部门的取得与设置
public List getDepartmentList() {
return departmentList;
}
public void setDepartmentList(List newValue){
this.departmentList=newValue;
}
//权限的取得与设置
public List getPermissions() {
return permissions;
}
public void setPermissions(List newValue){
this.permissions=newValue;
}
//用户的取得与设置
public List getUserList() {
return userList;
}
public void setUserList(List newValue){
this.userList=newValue;
}
//权限的取得与设置
public List getRoleList() {
return roleList;
}
public void setRoleList(List newValue){
this.roleList=newValue;
}
//创建日志对象
Log log=LogFactory.getLog(this.getClass());
//依赖注入
public void setUserService(UserService newValue){
this.userService=newValue;
}
//取得下拉列表的标签名
public String getDepName(Integer id) {
if (id != null && this.departmentList != null) {
Iterator ite = this.departmentList.iterator();
while(ite.hasNext()) {
SelectItem i = (SelectItem)ite.next();
if (id.equals(i.getValue())) {
return i.getLabel();
}
}
}
return null;
}
//用户登录动作
public String loginAction(){
FacesContext facesContext= FacesContext.getCurrentInstance();
Person person = null;
//调用业务对象的登录方法
try{
person = userService.login(userName, password);
if(person!=null){
//在此可以做一些想做的事情。
this.departmentList = new ArrayList();
for (int i=0; i<person.getDepartments().size(); i++) {
Department department=
(Department)person.getDepartments().get(i);
this.departmentList.add
(new SelectItem(department.getDepartmentId()
.toString(),department.getDepartmentName()));
}
this.permissions=person.getPermissions();
//保存一个登录用户的基本信息到会话范围
HttpServletRequest request = (HttpServletRequest)
facesContext.getExternalContext().getRequest();
HttpSession session = request.getSession();
session.setAttribute("userId", person.getPersonId());
session.setAttribute("userName",person.getPersonName());
//session.setAttribute("password", person.getPassword());
//session.setAttribute("email",person.getEmail());
this.userName=person.getPersonId();
this.personName=person.getPersonName();
this.email=person.getEmail();
this.password=person.getPassword();
//在日志中记录成功登录的用户
log.info(userName + ": login success!");
//用户成功登录后,返回loginAction方法的结果,
//该结果是一个字符串,用于与导航规则比较,
//导航到登录成功后的页面
return "success";
}
else{
//声明一个没有登录成功的消息
FacesMessage msg=MessageFactory.getMessage
(facesContext,"error_password","");
//为了与原来例子保持一致,这里设置了消息的严重等级
msg.setSeverity(FacesMessage.SEVERITY_WARN);
//将消息保存在facesContext中,供其他过程使用
facesContext.addMessage(null,msg);
//返回loginAction方法的结果,
//该结果是一个字符串,用于与导航规则比较,
//导航到登录失败后的页面
return "failure";
}
}
catch(UserNotExistException ue){
//捕获UserNotExistException异常,其余参见上面else处的注释
FacesMessage msg=MessageFactory.getMessage(facesContext,
ue.getMessage(),new Object[]{ue.getUsername()});
msg.setSeverity(FacesMessage.SEVERITY_WARN);
facesContext.addMessage(null,msg);
//在日志中记录尝试登录的用户ID
log.warn(userName + ": want log in!");
return "failure";
}
catch(DbException de){
//捕获DbException异常,其余参见上面else处的注释
FacesMessage msg=MessageFactory.getMessage
(facesContext,"error_database_error","");
msg.setSeverity(FacesMessage.SEVERITY_WARN);
facesContext.addMessage(null,msg);
return "failure";
}
catch(Exception e){
//捕获其他异常,其余参见上面else处的注释,
FacesMessage msg=MessageFactory.getMessage
(facesContext,"error_unexpected","");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
facesContext.addMessage(null,msg);
return "error";
}
}
//用于在分配用户角色时用户选择一个部门后,列出该部门的人员列表
//列出角色列表
public void departmentChanged(ValueChangeEvent event) {
//取得faces上下文
FacesContext facesContext= FacesContext.getCurrentInstance();
try{
//获得选取的部门
Department selectedDep=new Department();
Integer departmentId=(Integer)event.getNewValue();
String departmentName=getDepName(departmentId);
selectedDep.setDepartmentId(departmentId);
selectedDep.setDepartmentName(departmentName);
//准备一个用户id和姓名的选择项目列表,
//为在诸如下拉列表之类的UI中显示它们做准备
this.userList=new ArrayList();
for (int i=0;i<userService.depUsers(selectedDep).size();i++){
Person person=(Person)userService
.depUsers(selectedDep).get(i);
this.userList.add(new SelectItem(person.getPersonId(),
person.getPersonName()));
}
//准备一个角色id和角色名称的选择项目列表,
//为在诸如下拉列表之类的UI中显示它们做准备
this.roleList=new ArrayList();
for (int i=0; i<userService.Roles().size(); i++) {
Role role=(Role)userService.Roles().get(i);
this.roleList.add(new SelectItem(role.getRoleId(),
role.getRoleName()));
}
}
catch(DbException de){
//捕获DbException异常
FacesMessage msg=MessageFactory.getMessage
(facesContext,"error_database_error","");
msg.setSeverity(FacesMessage.SEVERITY_WARN);
facesContext.addMessage(null,msg);
}
catch(Exception e){
//捕获其他异常
FacesMessage msg=MessageFactory.getMessage
(facesContext,"error_unexpected","");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
facesContext.addMessage(null,msg);
}
}
//处理用户安全退出系统
public String logoutAction(){
//取得faces上下文
FacesContext facesContext= FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)
facesContext.getExternalContext().getRequest();
//使会话失效
request.getSession().invalidate();
return "success";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -