在前面一步一步做项目(4)配置SSH的基础上继续。
系统采用用户登录信息和用户信息分离的思路,这里建立Users.java类,主要用于管理用户进行登录的信息(当然,其思想可用于其他信息的管理),因此,只包含几个基本属性,如下所示:
package cn.lut.curiezhang.model;
/**
* SSH框架进行用户管理的持久层的POJO类
* @author curiezhang
*
*/
public class Users {
// 用户id
private String userId;
// 用户名
private String userName;
// 用户密码
private String userPassword;
// 用户联系电话
private String userPhone;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserPhone() {
return userPhone;
}
public void setUserPhone(String userPhone) {
this.userPhone = userPhone;
}
}
在Users.java所在的目录下,创建Hibernate映射文件Users.hbm.xml,来实现实体类到数据库表的映射,如下所示:
<hibernate-mapping>
<class name="cn.lut.curiezhang.model.Users" table="USERS">
<id name="userId">
<column name="USER_ID" length="32">
<comment>用户idcomment>
column>
<generator class="assigned"/>
id>
<property name="userName">
<column name="USER_NAME" not-null="true" length="50">
<comment>用户名comment>
column>
property>
<property name="userPassword">
<column name="USER_PASSWORD" not-null="true" length="128">
<comment>用户密码comment>
column>
property>
<property name="userPhone">
<column name="USER_PHONE" length="20">
<comment>用户手机comment>
column>
property>
class>
hibernate-mapping>
建立UserDao.java类,如下所示:
package cn.lut.curiezhang.dao;
import java.util.Collection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import cn.lut.curiezhang.model.Users;
import cn.lut.curiezhang.util.SecurityFunctions;
/**
* SSH框架进行用户管理的持久层的DAO类
* @author curiezhang
*
*/
public class UserDao extends HibernateDaoSupport {
private static final Logger log = LogManager.getLogger(UserDao.class);
/**
* Dao中保存用户信息
* @param user
*/
public void save(Users user) {
log.debug("Dao > 存储用户信息,id为{}", user.getUserId());
this.getHibernateTemplate().save(user);
}
/**
* Dao中查询所有用户
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public Collection<Users> getAll() {
log.debug("Dao 》 查询所有用户");
Collection<Users> list;
list = (Collection<Users>) this.getHibernateTemplate().find("from Users");
return list;
}
/**
* Dao中检查用户是否有效
* @param username
* @param password
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public Users checkUser(String username, String password) {
log.debug("Dao > 检查用户是否存在,用户名为{}", username);
Collection<Users> list;
String password1 = SecurityFunctions.sha3(password, 512);
String hql = "from Users where userName='" + username + "' and userPassword='" + password1 + "'";
list = (Collection<Users>) this.getHibernateTemplate().find(hql);
if(list == null || list.isEmpty())
return null;
Users user = list.iterator().next();
if (username.equals(user.getUserName()) && SecurityFunctions.sha3(password, 512).equals(user.getUserPassword())) {
log.debug("Dao > 检查结果为用户存在,id为{}", user.getUserId());
return user;
}
return null;
}
/**
* Dao中删除用户
* @param userId
*/
public void delete(String userId) {
log.debug("Dao > 删除指定用户,id为{}", userId);
Object model = this.getHibernateTemplate().get(Users.class, userId);
this.getHibernateTemplate().delete(model);
}
/**
* Dao中查询指定id的用户
* @param userId
*/
public Users getUserId(String userId) {
log.debug("Dao > 查询指定id的用户,id为{}", userId);
return this.getHibernateTemplate().get(Users.class, userId);
}
/**
* Dao中修改用户
* @param user
*/
public void update(Users user) {
log.debug("Dao > 更新用户信息,id为{}", user.getUserId());
this.getHibernateTemplate().update(user);
}
/**
* Dao中根据id查询用户信息
* @param userId
*/
public Users getUserById(String userId) {
log.debug("DAO > 根据id查询用户信息,id为 {}", userId);
return this.getHibernateTemplate().get(Users.class, userId);
}
}
这里用到了SecurityFunctions类,主要是对密码进行加密,代码如下:
package cn.lut.curiezhang.util;
import java.io.UnsupportedEncodingException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bouncycastle.jcajce.provider.digest.SHA3;
import org.bouncycastle.util.encoders.Hex;
public class SecurityFunctions {
private static final Logger log = LogManager.getLogger(SecurityFunctions.class);
/**
* 在 {@code input} 串上使用 SHA-3 哈希函数
* @param input 应用 SHA-3 哈希函数的串
* @param bitLength 应用 SHA-3 哈希函数的位数,如256、384、512等
* @return input 的哈希函数串
*/
public static String sha3(String input, int bitLength){
String hash="";
try {
SHA3.DigestSHA3 md=new SHA3.DigestSHA3(bitLength);
md.update(input.getBytes("UTF-8"));
hash = Hex.toHexString(md.digest());
}
catch (UnsupportedEncodingException e) {
log.error("cmis: SHA-3 错误 ",e);
}
return hash;
}
public static void main(String[] args) {
System.out.println(sha3("111", 512));
}
}
建立UserService.java类,如下所示:
package cn.lut.curiezhang.service;
import java.util.Collection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import cn.lut.curiezhang.dao.UserDao;
import cn.lut.curiezhang.model.Users;
/**
* SSH框架进行用户管理的业务层的Service类
* @author curiezhang
*
*/
@Transactional
public class UserService {
private static final Logger log = LogManager.getLogger(UserService.class);
/**
* Service业务层注入DAO类
*/
private UserDao userDao;
public void setUserDao(UserDao userDao) {
log.debug("Service > 注入DAO类");
this.userDao = userDao;
}
/**
* Service业务层保存用户
* @param user
*/
public void save(Users user) {
log.debug("Service > 存储用户信息,id为 {}", user.getUserId());
userDao.save(user);
}
/**
* Service业务层查询所有用户
* @return list
*/
private Collection<Users> list;
public Collection<Users> getAll() {
log.debug("Service > 查询所有用户");
list = userDao.getAll();
return list;
}
/**
* Service业务层删除指定id的用户
* @param user
*/
public void delete(String userId) {
log.debug("Service > 删除指定id的用户,id为 {}", userId);
userDao.delete(userId);
}
/**
* Service业务层根据id查询用户信息
* @param userId
*/
public Users getUserId(String userId) {
log.debug("Service > 根据id查询用户信息,id为 {}", userId);
return userDao.getUserId(userId);
}
/**
* Service业务层修改用户信息
* @param user
*/
public void update(Users user) {
log.debug("Service > 修改用户信息,id为 {}", user.getUserId());
userDao.update(user);
}
/**
* Service业务层根据用户名和密码检查用户是否存在
* @param username
* @param password
*/
public Users checkUser(String username, String password) {
log.debug("Service > 根据用户名和密码检查用户信息,用户名为 {}", username);
return userDao.checkUser(username, password);
}
/**
* Service业务层根据id查询用户
* @param userId
*/
public Users getUserById(String userId) {
log.debug("Service > 根据id查询用户信息,id为 {}", userId);
return userDao.getUserById(userId);
}
}
建立UserAction.java,如下所示:
package cn.lut.curiezhang.action;
import java.util.ArrayList;
import java.util.Collection;
import java.util.ResourceBundle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.lut.curiezhang.model.Users;
import cn.lut.curiezhang.service.UserService;
import cn.lut.curiezhang.util.SecurityFunctions;
/**
* SSH框架进行用户管理的视图层的Action类
* @author curiezhang
*
*/
public class UserAction extends ActionSupport implements ModelDriven<Object> {
/**
* UserAction的ID
*/
private static final long serialVersionUID = -2978659526717330712L;
private static final Logger log = LogManager.getLogger(UserAction.class);
/**
* Struts和Spring整合过程中按名称自动注入的业务层的类
*/
private UserService userService;
public void setUserService(UserService userService) {
log.debug("Action > 注入的Service类");
this.userService = userService;
}
/**
* Action中模型驱动使用的类
*/
private Users model = new Users();
public void setModel(Users model) {
log.debug("Action > 设置模型值,id为{}", userId);
this.model = model;
}
@Override
public Object getModel() {
if (list != null) {
if(list.size() != 0)
log.debug("Action > 得到列表第一个元素id值,id为{}", ((Users) list.iterator().next()).getUserId());
return list;
} else {
log.debug("Action > 得到模型值,id为{}", userId);
return model;
}
}
/**
* Action中搜索唯一id使用的参数
*/
private String userId;
public String getUserId() {
log.debug("Action > 得到用户id,id为{}", userId);
return userId;
}
public void setUserId(String userId) {
log.debug("Action > 设置用户id,id为{}", userId);
if(userId != null) {
this.model = userService.getUserId(userId);
}
this.userId = userId;
}
/**
* Action中搜索所有数据时的返回结果
* @return list
*/
private Collection<Users> list;
public Collection<Users> getList() {
log.debug("Action > 查询所有用户");
return list;
}
/**
* Action中填写表单时执行该方法,可进行有效性验证
* @return true 不能通过有效性验证
*/
public void validate() {
log.debug("Action > 有效性验证,id为{}", userId);
}
/**
* Action中首页
*/
public String index() {
log.debug("Action > 访问index页面");
list = userService.getAll();
return SUCCESS;
}
/**
* Action中添加
*/
public String add() {
log.debug("Action > 访问add页面");
model = new Users();
return "success";
}
/**
* Action中确认删除
*/
public String deleteConfirm() {
log.debug("Action > 访问deleteConfirm页面");
return "success";
}
/**
* Action中修改
*/
public String modify() {
log.debug("Action > 访问modify页面");
return "success";
}
/**
* Action中查询
*/
public String browse() {
log.debug("Action > 访问browse页面");
return "success";
}
/**
* Action中登录
*/
public String loginAdmin() {
log.debug("Action > 访问管理员登录页面");
return "loginAdmin";
}
/**
* Action中创建新的用户
* @return
*/
public String create() {
log.debug("Action > 添加新用户,id为 {}", userId);
Collection<String> names = new ArrayList<String>();
Collection<String> ids = new ArrayList<String>();
list = userService.getAll();
for(Users user : list) {
names.add(user.getUserName());
ids.add(user.getUserId());
}
if(names.contains(model.getUserName()) || ids.contains(model.getUserId())){
String info = ResourceBundle.getBundle("Messages").getString("Users.result.createError");
addActionMessage(info);
return "error";
} else {
model.setUserPassword(SecurityFunctions.sha3(model.getUserPassword(), 512));
userService.save(model);
String info = ResourceBundle.getBundle("Messages").getString("Users.result.create");
addActionMessage(info);
return "success";
}
}
/**
* Action中删除指定id的用户
* @return
*/
public String delete() {
log.debug("Action > 删除指定id的用户,id为 {}", userId);
if(userId == null) {
String info = ResourceBundle.getBundle("Messages").getString("Users.result.deleteError");
addActionMessage(info);
return "error";
}
Collection<String> names = new ArrayList<String>();
list = userService.getAll();
for(Users user : list) {
names.add(user.getUserId());
}
if(names.contains(userId)){
userService.delete(userId);
String info = ResourceBundle.getBundle("Messages").getString("Users.result.delete");
addActionMessage(info);
return "success";
} else {
String info = ResourceBundle.getBundle("Messages").getString("Users.result.deleteError");
addActionMessage(info);
return "error";
}
}
/**
* Action中修改用户信息
* @return
*/
public String update() {
log.debug("Action > 修改用户信息,id为 {}", userId);
String newUserName = model.getUserName();
String userId = model.getUserId();
Collection<String> names = new ArrayList<String>();
list = userService.getAll();
for(Users user : list) {
if(user.getUserId().equals(userId) == false)
names.add(user.getUserName());
}
if(names.contains(newUserName)){
String info = ResourceBundle.getBundle("Messages").getString("Users.result.updateError");
addActionMessage(info);
return "error";
} else {
model.setUserPassword(SecurityFunctions.sha3(this.getUserPassword2(), 512));
userService.update(model);
String info = ResourceBundle.getBundle("Messages").getString("Users.result.update");
addActionMessage(info);
return "success";
}
}
/**
* Action中再次输入密码使用
*/
private String userPassword2;
public String getUserPassword2() {
return userPassword2;
}
public void setUserPassword2(String userPassword2) {
this.userPassword2 = userPassword2;
}
/**
* Action中修改用户密码
* @return
*/
public String updatePassword() {
log.debug("Action > 修改用户信息,id为 {}", userId);
String userId = model.getUserId();
model = userService.getUserById(userId);
log.debug("Action > 修改用户信息,id为 {},{},{}", userId, model.getUserName(), model.getUserPhone());
model.setUserPassword(SecurityFunctions.sha3(this.getUserPassword2(), 512));
userService.update(model);
String info = ResourceBundle.getBundle("Messages").getString("Users.result.updatePassword");
addActionMessage(info);
return "success";
}
}
针对Action建立对应的验证配置文件,这里建立UserAction-validation.xml,如下所示:
<validators>
<field name="userId">
<field-validator type="requiredstring">
<param name="trim">trueparam>
<message>必须输入用户编号message>
field-validator>
<field-validator type="stringlength">
<param name="minLength">3param>
<param name="maxLength">32param>
<message>用户编号长度应在3到32个字符间message>
field-validator>
field>
<field name="userName">
<field-validator type="requiredstring">
<param name="trim">trueparam>
<message>必须输入用户名message>
field-validator>
<field-validator type="stringlength">
<param name="minLength">2param>
<param name="maxLength">50param>
<message>用户名长度应在2到50个字符间message>
field-validator>
field>
<field name="userPassword">
<field-validator type="requiredstring">
<message>必须输入用户密码message>
field-validator>
<field-validator type="stringlength">
<param name="minLength">6param>
<param name="maxLength">128param>
<message>为了您账号的安全,请设置6个以上的密码message>
field-validator>
field>
<field name="userPassword2">
<field-validator type="requiredstring">
<message>必须再次输入用户密码message>
field-validator>
<field-validator type="fieldexpression">
<param name="expression">param>
<message>两次密码不相等message>
field-validator>
field>
validators>
系统中用到的文本串、设置信息都存放在Messages.properties中,代码如下:
#################### cmis.application ######################
Application.name=cmis
#################### /cmis.application ######################
#################### cmis.navigation ######################
Navigation.application.homepage=cmis
Navigation.admin.homepage=\u540E\u53F0\u7BA1\u7406
Navigation.help=\u5E2E\u52A9
#################### /cmis.navigation ######################
#################### cmis.footer ######################
Footer.copyright=2019 © \u5F20\u805A\u793C
#################### /cmis.footer ######################
#################### cmis.DataTables ######################
DataTables.title.operate=\u64CD\u4F5C
DataTables.title.browse=\u6D4F\u89C8
DataTables.title.modify=\u4FEE\u6539
DataTables.title.delete=\u5220\u9664
#################### /cmis.DataTables ######################
#################### cmis.button ######################
Button.help.close=\u5173\u95ED
Button.delete=\u5220\u9664
Button.cancel=\u53D6\u6D88
#################### /cmis.button ######################
#################### cmis.admin.users ######################
Users.deleteConfirm=\u7528\u6237
Users.deleteConfirm.prompt=\u60A8\u771F\u7684\u8981\u5220\u9664\u201C{0}\u201D\uFF1A\u201C{1}\u201D\u5417\uFF1F
# navigation
Navigation.admin.users.homepage=\u7528\u6237\u7BA1\u7406
# page title
Users.page.title=\u7BA1\u7406\u7528\u6237\u4FE1\u606F
# title
Users.title.index=\u7528\u6237\u5217\u8868
Users.title.add=\u6DFB\u52A0\u65B0\u7528\u6237
Users.title.browse=\u6D4F\u89C8\u7528\u6237\u8BE6\u60C5
Users.title.deleteConfirm=\u786E\u8BA4\u5220\u9664\u8BE5\u7528\u6237
Users.title.modify=\u4FEE\u6539\u7528\u6237\u4FE1\u606F
Users.title.modifyPassword=\u4FEE\u6539\u7528\u6237\u5BC6\u7801
# button title
Users.deleteConfirm.user=\u7528\u6237
Users.button.home=\u8FD4\u56DE\u7528\u6237\u7BA1\u7406\u9996\u9875
Users.button.add=\u6DFB\u52A0\u65B0\u7528\u6237
Users.button.save=\u4FDD\u5B58\u65B0\u7528\u6237\u4FE1\u606F
Users.button.modify=\u4FDD\u5B58\u4FEE\u6539\u7684\u7528\u6237\u4FE1\u606F
Users.button.modifyPassword=\u4FEE\u6539\u7528\u6237\u5BC6\u7801
Users.button.reset=\u6E05\u7A7A\u7528\u6237\u4FE1\u606F
# Users help
Users.help.indexTitle=\u7BA1\u7406\u7528\u6237\u4FE1\u606F\u2014\u5E2E\u52A9
Users.help.addTitle=\u6DFB\u52A0\u65B0\u7528\u6237\u2014\u5E2E\u52A9
Users.help.browseTitle=\u6D4F\u89C8\u7528\u6237\u8BE6\u60C5\u2014\u5E2E\u52A9
Users.help.modifyTitle=\u4FEE\u6539\u7528\u6237\u4FE1\u606F\u2014\u5E2E\u52A9
Users.help.deleteConfirmTitle=\u786E\u8BA4\u5220\u9664\u8BE5\u7528\u6237\u2014\u5E2E\u52A9
Users.help.modifyPasswordTitle=\u4FEE\u6539\u7528\u6237\u5BC6\u7801\u2014\u5E2E\u52A9
Users.help.index=\u5217\u51FA\u6240\u6709\u7684\u7528\u6237\uFF0C\u53EF\u4EE5\u5BF9\u8FDB\u884C\u76F8\u5E94\u7684\u7BA1\u7406\u64CD\u4F5C\u3002
Users.help.add=\u6DFB\u52A0\u65B0\u7684\u7528\u6237
Users.help.browse=\u6D4F\u89C8\u7528\u6237\u8BE6\u60C5\uFF0C\u67E5\u770B\u7279\u5B9A\u7684\u7528\u6237\u7684\u8BE6\u7EC6\u4FE1\u606F\u3002
Users.help.modify=\u4FEE\u6539\u7528\u6237\u4FE1\u606F
Users.help.deleteConfirm=\u786E\u8BA4\u5220\u9664\u8BE5\u7528\u6237\uFF0C\u4E00\u65E6\u5220\u9664\uFF0C\u4E0D\u80FD\u6062\u590D\uFF01
Users.help.modifyPassword=\u4FEE\u6539\u7528\u6237\u5BC6\u7801\uFF0C\u9700\u8981\u8F93\u5165\u539F\u5BC6\u7801\uFF0C\u7136\u540E\u8F93\u5165\u65B0\u5BC6\u7801\u4E24\u6B21\uFF01
# Users field error
Users.fieldError.userId=\u7528\u6237\u7F16\u53F7\u4E0D\u80FD\u4E3A\u7A7A\u6216\u91CD\u590D
Users.fieldError.userName=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A\u6216\u91CD\u590D
Users.fieldError.userPassword=\u7528\u6237\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A
Users.fieldError.userPasswordConfirm=\u8BF7\u518D\u6B21\u8F93\u5165\u7528\u6237\u5BC6\u7801
Users.fieldError.status=\u8BF7\u9009\u62E9\u7528\u6237\u72B6\u578B\uFF0C\u9ED8\u8BA4\u4E3A\u8003\u751F\u7528\u6237\uFF0C\u53EF\u7528
# Users field prompt
Users.fieldPrompt.userId=\u8BF7\u8F93\u5165\u7528\u6237\u7F16\u53F7
Users.fieldPrompt.userName=\u8BF7\u8F93\u5165\u7528\u6237\u540D
Users.fieldPrompt.userPassword=\u8BF7\u8F93\u5165\u7528\u6237\u5BC6\u7801
Users.fieldPrompt.userPasswordConfirm=\u8BF7\u518D\u6B21\u8F93\u5165\u7528\u6237\u5BC6\u7801
Users.fieldPrompt.userPhone=\u8BF7\u8F93\u5165\u624B\u673A\u53F7
# Users field name
Users.fieldName.userId=\u7528\u6237\u7F16\u53F7
Users.fieldName.userName=\u7528\u6237\u540D
Users.fieldName.userPassword=\u7528\u6237\u5BC6\u7801
Users.fieldName.userPasswordConfirm=\u786E\u8BA4\u5BC6\u7801
Users.fieldName.userPhone=\u624B\u673A\u53F7
# Users result
Users.result.createError=\u8BE5\u7528\u6237\u7F16\u53F7\u6216\u7528\u6237\u540D\u4EE3\u8868\u7684\u7528\u6237\u5DF2\u5B58\u5728\uFF0C\u7528\u6237\u7F16\u53F7\u6216\u7528\u6237\u540D\u4E0D\u80FD\u91CD\u590D\uFF01
Users.result.create=\u521B\u5EFA\u65B0\u7528\u6237\u6210\u529F\uFF01
Users.result.delete=\u5220\u9664\u8BE5\u7528\u6237\u6210\u529F\uFF01
Users.result.deleteError=\u8BE5\u7528\u6237\u4E0D\u5B58\u5728\u6216\u5DF2\u5220\u9664\uFF01
Users.result.update=\u7528\u6237\u4FE1\u606F\u4FEE\u6539\u6210\u529F\uFF01
Users.result.updatePassword=\u7528\u6237\u5DF2\u627E\u5230\uFF0C\u5E76\u91CD\u7F6E\u7528\u6237\u5BC6\u7801\u6210\u529F\uFF01
Users.result.updateError=\u8BE5\u7528\u6237\u540D\u4EE3\u8868\u7684\u7528\u6237\u5DF2\u5B58\u5728\uFF0C\u4E0D\u80FD\u4F7F\u7528\u76F8\u540C\u7684\u7528\u6237\u540D\uFF01
#################### /cmis.admin.users ######################
当然,这里的很多属性值在web页构建的过程中会使用到。
至此,java编程基本完成,下一步就是进行配置设置,配置后的程序就可以部署了。