其他分享
首页 > 其他分享> > SpringMVC:完全注解开发。

SpringMVC:完全注解开发。

作者:互联网

异常处理器

基于配置的方式配置异常处理器

SpringMVC的配置文件中,创建异常处理器。

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.ArithmeticException">error</prop>
        </props>
    </property>
    <property name="exceptionAttribute" value="exception"></property>
</bean>

基于注解方式配置异常处理

@ControllerAdvice//这是一个Controller的派生注解。
public class ExceptionController {
	//这个注解的value属性的类型是Class类型的数组。
    @ExceptionHandler(value = {NullPointerException.class,ArithmeticException.class})
    public String testException(Model model, Exception exception){
        model.addAttribute("exception",exception);
        return "/error";
    }
}

基于注解方式代替web.xml和SpringMVC.xml

代替web.xml

//web工程的初始化类,用来代替web.xml
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

    //获取Spring的配置类
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConf.class};
    }
    //获取SpringMVC的配置类
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMVCConf.class};
    }
    //获取DispatcherServlet的配置
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    //配置Filter过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceResponseEncoding(true);
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        return new Filter[]{characterEncodingFilter,hiddenHttpMethodFilter};
    }
}

代替SpringMVC.xml

/*
 *1.扫描组件 2.视图解析器 3.view-controller
 *4.default-servlet-handler 5.mvc注解驱动
 *6.文件上传解析器 7.异常处理器 8.拦截器
 */
@Configuration
@ComponentScan(basePackages = "com.boerk")//1.扫描组件,同:<context:component-scan base-package="com.boerk.mvc"/>
@EnableWebMvc//5.开启MVC注解驱动,同:<mvc:annotation-driven />
public class SpringMVCConf implements WebMvcConfigurer {

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

    //3.配置视图控制器
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    //8.配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        TestInterceptor testInterceptor = new TestInterceptor();
        registry.addInterceptor(testInterceptor).addPathPatterns("/**");
        registry.addInterceptor(testInterceptor).excludePathPatterns("/");
    }

    //4.开放对静态资源的访问
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //2.配置生成模板解析器
    @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;
    }
}

标签:解析器,return,SpringMVC,templateResolver,开发,new,Override,注解,public
来源: https://www.cnblogs.com/Boerk/p/15876818.html