1.简介
用户登录模块,指的是根据用户输入的用户名和密码,对用户的身份进行验证等。如果用户没有登录,用户就无法访问其他的一些jsp页面,甚至是action都不能访问。
二、简单设计及实现
本程序是基于Java的SSH框架进行的。
1.数据库设计
用户登录模块,指的是根据用户输入的用户名和密码,对用户的身份进行验证等。如果用户没有登录,用户就无法访问其他的一些jsp页面,甚至是action都不能访问。
二、简单设计及实现
本程序是基于Java的SSH框架进行的。
1.数据库设计
我们应该设计一个用户表,其Userinfo表,对应的SQL语句为(Oracle数据库):
create table userinfo
(
id varchar2(36) primary key, username varchar2(50) not null, password varchar2(50) not null, auth_limit varchar2(10) not null, register_time varchar2(40), create_time varchar2(40), remarks varchar2(1024) );
分别是id,也就是UUID,用户名、密码、权限、注册时间、记录插入数据库的时间、备注等几个字段。
2.使用MyEclipse的Hibernate逆向工具,生成对应的Java Bean和相应的hibernate的xml配置文件Userinfo.hbm.xml
package edu.njupt.zhb.bean;
/**
* Userinfo entity. @author MyEclipse Persistence Tools
*/
public class Userinfo implements java.io.Serializable { // Fields private String id; private String username; private String password; private String authLimit; private String registerTime; private String createTime; private String remarks; // Constructors /** default constructor */ public Userinfo() { } /** minimal constructor */ public Userinfo(String id, String username, String password, String authLimit) { this.id = id; this.username = username; this.password = password; this.authLimit = authLimit; } /** full constructor */ public Userinfo(String id, String username, String password, String authLimit, String registerTime, String createTime, String remarks) { this.id = id; this.username = username; this.password = password; this.authLimit = authLimit; this.registerTime = registerTime; this.createTime = createTime; this.remarks = remarks; } // Property accessors public String getId() { return this.id; } public void setId(String id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public String getAuthLimit() { return this.authLimit; } public void setAuthLimit(String authLimit) { this.authLimit = authLimit; } public String getRegisterTime() { return this.registerTime; } public void setRegisterTime(String registerTime) { this.registerTime = registerTime; } public String getCreateTime() { return this.createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public String getRemarks() { return this.remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } }
3.新建一个UserInfoService接口
/*
* $filename: VideoInfoService.java,v $
* $Date: 2014-1-2 $
* Copyright (C) ZhengHaibo, Inc. All rights reserved.
* This software is Made by Zhenghaibo.
*/ package edu.njupt.zhb.service; import edu.njupt.zhb.bean.Userinfo; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *GitHub https://github.com/nuptboyzhb *mail: zhb931706659@126.com *2014-1-2 Nanjing,njupt,China */ public interface UserInfoService { String getUserInfoList(int page, int rows); String addUser(Userinfo userinfo); String deleteUser(String userId); String editUser(Userinfo userinfo); Userinfo getUserInfoByName(String username); }
4.新建一个UserInfoServiceImpl类
/*
* $filename: VideoInfoServiceImpl.java,v $
* $Date: 2014-1-2 $
* Copyright (C) ZhengHaibo, Inc. All rights reserved.
* This software is Made by Zhenghaibo.
*/
package edu.njupt.zhb.service.impl;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import edu.njupt.zhb.bean.Userinfo; import edu.njupt.zhb.dao.BaseDao; import edu.njupt.zhb.service.UserInfoService; import edu.njupt.zhb.utils.DataGrid; import edu.njupt.zhb.utils.Tips; import edu.njupt.zhb.view.ViewUser; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *GitHub https://github.com/nuptboyzhb *mail: zhb931706659@126.com *2014-1-2 Nanjing,njupt,China */ public class UserInfoServiceImpl implements UserInfoService{ private BaseDao<Userinfo> userinfoBaseDao; public BaseDao<Userinfo> getUserBaseDao() { return userinfoBaseDao; } public void setUserBaseDao(BaseDao<Userinfo> userinfoBaseDao) { this.userinfoBaseDao = userinfoBaseDao; } @Override public String getUserInfoList(int page, int rows) { // TODO Auto-generated method stub System.out.println("page="+page+",rows="+rows); String hql = "from Userinfo"; try { List<Userinfo> list = userinfoBaseDao.find(hql,page,rows); List<ViewUser> resultList = new ArrayList<ViewUser>(); for(Userinfo userinfo:list){ ViewUser viewUser = new ViewUser(); viewUser.setBz(userinfo.getRemarks()); viewUser.setId(userinfo.getId()); viewUser.setPwd(userinfo.getPassword()); viewUser.setYhm(userinfo.getUsername()); viewUser.setYhqx(userinfo.getAuthLimit()); viewUser.setZcsj(userinfo.getRegisterTime()); resultList.add(viewUser); } DataGrid<ViewUser> dataGrid = new DataGrid<ViewUser>(); dataGrid.setRows(resultList); dataGrid.setTotal(userinfoBaseDao.total(hql)); String result = JSONObject.fromObject(dataGrid).toString(); return result; } catch (Exception e) { e.printStackTrace(); return null; } } public String addUser(Userinfo userinfo) { // TODO Auto-generated method stub Tips tips = new Tips(); String hql = "from Userinfo where username = '"+userinfo.getUsername()+"'"; try { List<Userinfo> list = userinfoBaseDao.find(hql); if(list!=null&&list.size()>0){ tips.setMsg("添加失败!用户名已经存在!"); return JSONObject.fromObject(tips).toString(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { userinfoBaseDao.save(userinfo); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); tips.setMsg("添加失败"); return JSONObject.fromObject(tips).toString(); } tips.setMsg("添加成功"); return JSONObject.fromObject(tips).toString(); } public String deleteUser(String userid) { // TODO Auto-generated method stub Tips tips = new Tips(); try { userinfoBaseDao.executeHql("delete from Userinfo where id = '"+userid+"'"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); tips.setMsg("删除失败");