1、配置SpringBootApplication(對spring boot來說這是最基本)
package io.github.syske.springboot31;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
@Configuration
@SpringBootApplication
public class SpringBoot31Application {
public static void main(String[] args) {
//SpringApplication.run(SpringBoot31Application.class, args);
SpringApplication application = new SpringApplication(SpringBoot31Application.class);
//關閉banner,也可以通過在resouces文件夾下添加banner.txt替換banner,banner生成網站
// http://patorjk.com/software/taag/#p=testall&h=0&f=Chiseled&t=syske
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
}
}
2、創建配置類
完整配置
package io.github.syske.springboot31.config;
import io.github.syske.springboot31.formatter.DateFomaters;
import io.github.syske.springboot31.interceptor.SessionInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class Webconfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/user/login.html").setViewName("login");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SessionInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/login","/user/login.html","/bootsrap/**");
}
@Override
public void addFormatters(FormatterRegistry registry) {
//給當前的Spring容器中添加自定義格式轉換器.
registry.addConverter(new DateFomaters());
}
}
3、配置Controller
controller是在配置類中添加的
- 主要是針對一些僅需要返回頁面的Controller,如果需要model操作則不適用
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/user/login.html").setViewName("login");
}
- 主要是通過addViewController方法進行添加,方法中傳入的是訪問的路徑,上面配置的訪問路徑為:http://localhost:8080/user/login.html, setViewName方法設置的是返回的視圖名,我的配置對應的是login.html
- 方法內可以配置多個視圖模型
4、配置DateFomatters
自定義的convertor
package io.github.syske.springboot31.formatter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFomaters implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
if(!StringUtils.isEmpty(source)) {
try {
date = dateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
}
return date;
}
}
將自定義的轉化類添加到Spring boot配置中
@Override
public void addFormatters(FormatterRegistry registry) {
//給當前的Spring容器中添加自定義格式轉換器.
registry.addConverter(new DateFomaters());
}
5、配置攔截器(Interceptor)
自定義攔截器
package io.github.syske.springboot31.interceptor;
import org.springframework.web.servlet.mvc.WebContentInterceptor;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SessionInterceptor extends WebContentInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
Object username = request.getSession().getAttribute("username");
if(username != null) {
return true;
} else {
try {
request.getRequestDispatcher("/user/login.html").forward(request,response);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
}
將自定義的攔截器添加到Spring boot配置中
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SessionInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/login","/user/login.html","/bootsrap/**");
}
6、測試
登錄攔截測試,以及格式化打印
package io.github.syske.springboot31.controller;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.Date;
@Controller
public class LoginController {
@RequestMapping("/login")
public String login(String username, Date logindate, HttpSession session) {
if(!StringUtils.isEmpty(username)) {
session.setAttribute("username", username);
System.out.println(logindate);
return "index";
}
return "login";
}
}