springboot之springmvc自动配置原理,国际化

一、自配配置

1、官方介绍地址:

https://docs.spring.io/spring-boot/docs/1.5.22.BUILD-SNAPSHOT/reference/html/boot-features-developing-web-applications.html
27.1.1 Spring MVC auto-configuration
springboot自动配置好了springmvc
以下是springboot对springMVC的默认:
Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(view)),视图对象决定如何渲染(转发、重定向)
ContentNegotiatingViewResolver:组合所有的视图解析器
如何定制:我们可以自己给容器中添加一个视图解析器,自动的将其组合起来
编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc;
既保留了所有的自动配置,也能用我们扩展的配置:

@Configration
public class MyMvcConfig extends WebMvcConfigurerAdapter{
	@Override
	public void addVieweControllers(ViewControllerRegistry registry){
		//浏览器发送/hello请求到 index页面
		registry.addViewController("/hello").setViewName("index");
	}
}

全面接管SpringMvc

SpringBoot对SpringMVC的自动配置不需要流量,所有都是我们自己配置;所有的springmvc的自动配置都失效了,比如webjars,rosource等文件夹就扫描不到。我们需要在配置类中添加
@EnableWebMVC即可。一般情况下不使用这种方式。除非自己开发能力比较强。

国际化

自定义国际化语言解析器
//默认根据请求头的区域信息获取Locale进行国际化

//可以在链接上携带区域信息
pulic class MylocalResolver implements LocaleResolver{
	public	Locale resolveLocale(HttpServletRequest request){
		String l = request.getParameter("l");
		Locale locale = Locale.getDefault();
		if(!StringUtils.isEmpty(l)){
			String[] split=l.split("_");
			locale = new Local(split[0],split[1]);
		}
		return locale;
	}
	public void setLocale(HttpServletRequest var1, @Nullable HttpServletResponse var2, @Nullable Locale var3){}
}
//将自定义的语言解析器放入到自定义的配置解析器里面
@Configration
public class MyMvcConfig extends WebMvcConfigurerAdapter{
	@Override
	public void addVieweControllers(ViewControllerRegistry registry){
		//浏览器发送/hello请求到 index页面
		registry.addViewController("/hello").setViewName("index");
	}
	@Bean
	public LocalResolver localResolver(){
		return new MyLocalResolver();
	}
}
//点击链接切换国际化
//html页面中的代码
<a href= '' th:href="#{index.html(l='zh_CN')}">中文</a>
<a href= '' th:href="#{index.html(l='en_US')}">English</a>

注:html中使用th:text或者th:href的好处就是链接路径改变了,系统会自动加上

#热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

登录错误消息的显示

<p style = 'color:red' th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

注册拦截器

@Configration
public class MyMvcConfig extends WebMvcConfigurerAdapter{ 
//所有的WebMvcConfigurerAdapter组建都会一起起作用
@Bean //将组建注册到容器
  public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
	WebMvcConfigurerAdapter  adpter = new WebMvcConfigurerAdapter (){
		@Override
		public void addViewControllers(ViewControllerRegistry registry){
			registry.addViewController("/hello").setViewName("index");
		}
		//注册拦截器
		@Override
		public void addInterceptrors(InterceptorRegistry registry){
			//静态资源;springboot 已经做好了静态资源映射,所以就不用再做处理了
			registry.addInterceptor(new LoginHandlerInterceptor()).addpathpatter("/**")
			.excludePathPatters("index.html","/","user/login");
		}
	}
	}
}
发布了31 篇原创文章 · 获赞 1 · 访问量 5686

猜你喜欢

转载自blog.csdn.net/wjs040/article/details/93034440