一、登录 、注册
1、导入相关工具类、jar包及界面
mvc.jar以及相关环境,Easyui的文件,json相关资源
2、登录、注册界面
①、登录界面代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>网上书城登录</title>
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/static/css/fg.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
<script src="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/js/bootstrap.js"></script>
</head>
<body class="text-center">
<form class="form-signin" action="${pageContext.request.contextPath}/user.action?methodName=login" method="post">
<h1 class="h3 mb-3 font-weight-normal">用户登录</h1>
<label for="name" class="sr-only">账号</label>
<input type="text" id="name" name="name" class="form-control" placeholder="请输入账号" required autofocus>
<label for="pwd" class="sr-only">密码</label>
<input type="password" id="pwd" name="pwd" class="form-control" placeholder="请输入密码" required>
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" id="login">登录</button>
<p class="mt-5 mb-3 text-muted">© 2017-2020</p>
</form>
<script>
$(function () {
<%--$("#login").click(function () {--%>
<%--$.ajax({--%>
<%--url:'${pageContext.request.contextPath}/user.action?methodName=login',--%>
<%--data:"name="+$("#name").val()+"&pwd="+$("#pwd").val(),--%>
<%--success:function (data) {--%>
<%--}--%>
<%--});--%>
<%--});--%>
})
</script>
</body>
</html>
②、注册界面代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>网上书城注册</title>
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/static/css/fg.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
<script src="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/js/bootstrap.js"></script>
</head>
<body class="text-center">
<form class="form-signin" action="${pageContext.request.contextPath}/user.action?methodName=register" method="post">
<h1 class="h3 mb-3 font-weight-normal">用户注册</h1>
<label for="name" class="sr-only">账号</label>
<input type="text" id="name" name="name" class="form-control" placeholder="请输入账号" required autofocus>
<label for="pwd" class="sr-only">密码</label>
<input type="password" id="pwd" name="pwd" class="form-control" placeholder="请输入密码" required>
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" id="">注册</button>
<p class="mt-5 mb-3 text-muted">© 2017-2020</p>
</form>
</body>
</html
3、实体类、dao、web
①、实体类
package com.csf.entity;
public class User {
private long id;
private String name;
private String pwd;
private int type;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", type=" + type + "]";
}
}
②、UserDao
package com.csf.dao;
import com.csf.entity.User;
import com.zking.util.BaseDao;
public class UserDao extends BaseDao<User> {
public User login(User user) throws Exception {
String sql="select * from t_easyui_user where name='"+user.getName()+"' and pwd='"+user.getPwd()+"'";
return super.executeQuery(sql, User.class, null).get(0);
}
public void add(User user) throws Exception {
String sql="insert into t_easyui_user(name,pwd) values(?,?)";
super.executeUpdate(sql, user, new String [] {"name","pwd"});
}
}
③、UserAction
package com.csf.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.csf.dao.UserDao;
import com.csf.entity.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
public class UserAction extends ActionSupport implements ModelDriver<User> {
private User user=new User();
private UserDao userDao=new UserDao();
public User getModel() {
return user;
}
public String login(HttpServletRequest req, HttpServletResponse resp) {
try {
User u = userDao.login(user);
if(u==null) {
return "toLogin";
}
req.getSession().setAttribute("cuser", u);
} catch (Exception e) {
e.printStackTrace();
return "toLogin";
}
// 只要数据库中有这个用户,就跳转到主界面
return "main";
}
public String register(HttpServletRequest req, HttpServletResponse resp) {
try {
userDao.add(user);
req.setAttribute("msg", "用户名密码错误");
} catch (Exception e) {
e.printStackTrace();
return "toRegister";
}
// 如果注册成功,跳转到登录界面
return "toLogin";
}
}
4、配置
编写配置文件,mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/user" type="com.ysq.web.UserAction">
<forward name="main" path="/bg/mainTemp.jsp" redirect="false" />
<forward name="toLogin" path="/login.jsp" redirect="true" />
<forward name="toRegister" path="/register.jsp" redirect="false" />
</action>
</config>
5、界面展示
登录界面:
注册界面:
后台主页面:
考虑到用户登录和管理员登录两种情况(权限菜单)
管理员
用户:
dao层实现:
public List<Permission> listPlus(String ids) throws Exception {
String sql="select * from t_easyui_permission where id in ("+ids+")";
return super.executeQuery(sql, Permission.class, null);
}
原理:在表中,有专门的权限表,对应用户类型和菜单id,所以还需要提供查询权限的方法,
另写一个类
public List<RolePermission> findRolePermission(int type) throws Exception {
String sql="select * from t_easyui_role_Permission where rid="+type+"";
return super.executeQuery(sql,RolePermission.class, null);
}
而具体得到菜单需要综合运用:
public String tree(HttpServletRequest req, HttpServletResponse resp) {
try {
User cuser = (User) req.getSession().getAttribute("cuser");
if(cuser ==null) {
return "toLogin";
}
int type=cuser.getType();
List<RolePermission> findRolePermission = Rolepermissiondao.findRolePermission(type);
StringBuffer sb=new StringBuffer();
for (RolePermission rp : findRolePermission) {
sb.append(",").append(rp.getPid());
}
List<TreeVo<Permission>> treePlus = permissionDao.treePlus(sb.substring(1));
ResponseUtil.writeJson(resp, treePlus);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, "0");
} catch (Exception e1) {
e1.printStackTrace();
}
}
return null;
}
就可以达到不同身份进入后台,所看到的菜单不一样