Spring Boot核心技术系统学习总结(五)web开发02
主要内容:
Restful配置
国际化
登陆&拦截器
公共页抽取
一.Restful配置(spring boot1.5.10.RELEASE+thymelef3.0.9.RELEASE)
1.pom.xml文件依赖加载
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
com.springboot.web
springboot-web
0.0.1-SNAPSHOT
springboot-web
Demo project for Spring Boot
1.8
UTF-8
3.0.9.RELEASE
2.2.2
UTF-8
org.springframework.boot
spring-boot-starter-web
log4j-api
org.apache.logging.log4j
spring-jcl
org.springframework
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-configuration-processor
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
spring-jcl
org.springframework
org.webjars
jquery
3.3.1
org.webjars
bootstrap
4.0.0
org.springframework.boot
spring-boot-starter-thymeleaf
org.slf4j
jcl-over-slf4j
1.7.26
org.slf4j
log4j-over-slf4j
1.7.25
org.springframework.boot
spring-boot-maven-plugin
2.applications.properties文件:
☆3.扩展Spring MVC功能配置:
package com.springboot.web.config;
import com.springboot.web.component.LoginHandlerInterceptor;
import com.springboot.web.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @author Jupiter
* @date 2019/2/27-22:58
* @description MVC扩展配置
*/
//使用WebmvcConfigurerAdapter可以扩展SpringMVC的功能
@Configuration
public class MyMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送 /atmars 请求来到 success
registry.addViewController("/atmars").setViewName("success");
}
//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //将组件注册到容器
public WebMvcConfigurerAdapter webMvcConfigurer(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("main.html").setViewName("dashboard");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html", "/", "/user/login");
}
};
return adapter;
}
}
二.国际化
1.步骤:
|- 在application.properties文件中添加:
# 国际化basename
spring.messages.basename=i18n.login
编写国际化配置文件,注意命名格式:
|- SpringBoot自动配置了国际化组件:MessageSourceAutoConfiguration类:
|- 在页面设置国际化key,两种取值方式
[[#{login.remember}]]
2.点击切换国际化实现
原理:切换区域信息对象Locale,SpringBoot默认配置源码:
实现步骤:
|- 新建配置类:MyLocaleResolver
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String language = request.getParameter("l");
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(language)){
String[] split = language.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
|- 注入自定义配置组件
|- 页面中在请求后追加请求参数:
中文
English
三、登陆&拦截器
1.自定义拦截器
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request.getSession().getAttribute("loginUser");
if (user == null ){
//未登录,返回登录页面
request.setAttribute("msg", "没有权限,请先登录");
request.getRequestDispatcher("index.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 {}
}
2.注册拦截器
//使用WebmvcConfigurerAdapter可以扩展SpringMVC的功能
@Configuration
public class MyMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送 /atmars 请求来到 success
registry.addViewController("/atmars").setViewName("success");
}
//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //将组件注册到容器
public WebMvcConfigurerAdapter webMvcConfigurer(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("main.html").setViewName("dashboard");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
//拦截需要用户登录后操作的页面,放行公共页面
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html", "/", "/user/login");
}
};
return adapter;
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
四、公共页抽取
1.thymeleaf页面公共元素抽取步骤:
1、抽取公共片段