springboot导入bootstrap以及用户增删改查

修改语法

将html中添加 xmls:th=“http://www.thymeleaf.org”>

<html lang="en" xmlns:th="http://www.thymeleaf.org">

超链接,href变为th:href,url使用@开头,其中/代表项目的根目录,一般从static或thymeleaf目录开始写目录

<link th:href="@{/qinjiang/css/qinstyle.css}" rel="stylesheet">

去除thymeleaf的缓存,在application.properties

spring.thymeleaf.cache=false

图片,加上th:@{}

<img th:src="@{/qinjiang/img/bootstrap-soid.svg}" alt="">

image-20220123211530134

建虚拟目录

image-20220123213700238

server.servlet.context-path=/sheepbotany

相当于在所有目录前添加sheepbotany目录,此时访问时需要添加

原来:

image-20220123213325928

后:

image-20220123213424544

2:实现中英文切换

1:确保file-Encoding为UTF-8

image-20220123214043173

在resources目录下新建i8n目录,然后新建login.properties与login_zh_CN.properties此时会出现资源包‘login’文件夹,将此属性文件添加到资源包。

image-20220123220025754

点击加号

image-20220123220047753

image-20220123220115162

打开login.properties,点击资源包

image-20220123220511524

进行绑定

spring.message.basename=i18n.login

image-20220123221841703

并把页面相应的sign in改为th:text="#{login.tip}"

<h1 class="header" th:text="#{login.tip}">登录</h1>

修改按钮

[[#{login.remember}]]

中英文切换链接

<a th:href="@{/index.html(l='zh_CN')}">中文</a>
<a th:href="@{/index.html(l='en_US')}">英文</a>

接口:

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");

    }

    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocalResolver();
    }

}
public class MyLocalResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获得请求中的语言参数
        String language=request.getParameter("l");
        Locale locale=Locale.getDefault();//语言参数不存在时则使用默认
        //如果请求的链接携带了国际化参数
        if(!StringUtils.isEmpty(language)){
            //zh_CN
            String[] split=language.split("_");
            //国家,地区
            locale=new Locale(split[0],split[1]);
        }

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}

3:实现登录功能

1:action中存放映射地址,login表示映射主体,返回字符串OK

@ResponseBody的作用其实是将java对象转为json格式的数据。

@Controller
public class LoginController {
    @RequestMapping("/user/login")
    @ResponseBody
    public String login(){
        return "OK";
    }
}
<form th:action="@{/user/login}">

image-20220126205925411

随便输入账号和密码

image-20220126205451624

2:添加参数

<input type="password" name="password">
<input type="text" placeholder="Username" name="username">
<p style="color: palevioletred" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

登录:

当账号不为空而且密码为123456

@Controller
public class LoginController {
    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username,@RequestParam("password") String password,
    Model model){

        if(!StringUtils.isEmpty(username)&&"123456".equals(password)){
            return "index";
        }else{
            model.addAttribute("msg","你登录失败了噢");
            return "login";
        }

    }
}

image-20220127132100199

3:修改html映射地址

在MyMvcConfig类中写入:

registry.addViewController("main.html").setViewName("index");

image-20220127103755103

image-20220127133404238

image-20220127133641842

4:拦截器

接收前端的账号和密码

判断账号和密码时设置请求loginUser与参数

在拦截器中进行接收请求与参数

判断请求返回true或者false

在config类中重写拦截器方法进行接收,是true时允许通过,同时添加不是true也可以通过的部分。

(如果没有检测到session则弹出)

public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录成功后,应该有用户的session
        Object loginUser=request.getSession().getAttribute("loginUser");
        if(loginUser==null){
            //没有登录
            request.setAttribute("msg","没有权限,请先登录");
            request.getRequestDispatcher("/login.html").forward(request,response);
            return false;
        }else {
            return true;
        }

    }


}

MyMvcConfig类加入

public void addInterceptors(InterceptorRegistry registry){
    registry.addInterceptor((new LoginHandlerInterceptor())).
            addPathPatterns("/**").excludePathPatterns(
                    "/login.html","/","/user/login","/css/*","/js/**","/img/**");
}

5:实现登录后显示用户姓名

<h3>[[${session.loginUser}]]</h3>

image-20220128204054771

6:登录后对用户进行遍历

用户表见博客:

