javaConfig下的springmvc配置

javaConfig下的springmvc配置

一、静态资源过滤

  • XML的配置

    <mvc:resources mapping="/**" location="/"/>
  • java配置

继承WebMvcConfigurationSupport 重写addResourceHandlers方法

@Configuration
@ComponentScan(basePackages = "org.javaboy")
public class SpringMVCConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/");
    }
}

二、视图解析器

  • xml的配置

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
  • java配置

    继承WebMvcConfigurationSupport 重写configureViewResolvers方法

    @Configuration
    @ComponentScan(basePackages = "org.javaboy")
    public class SpringMVCConfig extends WebMvcConfigurationSupport {
        @Override
        protected void configureViewResolvers(ViewResolverRegistry registry) {
            registry.jsp("/jsp/", ".jsp");
        }
    }

三、路径映射

有的时候,我们的控制器的作用仅仅只是一个跳转,里边没有任何业务逻辑,像这种情况,可以不用定义方法,可以直接通过路径映射来实现页面访问。

  • xml的配置

    <mvc:view-controller path="/hello" view-name="hello" status-code="200"/>
    
    <!--这行配置,表示如果用户访问 /hello 这个路径,则直接将名为 hello 的视图返回给用户,并且响应码为 200,这个配置就可以替代 Controller 中的方法。-->
  • java配置

    继承WebMvcConfigurationSupport 重写addViewControllers方法

    @Configuration
    @ComponentScan(basePackages = "org.javaboy")
    public class SpringMVCConfig extends WebMvcConfigurationSupport {
        @Override
        protected void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/hello3").setViewName("hello");
        }
    }

四、Json配置

SpringMVC 中,默认提供了 Jackson 和 gson 的 HttpMessageConverter ,分别是:MappingJackson2HttpMessageConverter 和 GsonHttpMessageConverter 。

正因为如此,我们在 SpringMVC 中,如果要使用 JSON ,对于 jackson 和 gson 我们只需要添加依赖,加完依赖就可以直接使用了。具体的配置是在 AllEncompassingFormHttpMessageConverter 类中完成的

如果开发者使用了 fastjson,那么默认情况下,SpringMVC 并没有提供 fastjson 的
HttpMessageConverter ,这个需要我们自己提供,如果是在 XML 配置中,fastjson 除了加依赖,还要显式配置
HttpMessageConverter,如下:

  • xml的配置

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
  • java的配置

    @Configuration
    @ComponentScan(basePackages = "org.javaboy")
    public class SpringMVCConfig extends WebMvcConfigurationSupport {
        @Override
        protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
            converter.setDefaultCharset(Charset.forName("UTF-8"));
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setCharset(Charset.forName("UTF-8"));
            converter.setFastJsonConfig(fastJsonConfig);
            converters.add(converter);
        }
    }

猜你喜欢

转载自www.cnblogs.com/XtsLife/p/11306825.html
今日推荐