Use Java configuration to complete the construction of SpringMVC

SpringMVC request process


Write picture description here

Briefly describe the request process:
1. The request arrives at the front controller DispatcherServlet, and its role is to forward the request to the corresponding controller (controller)
2. The DispatcherServlet maps the processor to get the specific controller, and then forwards the request
3. The controller processes the information, and then packages the model (in the model is the information that needs to be displayed to the foreground from the background), and indicates the name of the view used to render the output
4. DispatcherServlet is using the view parser (the controller does not directly transmit Give the name of the view resolver, but through Servlet, this is to decouple the controller and the view resolver) to match a specific view for rendering

Build SpringMVC

One, configure DispatcherServlet

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * 
 * @author CELINE
 *
 */
//扩展自Abstrac~Initializer的任意类,都会自动地配置Dispatcher-Servlet和Spring应用上下文
//spring的应用上下文会位于程序的Servlet上下文之中
public class SpittrWebAppInitializer  extends
        AbstractAnnotationConfigDispatcherServletInitializer {
    
    

   @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    //映射“/”,表示会使用默认的Servlet
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

}

Write picture description here

The SpittrWebAppInitializer class we created inherits
AbstractAnnotationConfigDispatcherServletInitializer. Any class that inherits from this class will automatically configure Dispatcher-Servlet and Spring application context* , but the real configuration context is the WebApplicationInitializer class

There are usually two application contexts in Spring Web. One is the Spring application context. This context is loaded by DispatcherServlet and corresponds to the getServletConfigClasses() method above. The other context is not spring and needs to be created through ContextLoaderListerner. The corresponding method is getRootConfigClasses()

AbstractAnnotationConfigDispatcherServletInitializer will create both DispatcherServlet and ContextLoaderListener

二、WebConfig

package spitter.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter {
    
    

    /**
     * 配置JSP视图解析器,他会查找jsp文件,在查找的时候
     * 他会在视图名称上加一个特定的前缀和后缀
     * home的视图——解析成为/WEB-INF/views/home.jsp
     * @return
     */
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver=
                new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    /**
     * 通过调用enable方法,我们要求DispatcherServelet将
     * 对静态资源的请求转发到Servlet容器中的默认的Servlet上,
     * 不是DispatcherServelet本身处理
     * @param configurer
     */
    public void configureDefaultServleHandling(
            DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }
}

Three, write the controller

package spitter.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class HomeController {
    
    

    @RequestMapping(value="/",method=RequestMethod.GET)
    public String home(){
        //视图名为home
        //配置的InternalResourceViewResolver方式,
        //home会被解析成"/WEB-INF/views/home.jsp"
        return "home";
    }
}

Fourth, define a simple JSP page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Spitter</title>
</head>

<body>
    <h1>Welcome to Spittr!!!</h1>
</body>

</html>

Run it to see the effect:

Write picture description here

Success!!~~That's All

Guess you like

Origin blog.csdn.net/cd18333612683/article/details/79130683