springboot使用HashMap的数据库_lllwky的博客-CSDN博客

跳转的链接:

image-20220129213921459

image-20220129214704026

Controller类

package com.example.demo2.Controller;

import com.example.demo2.Dao.EmployeeDao;
import com.example.demo2.pojo.Employee;
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 java.util.Collection;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    @RequestMapping("/emp/list")
    public String list(Model model){
        Collection<Employee> employees =employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }
}

访问二级目录下的html

image-20220129214516447

因为return的是emp/list,所以访问的请求显示的也是:

image-20220129214758678

<table>
    <tr>
        <th>id</th>
        <th>lastName</th>
        <th>email</th>
        <th>gender</th>
        <th>department</th>
        <th>birth</th>
    </tr>
    <tr th:each="emp:${emps}">
        <td th:text="${emp.getId()}"></td>
        <td>[[${emp.getLastName()}]]</td>
        <td th:text="${emp.getEmail()}"></td>
        <td th:text="${emp.getSex()}"></td>
        <td th:text="${emp.getDepartment().getDepartmentName()}"></td>
        <td th:text="${emp.getBirth()}"></td>
    </tr>
</table>

时间和性别进行修改:

<td th:text="${emp.getSex()==0?'':''}"></td>
 <td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>

image-20220130141310378

7:实现增加用户

跳转到add页面

1:文件位置

此处/emp意思为当点击链接时发送emp请求

image-20220131234444749

get到请求并传参到前端:

@GetMapping("/emp")//get请求
public String toAddpage(Model model){
    //查出所有部门信息
    Collection<Department> departments=departmentDao.getDepartments();
    model.addAttribute("departments",departments);
    return "emp/add";
}

image-20220131235440669

add.html页面

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form th:action="@{/emp}" method="post">
<table>
  <tr>
    <td><lable>LastName:</lable></td>
    <td><input type="text" name="lastName"></td>
  </tr>
  <tr>
    <td><lable>Email:</lable></td>
    <td><input type="text" name="email"></td>
  </tr>
  <tr>
    <td><lable>Gender:</lable></td>
    <td><input name="sex" type="radio" value="0"></td>
    <td><input name="sex" type="radio" value="1"></td>
  </tr>
  <tr>
    <td><lable>department</lable></td>
    <td>
    <select name="department.id" id="">
      <option th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}"></option>
    </select>
    </td>
  <tr>
  <td><lable>Birth</lable></td>
  <td><input type="text" name="birth"></td>
  </tr>
<tr>
  <td><button type="submit">提交</button></td>
</tr>
</table>
</form>
</body>
</html>

添加用户时,发出post请求,请求名依然是emp,此时在controller中进入post方法,并将employee返回给前端。

@PostMapping("/emp")
public String addEmp(Employee employee){
    System.out.println(employee);
    employeeDao.save(employee);
    //添加的操作
    return "redirect:/emp/list";
}

在这里插入图片描述

8:实现修改用户

image-20220204215012895

前端代码:

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form th:action="@{/updateEmp}" method="post">
  <input type="hidden" name="id" th:value="${emp.getId()}">
<table>
  <tr>
    <td><label>LastName:</label></td>
    <td><input th:value="${emp.getLastName()}" type="text" name="lastName"></td>
  </tr>
  <tr>
    <td><label>Email:</label></td>
    <td><input th:value="${emp.getEmail()}" type="text" name="email"></td>
  </tr>
  <tr>
    <td><label>Gender:</label></td>
    <td><input th:checked="${emp.getSex()==0}" name="sex" type="radio" value="0"></td>
    <td><input th:checked="${emp.getSex()==1}" name="sex" type="radio" value="1"></td>
  </tr>
  <tr>
    <td><label>department</label></td>
    <td>
    <select name="department.id" id="">
      <option th:selected="${dept.getId()==emp.getDepartment().getId()}" th:each="dept:${departments}"
              th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}"></option>
    </select>
    </td>
  <tr>
  <td><lable>Birth</lable></td>
  <td><input th:value="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm')}"  type="text" name="birth"></td>
  </tr>
<tr>
  <td><button type="submit">修改</button></td>
</tr>
</table>
</form>
</body>
</html>

部门的选择:emp的id与部门的id相等

后端部分

