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"; }
}

最新文章

  1. 作业七:团队项目——Alpha版本冲刺阶段-14
  2. HTML基础及一般标签
  3. php栈数据结构和括号匹配算法
  4. [Cocos2d-x For WP8]Layer 层
  5. db2icrt创建实例,提示主机名无效
  6. 传统认知PK网络认知 刚子扯谈烤串认知
  7. ZOJ1238 Guess the Number
  8. Linq之查询表达式语法详解
  9. python爬虫学习(2)__抓取糗百段子,与存入mysql数据库
  10. [yueqian_scut]蓝牙防丢器原理、实现与Android BLE接口编程
  11. 提高jQuery执行效率
  12. Java_io体系之BufferedWriter、BufferedReader简介、走进源码及示例——16
  13. Docker安装weblogic
  14. Android Studio(AS)--&gt;导入项目
  15. An error occurred while starting the application.
  16. SQL Server 各种时间业务处理
  17. 面向对象【day08】:静态方法、类方法、属性方法(九)
  18. 【转】void及void指针的深刻解析
  19. python - http请求带Authorization
  20. 安装nvm之后node不可用,“node”不是内部或外部命令,也不是可运行的程序或批处理文件(ng)

热门文章

  1. Oracle10g客户端连接远程数据库配置图解
  2. ubuntu安装nginx踩坑
  3. MongDB安装使用
  4. Mongo实战之数据空洞的最佳实践
  5. java基础之io流总结三:字节流读写
  6. php 读取和下载execl
  7. mybatis学习笔记 spring与mybatis整合
  8. Debian上SCST的设置
  9. SP1557 GSS2 - Can you answer these queries II
  10. 第八课 ROS的空间描述和变换