springmvc configuration of javaConfig

springmvc configuration of javaConfig

A static resource filter

  • XML configuration

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

Methods inherited WebMvcConfigurationSupport rewrite addResourceHandlers

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

Second, the view resolver

  • xml configuration

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

    Methods inherited WebMvcConfigurationSupport rewrite configureViewResolvers

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

Third, the path mapping

Sometimes, our role is just a jump controller, inside there is no business logic, as in this case, you can not define the method can be implemented directly through the path mapped page views.

  • xml configuration

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

    Methods inherited WebMvcConfigurationSupport rewrite addViewControllers

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

Four, Json configuration

SpringMVC, the default provided Jackson and gson of HttpMessageConverter, namely: MappingJackson2HttpMessageConverter and GsonHttpMessageConverter.

Because of this, we SpringMVC, if you want to use JSON, for jackson and gson we just need to add a dependency, dependent on completion of the addition can be used directly. The configuration is done in a particular class AllEncompassingFormHttpMessageConverter

If the developer uses fastjson, then by default, SpringMVC does not provide fastjson of
HttpMessageConverter, that we need to provide their own, if it is in the XML configuration, in addition to adding fastjson dependence, but also explicitly configured
HttpMessageConverter, as follows:

  • xml configuration

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

    @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);
        }
    }

Guess you like

Origin www.cnblogs.com/XtsLife/p/11306825.html