首先我先来说说我一个小白对springboot的理解吧,它是基于spring的基础,自带服务器引擎,简化了maven的引入,提供多种功能的一个框架。下图是springboot官网的一些说明。
## 构建项目
1.使用工具idea构建项目,file-new project-spring initializr自动构建项目,选择你需要引入的配置,web、mysql、mybatis.(如果只是spingboot web项目的话,就直接选web即可)。 2.如果需要引擎模板,或者一些webjar的话可以在pom.xml中引入相关依(webjar官网有依赖文档)
例如:引入thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
## 书写拦截器
1.在templates写新建success.html、login.html.
login.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="utf-8" />
<title>login</title>
<link rel="stylesheet" th:href="@{/css/login.css}" />
<script type="text/javascript" th:src="@{/js/jquery-3.3.1.min.js}" ></script>
</head>
<body>
<div class="login">
<span class="title">酒店管理系统</span>
<span class="tip">▼</span>
<p style="color:red" th:text="${msg}" th:if="${msg!=null}"> </p>
<form id="form" action="/login" method="post">
<div class="text"><label>用户名:</label><input type="text" name="user" id="name" value=""></div>
<div class="text"><label>密 码:</label><input type="password" name="pass" id="password" value="" /></div>
<button type="submit" onclick="checkLogin()">登 陆</button>
</form>
</div>
<script type="text/javascript">
if('${result}'=='0'){
alert("用户名或密码错误");
}
function checkLogin() {
$(function(){
var name=$("#name").val();
var pass=$("#password").val();
if($("#name").val()==""||$("#name").val()==null||$("#password").val()==""||$("#password").val()==null){
alert("用户名和密码不能为空");
}else{
$("#form").submit(function(e){
});
}
});
}
</script>
</body>
</html>
success.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功</h1>
<tr th:text="${i.name}" th:each="i:${a}">
</tr>
</body>
</html>
2.映射:
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class Hello {
//映射路径
@RequestMapping({"/","/login.html"})
public String input(){
//返回跳转的templates的路径
return "login";
}
@RequestMapping("login")
//接收请求以及做出响应,如果用户密码正确则更新session中的值。
public String login(@RequestParam("user") String username, HttpSession session, @RequestParam("pass") String password){
if(username.equals("admin")&&password.equals("123456")){
session.setAttribute("user",username);
return "redirect:/success";
}else{
return "redirect:/login.html";
}
}
@RequestMapping("success")
public String inputs(Map<String,Object> a){
a.put("hello","yzx");
a.put("name","yzx1");
a.put("name2","yzx1");
return "success";
}
}
3.书写拦截器:
package com.example.demo.controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class loginfiter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String name=(String) request.getSession().getAttribute("user");
if(name==null) {
//登录验证不通过提示信息并返回首页
request.setAttribute("msg","没有权限");
request.getRequestDispatcher("/login.html").forward(request,response);
return false;
}else{
//放行请求
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
4.注册拦截器
package com.example.demo.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class config extends WebMvcConfigurerAdapter {
@Bean
public loginfiter getSecurityInterceptor() {
return new loginfiter();
}
/*public void addView(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
}*/
@Override
//将之前的拦截器loginfiter加入到容器中。
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/login.html","/","/login");
}
}
网友评论