@GetMapping("/emp/{id}")
public String toUpdate(@PathVariable("id")Integer id,Model model){
    Employee employee=employeeDao.getElementById(id);
    model.addAttribute("emp",employee);
    Collection<Department> departments=departmentDao.getDepartments();
    model.addAttribute("departments",departments);

    return "emp/update";
}

@PostMapping ("/updateEmp")
public String updateEmp(Employee employee){
    employeeDao.save(employee);
    return "redirect:/emp/list";

}

效果:

image-20220204215544455

9:实现删除用户

使用的方法:

employeeDao.delete(id);

前端页面的list.html加上:

<a th:href="@{/delEmp/{id}(id=${emp.getId()})}">删除</a>
@GetMapping("/delEmp/{id}")
public String deleteEmp(@PathVariable("id")Integer id){
    employeeDao.delete(id);
    return "redirect:/emp/list";
}

10:写自己喜欢的404页面

只需要在templetes页面中新建error文件夹,然后加上404.htmlimage-20220205214811699

此时输入不存在的页面会弹出:

image-20220205214856285

11:注销登录

html中写入

<a th:href="@{/user/logout}"><i class="bullhorn icon"></i> 注销登录</a>

在Controller中写入:

@RequestMapping("/user/logout")
public String logout(HttpSession session){
    session.invalidate();
    return "redirect:/login.html";

}

ger id){
employeeDao.delete(id);
return “redirect:/emp/list”;
}


### 10:写自己喜欢的404页面

只需要在templetes页面中新建error文件夹,然后加上404.html[外链图片转存中...(img-gq6kCgkK-1644071037101)]



此时输入不存在的页面会弹出:

[外链图片转存中...(img-VhdBjYsb-1644071037102)]

### 11:注销登录

html中写入

```html
<a th:href="@{/user/logout}"><i class="bullhorn icon"></i> 注销登录</a>

在Controller中写入:

@RequestMapping("/user/logout")
public String logout(HttpSession session){
    session.invalidate();
    return "redirect:/login.html";

}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# 简介 这个项目是一个简单的教务查询系统,其中有三种角色:管理员,教师,学生。三种角色都有相应的权限,其中: * 管理员:对课程、学生信息、教师信息等进行增删改查,修改个人密码,修改学生和教师的密码 * 教师:可以查看自己教授的课程,查询选修该课程的学生,对选修该课程的学生进行打分,修改个人密码 * 学生:可以进行选课,查看已修课程,查看已选课程,退选课程,修改个人密码 # 使用技术 Web框架:SpringBoot ORM框架:Mybatis 安全框架:Shiro 分页插件:PageHelper 连接池:SpringBoot自带的HiKariCP 日志:SpringBoot自带的LogBack 前端框架:Bootstrap # 在线预览 [http://23188i98c9.51mypc.cn:28070](http://23188i98c9.51mypc.cn:28070) * 登录账户 * 管理员账户:admin * 老师账户:1001 * 学生账户:10001 * 密码均为:123 # 快速上手 ### 1、运行环境和所需工具 * 编译器:IntelliJ IDEA * 项目构建工具:Maven * 数据库:Mysql、Redis * JDK版本:jdk1.8 * Tomcat版本:Tomcat8.x ### 2、初始化项目 * 在你的Mysql中,创建一个数据库名称为 EducationalManagementSystem 的数据库,并导入我提供的 .sql 文件。 * 进入src/main/resources修改application.properties配置文件,把数据库登录名和密码,改为你本地的。 * 使用 IntelliJ IDEA 导入项目,选择Maven项目选项,一路点击next就行。 * 在 IntelliJ IDEA 中,运行SpringBoot启动类。 * 运行 ![login](png/login.png) # 功能模块介绍 ### 1、登录模块功能 使用Shiro权限管理框架,实现登录验证和登录信息的储存,根据不同的登录账户,分发权限角色,对不同页面url进行角色设置 ### 2、管理员模块功能 管理员可对课程、学生信息、教师信息等进行增删改查,修改个人密码,修改学生和教师的密码 * 课程管理:当课程已经有学生选课成功时,将不能删除 * 学生管理:添加学生信息时,其信息也会添加到登录表中 * 教师管理:添加教师信息时,其信息也会添加到登录表中 * 账户密码重置:修改学生和教师的密码,不需要输入旧密码 * 修改密码:修改自己的密码,需要输入旧密码 ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值