SpringBoot Learning (Five) to regain the Spring Web MVC

Web WVC core components

Package Explanation
Handler Mapping The request interceptor list mapped together handler to perform pre-processing and post-processing. Mapping based on certain criteria, specific criteria because HandlerMapping implementation-specific. The two main implementations are HandlerMapping RequestMappingHandlerMapping (annotated Method @RequestMapping support band) and the SimpleUrlHandlerMapping (the URI path explicit registration mode to the handler)
Handler Adapter Help DispatcherServlet call mapped to the handler requests, regardless of the actual how to call the handler. The main purpose is to protect HandlerAdapter DispatcherServlet these details.
HandlerExceptionResolver Parsing Exception
ViewResolver View String name resolved from the actual processing based on the name of the logical view of the process returns to present it to the response.
LocaleResolver, LocaleContextResolver To provide an international view
ThemeResolver

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.imooc.web"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

Annotation-driven Web WVC

  • Notes configuration @Configuration
  • Component activation @EnableWebMvc
  • Custom components WebMvcConfigurer (Interface, based java1.8)

xml annotation and replace

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                System.out.println("addInterceptors.preHandle");
                return true;
            }
        });
    }
}

Code above @ Configuration, @ EnableWebMvc, @ Bean relevant code replaces the following xml configuration

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

@EnableWebMvc comment

  • Configuring @EnableWebMvc notes will automatically load the configuration class @Configuration

  • In DelegatingWebMvcConfiguration no @ Bean, @ Bean class in WebMvcConfigurationSupport

Other popular Web MVC comment

  • Model Properties @ModelAttribute
  • Request header @RequestHeader
  • Cookie @CookieValue
  • Section notice @ControllerAdvice
  • Exception Handling
  • Check parameters

The relevant code

  • https://github.com/zhaimiya/springboot

Guess you like

Origin www.cnblogs.com/VVII/p/12462464.html