本系统为本人学习SSH三大框架时所做的整合实例,系统角色包括普通用户和管理员两种,首页有管理员登录入口链接。系统功能主要包括管理员对用户的基本增、删、改、查和分页显示用户信息等。
使用UUID并将其切片,取前八位作为验证码(本算法尚不成熟,在大量使用后可能出现重复)
public static String[] codeMaker() {
String[] code = new String[3];
for (int i=0; i<3; i++) {
code[i] = UUID.randomUUID().toString().substring(0,8).toUpperCase();
}
return code;
}
public String login() {
User existUser = userService.login(user);
if (existUser == null) {
this.addActionError("用户名或密码错误");
return INPUT;
} else {
ActionContext.getContext().getSession().put("currUser", existUser);
return SUCCESS;
}
}
@Override
/**
* 业务层登陆的方法
*/
public User login(User user) {
User existUser = userDao.findByUsernameAndPassword(user);
return existUser;
}
@SuppressWarnings("unchecked")
@Override
/**
* dao中根据用户名和密码查询用户的方法
*/
public User findByUsernameAndPassword(User user) {
String hql = "from User where username = ? and password = ?";
List list = this.getHibernateTemplate().find(hql, user.getUsername(), user.getPassword());
if (list.size() > 0) {
return list.get(0);
}
return null;
}
<body>
<div id="cc" class="easyui-layout" fit=true style="width:100%;height:100%;">
<div region="west" split="true" title="菜单" style="width:200px;">
<div id="aa" class="easyui-accordion" fit=true>
<div title="功能导航" selected="true" style="overflow:auto;padding:10px;">
<div class="tab" style="border: 1px;">
<a href="user_info">个人信息a>
div>
<div class="tab" style="border: 1px;">
<a href="user_invite">已邀请人的信息a>
div>
<div class="tab" style="border: 1px;">
<a href="user_code">邀请码信息a>
div>
div>
div>
div>
<div region="center" title="主界面" style="padding:5px;">
<div id="welcome" class="tab">
<span>欢迎你,${currUser.name }span>
<span>
<a href="${ctx }/index.jsp">退出系统a>
span>
div>
<div id="tt" class="easyui-tabs" fit=true style="width:500px;height:250px;">div>
div>
div>
body>
public String findAll() {
PageBean pageBean = adminService.findAll(currPage);
List list = pageBean.getList();
ActionContext.getContext().getValueStack().push(pageBean);
ActionContext.getContext().getSession().put("users", list);
return "findAll";
}
private int currPage; //当前页
private int pageSize; //每页显示记录数
private int totalCount; //总记录
private int totalPage; //总页数
private List list; //封装实体
// 分页查询所有用户
@Override
public PageBean findAll(int currPage) {
PageBean pageBean = new PageBean();
// 封装pageBean
pageBean.setCurrPage(currPage); // 当前页数
int pageSize = 10;
pageBean.setPageSize(pageSize); // 每页记录数
int totalCount = adminDao.findCount();
pageBean.setTotalCount(totalCount); // 总记录数
double tc = totalCount; // 总页数
Double num = Math.ceil(tc / pageSize);
pageBean.setTotalPage(num.intValue());
int begin = (currPage - 1) * pageSize; // 每页开始的记录索引
List list = adminDao.findByPage(begin,pageSize); // list用来封装用户集合
pageBean.setList(list);
return pageBean;
}
@SuppressWarnings("unchecked")
@Override
public List findAll() {
String hql = "select * from User where uid != 1";
List list = this.getHibernateTemplate().find(hql);
if (list.size() > 0) {
return list;
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public int findCount() {
String hql = "select count(*) from User";
List list = this.getHibernateTemplate().find(hql);
if(list.size() > 0) {
return list.get(0).intValue();
}
return 0;
}
/**
* 分页函数
*/
@SuppressWarnings("unchecked")
@Override
public List findByPage(int begin, int pageSize) {
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
List list = this.getHibernateTemplate().findByCriteria(criteria, begin, pageSize);
return list;
}
<div id="page" align="left" class="font">
<table border="0" cellspacing="0" cellpadding="0" width="500px">
<tr>
<td align="left">
<span>第<s:property value="currPage" />/<s:property value="totalPage" />页span>
<span>
总记录数:<s:property value="totalCount" />
每页显示:<s:property value="pageSize" />
span>
<span>
<s:if test="currPage != 1">
<a href="${ctx }/admin_findAll?currPage=1">[首页]a>
<a href="${ctx }/admin_findAll?currPage=currPage-1" />">[上一页]a>
s:if>
<s:if test="currPage != totalPage">
<a href="${ctx }/admin_findAll?currPage=currPage+1" />">[下一页]a>
<a href="${ctx }/admin_findAll?currPage=totalPage" />">[尾页]a>
s:if>
span>
td>
tr>
table>
div>