Building Spring web applications (1)

1. Configure DispatcherServlet

1. Use java to configure DispatcherServlet in Servlet

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
 * Note: This configuration method is an alternative to the traditional web.xml approach. It can only work properly if it is deployed to a server that supports Servlet3.0 (that is, Tomcat7 or higher)
 *
 * As long as the AbstractAnnotationConfigDispatcherServletInitializer class is extended, the container will automatically discover this class and use it to configure the servlet context.
 * AbstractAnnotationConfigDispatcherServletInitializer will create both DispatcherServlet and ConetxtLoaderListener
 */
public class MySpringDispatcherServlet extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?> [] getRootConfigClasses() {
         // The class annotated with @Configuration brought back by this method will be used to configure the beans in the application context created by ConetxtLoaderListener. 
        System. out .println( " pppppppppp " ​​);
         return  new Class<?>[] {RootConfig.class } ;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        System.out .println ( " hhhhhhhh " );
         // Specify the configuration class
         // When DispatcherServlet starts, it will create the Spring application context and load the beans declared in the configuration file or configuration class. Here, DispatcherServlet is required to use the definition when loading the context Beans in the WebConfig configuration class. return new Class <?>[] {WebConfig.class };
         
    }

    // The function of this method is to map one or more paths to the DispatcherServlet, where "/" is mapped, which means that it is the default servlet of the application and it will process all requests entering the application. 
    @Override
     protected String[] getServletMappings() {
        System.out.println("vvvvvvvvvv");
        // 将DispatcherServlet 映射到 "/"
        return new String[] {"/"};
    }

}

WebConfig class

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 // Enable SpringMVC 
@ComponentScan(basePackages={ " config " , " controller " , " data " }) // Enable component scanning to specify the scan path 
public  class WebConfig extends WebMvcConfigurerAdapter {

    // Configure the view resolver 
    @Bean
     public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
         // The prefix attribute specifies the prefix of the resource page. Here, the resource location is directly located under /WEB-INF/ of the project 
        resolver.setPrefix( " /WEB-INF/views/ " );
         // suffix The property specifies the suffix of the resource page 
        resolver.setSuffix( " .jsp " );
         // exposeContextBeansAsAttributes property to expose the beans in the current spring environment as request attributes to the page.
        // exposedContextBeanNames property to limit the list of names of spring beans that can be exposed to the page. 
        resolver.setExposeContextBeansAsAttributes( true );
         return resolver;
    }
    
    // Configure the processing of static resources 
    public  void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
         // The function of this method is that DispatcherServlet forwards requests for static resources to the default Servlet in the Servlet container 
        configurer.enable();
    }
}

RootConfig class

@Configuration
 // Enable automatic scanning to specify multiple package paths 
@ComponentScan(basePackages={ " com " }, excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class ) })
 public  class RootConfig {
    
}

 

Then we write a controller 

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

@Controller   // declared as a controller 
@RequestMapping( " homepage " )   
//@RequestMapping({"/", "homepage"}) //Map handler to "/" and homepage
public class HomeController { // @RequestMapping(value="/",method=RequestMethod.GET) // Handle request processing at the GET request method level for "/" // @RequestMapping(method=RequestMethod.GET) @RequestMapping(value= " / register " ,method= RequestMethod.GET) public String home() { System.out.println("ssssssssssssssssssssss");System.out .println ( " llllllllllllll " ); return " home " ; //This is resolved to /WEB-INF/views/home.jsp } }

 

Problems encountered:

1. The home page cannot be accessed Reason: The default home page access is placed in the WebContent directory instead of the WEB-INF directory

2. The default access page works according to the order configured in web.xml

E.g:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

If there are both index.html and index.jsp in the WebContent directory, index,html will be displayed

3. When configuring spring mvc with pure java, put the relevant jar package in the WEB-INF/lib path, otherwise the implementation class of AbstractAnnotationConfigDispatcherServletInitializer will not be loaded.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325089477&siteId=291194637