一竖配、表的設(shè)計(jì)
二何址、需求分析
1、用戶登錄
2进胯、用戶管理:用戶角色動(dòng)態(tài)分析
3用爪、角色管理:角色權(quán)限動(dòng)態(tài)分析
4、菜單管理
三胁镐、數(shù)據(jù)表的設(shè)計(jì)
1偎血、用戶、角色、權(quán)限菜單關(guān)系
2、用戶和角色是一對(duì)多的關(guān)系
3蝇棉、角色和權(quán)限菜單是多對(duì)多的關(guān)系
t_user用戶表
CREATE TABLE `t_user` (
`userId`?int(11)?NOT?NULL?AUTO_INCREMENT?COMMENT?'用戶id',
`userName`?varchar(20)?DEFAULT?NULL?COMMENT?'用戶名',
`password`?varchar(20)?DEFAULT?NULL?COMMENT?'密碼',
`userType`?tinyint(4)?DEFAULT?NULL?COMMENT?'用戶類型',
`roleId`?int(11)?DEFAULT?NULL?COMMENT?'角色id(外鍵)',
`userDescription`?varchar(200)?DEFAULT?NULL?COMMENT?'用戶備注',
PRIMARY?KEY?(`userId`),
KEY?`FK_t_user`?(`roleId`),
CONSTRAINT?`FK_t_user`?FOREIGN?KEY?(`roleId`)?REFERENCES?`t_role`?(`roleId`)
)?ENGINE=InnoDB?AUTO_INCREMENT=33?DEFAULT?CHARSET=utf8
t_role角色表
CREATE TABLE `t_role` (
`roleId`?int(11)?NOT?NULL?AUTO_INCREMENT?COMMENT?'角色id',
`roleName`?varchar(20)?DEFAULT?NULL?COMMENT?'角色名稱',
`authIds`?varchar(50)?DEFAULT?NULL?COMMENT?'菜單權(quán)限ID集合',
`roleDescription`?varchar(200)?DEFAULT?NULL?COMMENT?'角色備注',
PRIMARY?KEY?(`roleId`)
)?ENGINE=InnoDB?AUTO_INCREMENT=19?DEFAULT?CHARSET=utf8
t_ruth權(quán)限菜單表
CREATE TABLE `t_auth` (
`authId`?int(11)?NOT?NULL?AUTO_INCREMENT?COMMENT?'菜單ID',
`authName`?varchar(20)?DEFAULT?NULL?COMMENT?'菜單名稱',
`authPath`?varchar(100)?DEFAULT?NULL?COMMENT?'菜單路徑',
`parentId`?int(11)?DEFAULT?NULL?COMMENT?'父級(jí)id',
`authDescription`?varchar(200)?DEFAULT?NULL?COMMENT?'資源備注',
`state`?varchar(20)?DEFAULT?NULL?COMMENT?'狀態(tài)',
`iconCls`?varchar(20)?DEFAULT?NULL?COMMENT?'菜單圖標(biāo)',
PRIMARY?KEY?(`authId`)
)?ENGINE=InnoDB?AUTO_INCREMENT=26?DEFAULT?CHARSET=utf8
四、開(kāi)發(fā)環(huán)境搭建
4.1制跟、使用到技術(shù):jsp+servlet+mysql+easyui
4.2、web環(huán)境搭建
點(diǎn)擊下一步
點(diǎn)擊下一步
4.3、創(chuàng)建好的web項(xiàng)目工程結(jié)構(gòu)
4.4、導(dǎo)入相關(guān)的jar包
4.5空郊、引入jquery-easyui框架
4.6、編寫(xiě)JSP頁(yè)面代碼
login.jsp
<%@pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
"Content-Type"content="text/html; charset=UTF-8">
"style/login.css"rel="stylesheet"type="text/css">
用戶登陸
"text/javascript">
functionloadimage(){
document.getElementById("randImage").src="images/image.jsp?"+Math.random();
}
"form1"name="form1"action="user?action=login"method="post">
"MAIN">
"top">
"top2">
"topA">
"topB">
"http://www.baidu.com"target="_blank">"images/login/logo.gif"alt=""style=""/>
"topC">
"topD">
"login">
"topE">
"middle_A">
"middle_B">
"middle_C">"btn">""src="images/login/btnlogin.gif"onclick="javascript:document.getElementById('form1').submit()"/>? "red">${error }
"middle_D">
"bottom_A">
"bottom_B">
login.css
body
{
margin:0;
padding:0;
font-size:12px;
background:#214D90url(../images/login/bg.gif)repeat-x;
color:#999999;
font-family:Tahoma,Verdana;
}
ul
{
list-style:none;
margin:0;
padding:0;
}
.Main
{
width:940px;
margin:0auto;
}
.top
{
height:75px;
background:url(../images/login/login_01.gif)no-repeat;
}
.top2
{
height:94px;
background:url(../images/login/login_02.gif)no-repeat;
}
.topA
{
width:155px;
height:140px;
float:left;
overflow:hidden;
background:url(../images/login/login_03.gif)no-repeat;
}
.topB
{
width:282px;
height:140px;
float:left;
overflow:hidden;
background:url(../images/login/login_04.gif)no-repeat;
}
.topC
{
width:27px;
height:140px;
float:left;
overflow:hidden;
background:url(../images/login/login_05.gif)no-repeat;
}
.topD
{
width:318px;
height:140px;
float:left;
overflow:hidden;
background:url(../images/login/login_06.gif)no-repeat;
}
.topE
{
width:158px;
height:140px;
float:left;
overflow:hidden;
background:url(../images/login/login_07.gif)no-repeat;
}
.middle_A
{
width:155px;
height:94px;
float:left;
overflow:hidden;
background:url(../images/login/login_08.gif)no-repeat;
}
.middle_B
{
width:309px;
height:94px;
float:left;
overflow:hidden;
background:url(../images/login/login_09.gif)no-repeat;
}
.middle_C
{
width:318px;
height:94px;
float:left;
overflow:hidden;
background:url(../images/login/login_10.gif)no-repeat;
}
.middle_D
{
width:158px;
height:94px;
float:left;
overflow:hidden;
background:url(../images/login/login_11.gif)no-repeat;
}
.bottom_A
{
height:72px;
clear:both;
overflow:hidden;
background:url(../images/login/login_12.gif)no-repeat;
}
.bottom_B
{
height:70px;
overflow:hidden;
text-align:center;
background:url(../images/login/login_13.gif)no-repeat;
}
.login
{
}
.loginli
{
line-height:35px;
height:35px;
overflow:hidden;
}
.left
{line-height:35px;
vertical-align:middle;
width:50px;
height:34px;
text-align:right;
display:-moz-inline-box;
display:inline-block;
padding-bottom:3px;
}
.txt
{
width:215px;
height:30px;
line-height:30px;
overflow:hidden;
border:0px;
padding-left:3px;
color:#999999;
background:url(../images/login/txt.gif)no-repeatleftcenter;
}
.txtCode
{
line-height:30px;
width:93px;
height:30px;
overflow:hidden;
border:0px;
padding-left:3px;
color:#999999;
background:url(../images/login/code.gif)no-repeatleftcenter;
}
.btn
{
margin-left:53px;
}
main.jsp注界面
${currentuser.userName},登陸成功
4.7羞延、編寫(xiě)相關(guān)后端代碼
實(shí)體類
packagecom.xiaowen.model;
importjava.io.Serializable;
/**
* 用戶實(shí)體類
*@authorxiaowen
*/
publicclassUserimplementsSerializable {
privatestaticfinallongserialVersionUID= 1L;
privateIntegeruserId;//用戶ID
privateStringuserName;//用戶名
privateStringpassword;//密碼
privateIntegeruserType;//用戶類型
privateIntegerroleId=1;//角色I(xiàn)D
privateStringuserDescription;//用戶備注
publicUser() {
super();
}
publicUser(StringuserName, Stringpassword) {
super();
this.userName=userName;
this.password=password;
}
publicInteger getUserId() {
returnuserId;
}
publicvoidsetUserId(IntegeruserId) {
this.userId=userId;
}
publicString getUserName() {
returnuserName;
}
publicvoidsetUserName(StringuserName) {
this.userName=userName;
}
publicString getPassword() {
returnpassword;
}
publicvoidsetPassword(Stringpassword) {
this.password=password;
}
publicInteger getUserType() {
returnuserType;
}
publicvoidsetUserType(IntegeruserType) {
this.userType=userType;
}
publicInteger getRoleId() {
returnroleId;
}
publicvoidsetRoleId(IntegerroleId) {
this.roleId=roleId;
}
publicString getUserDescription() {
returnuserDescription;
}
publicvoidsetUserDescription(StringuserDescription) {
this.userDescription=userDescription;
}
}
dao類
packagecom.xiaowen.dao;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importcom.xiaowen.model.User;
/**
* 用戶的Dao數(shù)據(jù)層
*@authorxiaowen
*/
publicclassUserDao {
/**
* 用戶登陸
*@paramcon
*@paramuser
*@return
*@throwsException
*/
publicUserlogin(Connectioncon,Useruser)throwsException{
UserresultUser=null;
Stringsql="select * from t_user where userName=? and password=?";
PreparedStatementpst=con.prepareStatement(sql);
pst.setString(1,user.getUserName());
pst.setString(2,user.getPassword());
ResultSetrs=pst.executeQuery();
if(rs.next()){
resultUser=newUser();
resultUser.setUserId(rs.getInt("userId"));
resultUser.setUserName(rs.getString("userName"));
resultUser.setPassword(rs.getString("password"));
resultUser.setRoleId(rs.getInt("roleId"));
}
returnresultUser;
}
}
工具類
packagecom.xiaowen.util;
importjava.sql.Connection;
importjava.sql.DriverManager;
/**
* 數(shù)據(jù)庫(kù)工具類
*@authorxiaowen
*/
publicclassDbUtil {
privateStringdbUrl="jdbc:mysql://localhost:3306/db_rbps";
privateStringdbUserName="root";
privateStringdbPassword="123456";
privateStringdbDriver="com.mysql.jdbc.Driver";
/**
* 數(shù)據(jù)庫(kù)連接
*@return
*@throwsException
*/
publicConnection getCon()throwsException{
Class.forName(dbDriver);
Connectioncon=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
returncon;
}
/**
* 數(shù)據(jù)庫(kù)關(guān)閉
*@paramcon
*@throwsException
*/
publicvoidcloseCon(Connectioncon)throwsException{
if(con!=null){
con.close();
}
}
publicstaticvoidmain(String[]args) {
DbUtildbUtil=newDbUtil();
try{
dbUtil.getCon();
System.out.println("數(shù)據(jù)庫(kù)連接成功!");
}catch(Exceptione) {
e.printStackTrace();
System.out.println("數(shù)據(jù)庫(kù)連接失斊⒒埂伴箩!");
}
}
}
service層
packagecom.xiaowen.server;
importjava.io.IOException;
importjava.sql.Connection;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importcom.xiaowen.dao.UserDao;
importcom.xiaowen.model.User;
importcom.xiaowen.util.DbUtil;
importcom.xiaowen.util.StringUtil;
/**
* 用戶Server處理層
*@authorxiaowen
*/
publicclassUserServerextendsHttpServlet{
privatestaticfinallongserialVersionUID= 1L;
DbUtildbUtil=newDbUtil();
UserDaouserDao=newUserDao();
Connectioncon=null;
@Override
publicvoidservice(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException, IOException {
response.setCharacterEncoding("utf-8");
Stringaction=request.getParameter("action");
if("login".equals(action)){
login(request,response);
}
}
privatevoidlogin(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException, IOException {
HttpSessionsession=request.getSession();
StringuserName=request.getParameter("userName");
Stringpassword=request.getParameter("password");
StringimageCode=request.getParameter("imageCode");
request.setAttribute("userName",userName);
request.setAttribute("password",password);
request.setAttribute("imageCode",imageCode);
if(StringUtil.isEmpty(userName)||StringUtil.isEmpty(password)){
request.setAttribute("error","用戶名或密碼錯(cuò)誤!");
request.getRequestDispatcher("login.jsp").forward(request,response);
return;
}
if(StringUtil.isEmpty(imageCode)){
request.setAttribute("error","驗(yàn)證碼為空鄙漏!");
request.getRequestDispatcher("login.jsp").forward(request,response);
return;
}
if(!imageCode.equals(session.getAttribute("sRand"))){
request.setAttribute("error","驗(yàn)證碼錯(cuò)誤");
request.getRequestDispatcher("login.jsp").forward(request,response);
return;
}
Useruser=newUser(userName,password);
try{
con=dbUtil.getCon();
Usercurrentuser=userDao.login(con,user);
if(currentuser==null){
request.setAttribute("error","用戶名或密碼錯(cuò)誤嗤谚!");
request.getRequestDispatcher("login.jsp").forward(request,response);
}else{
session.setAttribute("currentuser",currentuser);
response.sendRedirect("main.jsp");
}
}catch(Exceptione) {
e.printStackTrace();
}finally{
try{
dbUtil.closeCon(con);
}catch(Exceptione) {
e.printStackTrace();
}
}
}
}