Skip to content

Commit a2a68e3

Browse files
committed
shy-登录界面,主页导航栏
1 parent bd21ba2 commit a2a68e3

25 files changed

+524
-29
lines changed

src/main/java/com/niit/sms/bean/Student.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,15 @@
99
@NoArgsConstructor
1010
public class Student {
1111

12-
private Integer id;
13-
private String sno;
12+
private Integer sno;
1413
private String username;
1514
private String password;
16-
private char gender = '男';//default
15+
private char gender;
1716
private String email;
1817
private String telephone;
1918
private String address;
2019
private String introduce;
2120
private String portraitPath;//存储头像的项目路径
2221
private String clazzId;//班级名称
2322

24-
public Student(String username, String clazzId) {
25-
this.username = username;
26-
this.clazzId = clazzId;
27-
}
28-
2923
}

src/main/java/com/niit/sms/bean/Teacher.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
@NoArgsConstructor
1010
public class Teacher {
1111

12-
private Integer id;
13-
private String tno;
12+
private Integer tno;
1413
private String username;
1514
private String password;
1615
private char gender;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.niit.sms.config;
2+
3+
import org.springframework.web.servlet.HandlerInterceptor;
4+
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
8+
public class LoginInterceptor implements HandlerInterceptor {
9+
10+
@Override
11+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
12+
Object loginUser = request.getSession().getAttribute("loginUser");
13+
if (loginUser==null) {
14+
// request.setAttribute("msg","请先登录");
15+
response.sendRedirect("/login.html");
16+
return false;
17+
}else {
18+
return true;
19+
}
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.niit.sms.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
5+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
7+
8+
@Configuration
9+
public class MyMvcConfig implements WebMvcConfigurer {
10+
11+
@Override
12+
public void addInterceptors(InterceptorRegistry registry) {
13+
registry.addInterceptor(new LoginInterceptor())
14+
.addPathPatterns("/**")
15+
.excludePathPatterns("/","/user/login","/login.html","/layui/**");
16+
}
17+
18+
@Override
19+
public void addViewControllers(ViewControllerRegistry registry) {
20+
registry.addViewController("/").setViewName("login");
21+
registry.addViewController("/login.html").setViewName("login");
22+
registry.addViewController("/index.html").setViewName("index");
23+
registry.addViewController("/main.html").setViewName("main");
24+
registry.addViewController("/stuList.html").setViewName("stuList");
25+
registry.addViewController("/teaList.html").setViewName("teaList");
26+
registry.addViewController("/userList.html").setViewName("userList");
27+
registry.addViewController("/personInfo.html").setViewName("personInfo");
28+
registry.addViewController("/gradeList.html").setViewName("gradeList");
29+
registry.addViewController("/clazzList.html").setViewName("clazzList");
30+
}
31+
}

src/main/java/com/niit/sms/controller/UserController.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,61 @@
11
package com.niit.sms.controller;
22

3+
import com.niit.sms.bean.Admin;
4+
import com.niit.sms.bean.Student;
5+
import com.niit.sms.bean.Teacher;
36
import com.niit.sms.service.AdminService;
7+
import com.niit.sms.service.StudentrService;
8+
import com.niit.sms.service.TeacherService;
49
import org.springframework.beans.factory.annotation.Autowired;
510
import org.springframework.stereotype.Controller;
11+
import org.springframework.ui.Model;
612
import org.springframework.web.bind.annotation.RequestMapping;
7-
import org.springframework.web.bind.annotation.ResponseBody;
13+
14+
import javax.servlet.http.HttpSession;
815

916
@Controller
1017
@RequestMapping("/user")
1118
public class UserController {
1219

1320
@Autowired
1421
private AdminService adminService;
22+
@Autowired
23+
private TeacherService teacherService;
24+
@Autowired
25+
private StudentrService studentrService;
1526

1627
@RequestMapping("/login")
17-
@ResponseBody
18-
public String login(String username, String password) {
19-
String result = adminService.login(username, password).toString();
20-
if (result==null){
21-
return "!!!";
28+
public String login(String username, String password, String role, Model model, HttpSession session) {
29+
if ("0".equals(role)) {
30+
Admin admin = adminService.login(username, password);
31+
if (admin==null) {
32+
model.addAttribute("msg","用户名或密码错误");
33+
return "login";
34+
}else {
35+
session.setAttribute("loginUser",admin);
36+
session.setAttribute("role",0);
37+
return "redirect:/main.html";
38+
}
39+
}else if ("1".equals(role)) {
40+
Teacher teacher = teacherService.login(username, password);
41+
if (teacher==null) {
42+
model.addAttribute("msg","用户名或密码错误");
43+
return "login";
44+
}else {
45+
session.setAttribute("loginUser",teacher);
46+
session.setAttribute("role",1);
47+
return "redirect:/main.html";
48+
}
2249
}else {
23-
return result;
50+
Student student = studentrService.login(username, password);
51+
if (student==null) {
52+
model.addAttribute("msg","用户名或密码错误");
53+
return "login";
54+
}else {
55+
session.setAttribute("loginUser",student);
56+
session.setAttribute("role",2);
57+
return "redirect:/main.html";
58+
}
2459
}
2560
}
2661

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.niit.sms.mapper;
2+
3+
import com.niit.sms.bean.Student;
4+
import org.apache.ibatis.annotations.Param;
5+
6+
public interface StudentMapper {
7+
8+
Student login(@Param("sno") String sno, @Param("password") String password);
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.niit.sms.mapper;
2+
3+
import com.niit.sms.bean.Teacher;
4+
import org.apache.ibatis.annotations.Param;
5+
6+
public interface TeacherMapper {
7+
8+
Teacher login(@Param("tno") String tno, @Param("password") String password);
9+
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.niit.sms.service;
2+
3+
import com.niit.sms.bean.Student;
4+
5+
public interface StudentrService {
6+
7+
Student login(String sno, String password);
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.niit.sms.service;
2+
3+
import com.niit.sms.bean.Teacher;
4+
5+
public interface TeacherService {
6+
7+
Teacher login(String tno, String password);
8+
9+
}

src/main/java/com/niit/sms/service/impl/AdminServiceImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.niit.sms.bean.Admin;
44
import com.niit.sms.mapper.AdminMapper;
55
import com.niit.sms.service.AdminService;
6+
import com.niit.sms.utils.MD5Util;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Service;
89

@@ -14,6 +15,7 @@ public class AdminServiceImpl implements AdminService {
1415

1516
@Override
1617
public Admin login(String username, String password) {
17-
return adminMapper.login(username, password);
18+
String mdPassword = MD5Util.MD5Lower(password);
19+
return adminMapper.login(username, mdPassword);
1820
}
1921
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.niit.sms.service.impl;
2+
3+
import com.niit.sms.bean.Student;
4+
import com.niit.sms.mapper.StudentMapper;
5+
import com.niit.sms.service.StudentrService;
6+
import com.niit.sms.utils.MD5Util;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
10+
@Service
11+
public class StudentServiceImpl implements StudentrService {
12+
13+
@Autowired
14+
private StudentMapper studentMapper;
15+
16+
@Override
17+
public Student login(String tno, String password) {
18+
String mdPassword = MD5Util.MD5Lower(password);
19+
return studentMapper.login(tno, mdPassword);
20+
21+
}
22+
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.niit.sms.service.impl;
2+
3+
import com.niit.sms.bean.Admin;
4+
import com.niit.sms.bean.Teacher;
5+
import com.niit.sms.mapper.TeacherMapper;
6+
import com.niit.sms.service.TeacherService;
7+
import com.niit.sms.utils.MD5Util;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public class TeacherServiceImpl implements TeacherService {
13+
14+
@Autowired
15+
private TeacherMapper teacherMapper;
16+
17+
@Override
18+
public Teacher login(String tno, String password) {
19+
String mdPassword = MD5Util.MD5Lower(password);
20+
return teacherMapper.login(tno, mdPassword);
21+
22+
}
23+
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.niit.sms.utils;
2+
3+
import java.math.BigInteger;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
7+
public class MD5Util {
8+
9+
public static String MD5Lower(String plainText) {
10+
try {
11+
// 获得MD5摘要算法的 MessageDigest 对象
12+
MessageDigest md = MessageDigest.getInstance("MD5");
13+
14+
// 使用指定的字节更新摘要
15+
md.update(plainText.getBytes());
16+
17+
// digest()最后确定返回md5 hash值,返回值为8位字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
18+
// BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值。1 固定值
19+
return new BigInteger(1, md.digest()).toString(16);
20+
} catch (NoSuchAlgorithmException e) {
21+
e.printStackTrace();
22+
return null;
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
String text = "123456";
28+
System.out.println(MD5Lower(text));
29+
}
30+
31+
}

src/main/resources/application.yml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@ spring:
77
driver-class-name: com.mysql.cj.jdbc.Driver
88
username: root
99
password: 123456
10+
thymeleaf:
11+
prefix: classpath:/templates/
12+
suffix: .html
13+
mode: HTML5
14+
encoding: UTF-8
15+
content-type: text/html
16+
cache: false
1017

1118
#静态资源配置
1219
resources:
1320
static-locations: classpath:static/
1421

15-
#thymeleaf模板引擎配置
16-
thymeleaf:
17-
prefix: classpath:/templates/
18-
suffix: .html
19-
mode: HTML5
20-
encoding: UTF-8
21-
content-type: text/html
22-
cache: false
23-
2422
mybatis:
2523
mapper-locations: classpath:mapper/*.xml
2624
type-aliases-package: com.niit.sms.bean
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3+
<mapper namespace="com.niit.sms.mapper.StudentMapper">
4+
5+
<!-- 验证登录信息是否正确 -->
6+
<select id="login" resultType="com.niit.sms.bean.Student">
7+
select * from tb_student where sno=#{sno} and password=#{password}
8+
</select>
9+
10+
</mapper>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3+
<mapper namespace="com.niit.sms.mapper.TeacherMapper">
4+
5+
<!-- 验证登录信息是否正确 -->
6+
<select id="login" resultType="com.niit.sms.bean.Teacher">
7+
select * from tb_teacher where tno=#{tno} and password=#{password}
8+
</select>
9+
10+
</mapper>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<h1>学生信息</h1>
9+
</body>
10+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<h1>年级信息</h1>
9+
</body>
10+
</html>

src/main/resources/templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<html lang="en" xmlns:th="http://www.thymeleaf.org">
33
<head>
44
<meta charset="UTF-8">
5-
<title>Title</title>
5+
<title>首页</title>
66
</head>
77
<body>
88

9-
<h1>HelloWorld</h1>
9+
<h1>首页</h1>
1010

1111
</body>
1212
</html>

0 commit comments

Comments
 (0)