注解配置SpringMVC

Posted L1998

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了注解配置SpringMVC相关的知识,希望对你有一定的参考价值。

 

 

 

 

 

 

package com.sun.config;


import com.sun.interceptors.TestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import java.util.List;
import java.util.Properties;


//代替SpringMVC的配置文件
/*
1.扫描组件
2.视图解析器
3.view-controller
4.default-servlet-handler
5.mvc注解驱动
6.文件上传解析器
7.异常处理
8.拦截器
 */
@Configuration//将当前类标识为一个配置类
@ComponentScan("com.sun")//扫描组件
@EnableWebMvc//MVC注解驱动
public class WebConfig implements WebMvcConfigurer {


    //default-servlet-handler
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
       configurer.enable();
    }


    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        TestInterceptor testInterceptor=new TestInterceptor();
        registry.addInterceptor(testInterceptor).addPathPatterns("/**");//拦截所有请求
    }



    //view-controller
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/hello").setViewName("hello");
    }



    //文件上传解析器
    @Bean
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver commonsMultipartResolver=new CommonsMultipartResolver();
        return commonsMultipartResolver;
    }


    //异常处理
    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        SimpleMappingExceptionResolver exceptionResolver=new SimpleMappingExceptionResolver();
        Properties properties=new Properties();
        properties.setProperty("java.lang.ArithmeticException","error");
        exceptionResolver.setExceptionMappings(properties);
        exceptionResolver.setExceptionAttribute("exception");
        resolvers.add(exceptionResolver);
    }

    //配置生成模板解析器
    @Bean
    public ITemplateResolver templateResolver(){
        WebApplicationContext   webApplicationContext= ContextLoader.getCurrentWebApplicationContext();
        //ServletContextTemplateResolver需要一个ServletContext作为构造参数,可通过webApplicationContext的方法获得
        ServletContextTemplateResolver  templateResolver=new ServletContextTemplateResolver(webApplicationContext.getServletContext());
        templateResolver.setPrefix("/WEB-INF/templates/");//设置前缀
        templateResolver.setSuffix(".html");//设置后缀
        templateResolver.setCharacterEncoding("UTF-8");//设置编码
        templateResolver.setTemplateMode(TemplateMode.HTML);
        return templateResolver;
    }


    //生成模板引擎并为模板引擎注入模板解析器
    @Bean
    public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver){

        SpringTemplateEngine templateEngine=new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        return templateEngine;
    }

    //生成视图解析器并为视图解析器注入模板引擎
    @Bean
    public ViewResolver viewResolver(SpringTemplateEngine templateEngine){
        ThymeleafViewResolver viewResolver=new ThymeleafViewResolver();
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setTemplateEngine(templateEngine);
        return viewResolver;
    }


}

 

以上是关于注解配置SpringMVC的主要内容,如果未能解决你的问题,请参考以下文章

springmvc enablewebmvc注解作用

SpringMVC注解配置

springmvc的注解配置

SpringMVC--常用注解(详解)

spring mvc 必须使用注解配置吗?可不可使用xml配置?求高手详解

第三次课:使用注解的方式配置springMVC