springboot默认是配置好了SpringMVC的,所以,如果之前有使用SpringMVC的经验的话,就可以直接在这里使用了,但是默认的配置往往难以满足使用的需求,那么如何来实现springboot中个性化配置的SpringMVC呢?
一、配置视图解析器
有的时候我们需要指定哪些URL对应跳转哪个页面,这个时候就需要视图解析器,配置代码如下
@Configuration
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");
registry.addViewController("/main.html").setViewName("dashboard");
}
@Configuration这个注解的作用是,声明这个类是一个配置类,并且添加这个bean到容器中
WebMvcConfigurer这个接口,是springboot自动配置SpringMVC的接口,我们在写自己的SpringMVC的配置类的时候需要实现这个接口(在springboot2.0之前不是要实现这个接口,而是要继承WebMvcConfigurerAdapter这个类,但是在springboot2.0之后这个类被标记为不推荐了,因为springboot2.0之后的Java版本起步为Java8,而Java8的一个很重要的特性是接口可以有自己的默认方法,所以WebMvcConfigurerAdapter这个类就不再被需要了)
方法addViewControllers,这个方法就是向视图解析器添加URL和视图的对应关系的了,registry.addViewController的参数是URL,registry.addViewController(“url”).setViewName(“index”)的参数是视图名称,springboot默认的视图后缀名称是html,可以通过在application.properties文件中”spring.mvc.view.suffix”属性来配置视图的后缀名称
二、配置拦截器
配置拦截器的时候也需要我们写一个拦截器,然后在一个配置类中将拦截器注册进去,拦截器实现HandleInterceptor接口,比如我们要拦截URL,判断当前是否为登录状态,那我们实现preHandle方法就行,拦截器的代码如下:
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;
}
return true;
}
}
然后写一个配置类注册拦截器
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/","/index.html","/login","/asserts/**","/webjars/**");
}
}
addPathPatterns参数是要拦截的URL,/**代表拦截所有的URL,excludePathPatterns的参数是不拦截的URL
三、配置过滤器
过滤器代码
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("FilterProcessing...");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
然后注册过滤器
@Configuration
public class MyServletConfig {
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return filterRegistrationBean;
}
}
四、配置监听器
监听器的代码
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("contextInitialized...web启动");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("contextDestroyed...web销毁");
}
}
注册监听器
@Configuration
public class MyServletConfig {
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<MyListener> myListenerServletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return myListenerServletListenerRegistrationBean;
}
}
本文详细介绍如何在SpringBoot中进行SpringMVC的个性化配置,包括视图解析器、拦截器、过滤器和监听器的设置方法。
1189

被折叠的 条评论
为什么被折叠?



