springboot
1碾盟、簡(jiǎn)單實(shí)現(xiàn)
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping()
public class HelloController {
@GetMapping("/hello")
@ResponseBody
public String Hello(){
return "hello";
}
}
2、Application.yml 給屬性賦值配置
類名上面加注解
@ConfigurationProperties(prefix = "dog")
public class Dog {}
賦值
dog:
name: "小狗"
age: "12"
3蜜徽、數(shù)據(jù)校驗(yàn) JSR300校驗(yàn)
Maven引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
@Validated
public class Dog {
@Email
private String name;
}
多環(huán)境配置
server:
port: 8081
spring:
profiles:
active: dev
---
server:
port: 8082
spring:
config:
activate:
on-profile: dev
thymeleaf簡(jiǎn)單應(yīng)用
controller里面的書寫
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
@Controller
public class indexController {
@RequestMapping("/test")
public String test(Model model){
model.addAttribute("msg","hellospringboot");
model.addAttribute("users", Arrays.asList("zhangxu","gabgab"));
return "test";
}
}
templates 里面書寫
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:text="${msg}">Test</div>
<h3 th:each="user:${users}" th:text="${user}"></h3>
</body>
</html>
springboot攔截器
1狸臣、新鍵連接配置腳本聪铺,在config文件夾下面,LoginHandlerInterceptor
package com.example.config;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//攔截器
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object loginUser=request.getSession().getAttribute("loginUser");
if (loginUser==null){
request.setAttribute("msg","沒有權(quán)限摄杂,請(qǐng)先登錄");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else {
return true;
}
}
}
2坝咐、MymvcConfig配置
package com.example.config;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Controller
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
//攔截器配置
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login","/css/*","/jd/**","/img/**");
}
}
3、controller里面設(shè)置session
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String login(@RequestParam("username")String username,
@RequestParam("password") String password,
Model model, HttpSession session){
if (!StringUtils.isEmpty(username)&&"123".equals(password))
{
session.setAttribute("loginUser",username);
return "redirect:/main.html";
}else {
model.addAttribute("msg","我錯(cuò)了");
return "index";
}
}
}
4析恢、session取值
[[${session.loginUser}]]
layui.css 找不到
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<link rel="stylesheet" th:href="@{/layui/css/layui.css}">
<script th:src="@{/layui/layui.js}"></script>