SpringMVC实战--构建学生管理系统(03)
登陆页面验证及部分首页制作:(在上篇博客基础上继续)
文件目录:
新增代码:
ClassController.java
package com.neuedu.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/class")
public class ClassController {
@RequestMapping("/list")
public String list(HttpSession session) {
Object admin = session.getAttribute("admin");
if(admin == null) {
return "admin/login";
}
return "admin/classManager";
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin_index.css"/>
</head>
<body>
<div id="header">
</div>
<div id="left">
<ul id="nav">
<li><a href="${pageContext.request.contextPath}/class/list" target="content">班级管理</a></li>
<li><a href="studentManager.html" target="content">学生管理</a></li>
<li><a href="courseManager.html" target="content">课程管理</a></li>
<li><a href="scoreInManager.html" target="content">成绩录入</a></li>
</ul>
</div>
<div id="right">
<iframe id="iframe" name="content" src="" width="100%" height="700px"></iframe>
</div>
</body>
</html>
classManager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-common.css"/>
<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/static/js/admin-class.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="class-title">
班级列表
</div>
<div id="add-div">
<input type="button" id="add-button" value="添加" onclick="add()"/>
<div id="form-div">
<form action="" method="post">
班级名称:<input type="text" name="classname"/>
<input type="submit" value="提交"/>
</form>
</div>
</div>
<table cellspacing="0">
<tr>
<th>班级ID</th>
<th>班级名称</th>
<th>操作</th>
</tr>
<tr>
<td>1</td>
<td>一班</td>
<td>
<a href="">编辑</a>
<a href="">删除</a>
</td>
</tr>
<tr>
<td>2</td>
<td>二班</td>
<td>
<a href="">编辑</a>
<a href="">删除</a>
</td>
</tr>
<tr>
<td>3</td>
<td>三班</td>
<td>
<a href="">编辑</a>
<a href="">删除</a>
</td>
</tr>
<tr>
<td>4</td>
<td>四班</td>
<td>
<a href="">编辑</a>
<a href="">删除</a>
</td>
</tr>
</table>
</body>
</html>
修改代码:
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>管理员登陆页面</title>
<link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/htmleaf-demo.css">
<link href="${pageContext.request.contextPath}/static/css/signin.css" rel="stylesheet">
</head>
<body>
<div class="htmleaf-container">
<div class="signin">
<div class="signin-head"><img src="${pageContext.request.contextPath}/static/images/test/head_120.png" alt="" class="img-circle"></div>
<form class="form-signin" role="form" action="${pageContext.request.contextPath}/login/login" method="post">
<input name="username" value="${username}" type="text" class="form-control" placeholder="用户名" required autofocus />${message} //修改代码
<input name="password" value="${password}" type="password" class="form-control" placeholder="密码" required />
<button class="btn btn-lg btn-warning btn-block" type="submit">登录</button>
<label class="checkbox">
<input type="checkbox" value="remember-me"> 记住我
</label>
</form>
</div>
</div>
</body>
</html>
TbAdminMapper.java
package com.neuedu.mapper;
import org.apache.ibatis.annotations.Param;
import com.neuedu.po.TbAdmin;
public interface TbAdminMapper {
int deleteByPrimaryKey(Integer adminid);
int insert(TbAdmin record);
int insertSelective(TbAdmin record);
TbAdmin selectByPrimaryKey(Integer adminid);
int updateByPrimaryKeySelective(TbAdmin record);
int updateByPrimaryKey(TbAdmin record);
TbAdmin getAdminByUsernameAndPassword(@Param("username")String //新增代码
username,@Param("password")String password); //新增代码
}
TbAdminMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.neuedu.mapper.TbAdminMapper" >
<resultMap id="BaseResultMap" type="com.neuedu.po.TbAdmin" >
<id column="adminid" property="adminid" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
adminid, username, password
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from tb_admin
where adminid = #{adminid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from tb_admin
where adminid = #{adminid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.neuedu.po.TbAdmin" >
insert into tb_admin (adminid, username, password
)
values (#{adminid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.neuedu.po.TbAdmin" >
insert into tb_admin
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="adminid != null" >
adminid,
</if>
<if test="username != null" >
username,
</if>
<if test="password != null" >
password,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="adminid != null" >
#{adminid,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.neuedu.po.TbAdmin" >
update tb_admin
<set >
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
</if>
</set>
where adminid = #{adminid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.neuedu.po.TbAdmin" >
update tb_admin
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR}
where adminid = #{adminid,jdbcType=INTEGER}
</update>
<select id="getAdminByUsernameAndPassword" resultMap="BaseResultMap"> //新增代码
select * from tb_admin where username = #{username} and password = #{password} //新增代码
</select>
</mapper>
AdminService.java
package com.neuedu.service;
import com.neuedu.po.TbAdmin;
public interface AdminService {
public TbAdmin login(String username,String password); //新增代码
}
AdminServiceImpl.java
扫描二维码关注公众号,回复:
2693075 查看本文章
![](/qrcode.jpg)
package com.neuedu.service.impl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neuedu.mapper.TbAdminMapper;
import com.neuedu.po.TbAdmin;
import com.neuedu.service.AdminService;
@Service
public class AdminServiceImpl implements AdminService {
@Autowired
private TbAdminMapper tbAdminMapper; //新增代码
@Override
public TbAdmin login(String username, String password) { //新增代码
return tbAdminMapper.getAdminByUsernameAndPassword(username, password); //新增代码
}
}
LoginController.java
package com.neuedu.controller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.neuedu.po.TbAdmin;
import com.neuedu.service.AdminService;
@RequestMapping("login")
@Controller
public class LoginController {
@Autowired
private AdminService as;
@RequestMapping("/show")
public String showLogin() {
return "admin/login";
}
/*处理登陆请求*/ //新增代码
@RequestMapping("/login")
public String login(String username,String password,HttpSession session,Model model) {
TbAdmin tbAdmin = as.login(username, password);
if(tbAdmin == null) { //登陆失败 //新增代码
model.addAttribute("username", username);
model.addAttribute("password", password);
model.addAttribute("message", "用户名或密码错误");
return "admin/login";
}
//登陆成功 //新增代码
session.setAttribute("admin", tbAdmin);
return "admin/index";
}
}
输出:添加验证功能---班级管理页面完成