(源代码见仓库:https://gitee.com/jianghao233/course)
文件目录:
新增代码:
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"%>
classManager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
班级列表
班级ID
班级名称
操作
1
一班
编辑
删除
2
二班
编辑
删除
3
三班
编辑
删除
4
四班
编辑
删除
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
管理员登陆页面
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
adminid, username, password
delete from tb_admin
where adminid = #{adminid,jdbcType=INTEGER}
insert into tb_admin (adminid, username, password
)
values (#{adminid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
)
insert into tb_admin
adminid,
username,
password,
#{adminid,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
update tb_admin
username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
where adminid = #{adminid,jdbcType=INTEGER}
update tb_admin
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR}
where adminid = #{adminid,jdbcType=INTEGER}
AdminService.java
package com.neuedu.service;
import com.neuedu.po.TbAdmin;
public interface AdminService {
public TbAdmin login(String username,String password); //新增代码
}
AdminServiceImpl.java
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";
}
}
输出:添加验证功能---班级管理页面完成