Go to springmvc-web.xml configuration file

1. The Spring framework solves the problem of string encoding: filter  CharacterEncodingFilter (filter-name)  2. Configure the listener ContextLoaderListener (listener-class) 
in web.xml The function of the ContextLoaderListener is to automatically assemble the configuration information of the ApplicationContext when the Web container is started. Because it implements the ServletContextListener interface, the listener is configured in web.xml and the method it implements will be executed by default when the container is started. 3. Deploy the xml file of applicationContext: contextConfigLocation (param-name under context-param)  4. DispatcherServlet is the front controller, which is configured in the web.xml file. To intercept matching requests, the Servlet interception and matching rules must be defined by themselves, and the intercepted requests are distributed to the target Controller (the Action we wrote) for processing according to certain rules. DispatcherServlet (servlet-name, servlet-class, init-param, param-name(contextConfigLocation), param-value)  During the initialization process of DispatcherServlet, the framework will look for a file named [servlet- name]-servlet.xml configuration file, the bean defined in the generated file




<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  

    <!-- How to solve the encoding problem of the string passed from the page in the Spring framework?
    Let's take a look at the filter CharacterEncodingFilter that the Spring framework provides us with  
     This filter is filtered for each browser request, and then adds a function that the parent class does not have on it, namely processing character encoding.  
      Where encoding is used to set the encoding format, forceEncoding is used to set whether to ignore the request.getCharacterEncoding() method, and set to true to force the previous encoding format to be overwritten. -->  
    <filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    <!-- When using Spring in the project, there is no BeanFactory in the applicationContext.xml configuration file. If you want to directly reference the beans managed by the Spring container in the class file in the business layer, you can use the following methods-->  
    <!--1, configure the listener ContextLoaderListener--> in web.xml  
    The function of <!--ContextLoaderListener is to automatically assemble the configuration information of the ApplicationContext when the Web container is started. Because it implements the ServletContextListener interface, the listener is configured in web.xml and the method it implements will be executed by default when the container is started.  
    The ContextLoader class is associated with the ContextLoaderListener, so the entire loading configuration process is completed by the ContextLoader.  
    its API description  
    The first paragraph states that ContextLoader can be generated by ContextLoaderListener and ContextLoaderServlet.  
    If you look at the API of ContextLoaderServlet, you can see that it is also associated with the ContextLoader class and it implements the HttpServlet interface  
    In the second paragraph, ContextLoader creates a class like XmlWebApplicationContext, which implements the interface WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->  
    BeanFactory so that all beans in spring are created by this class  
     IUploaddatafileManager uploadmanager = (IUploaddatafileManager) ContextLoaderListener.getCurrentWebApplicationContext (). GetBean ("uploadManager");
     -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!--2, deploy the xml file of applicationContext-->  
    <!--If you do not write any parameter configuration information in web.xml, the default path is "/WEB-INF/applicationContext.xml,  
    The name of the xml file created in the WEB-INF directory must be applicationContext.xml.  
    If you want to customize the file name, you can add the context parameter of contextConfigLocation to web.xml:  
    Specify the corresponding xml file name in <param-value> </param-value>. If there are multiple xml files, they can be written together and separated by ",".  
    You can also use wildcards in applicationContext-*.xml, such as applicationContext-ibatis-base.xml in this directory,  
    ApplicationContext-action.xml, applicationContext-ibatis-dao.xml and other files will be loaded together.  
    The ContextLoader class is associated with the ContextLoaderListener, so the entire loading configuration process is completed by the ContextLoader. -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring/applicationContext.xml</param-value>  
    </context-param>  

    <!--If your DispatcherServlet intercepts "/", in order to achieve the REST style, all requests are intercepted, then the access to static files such as *.js, *.jpg will be intercepted at the same time. -->  
    <!--Scheme 1: Activate Tomcat's defaultServlet to process static files -->  
    <!--Write it in front of DispatcherServlet, let defaultServlet intercept the request first, so that the request will not enter Spring, I think the performance is the best. -->  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.css</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.swf</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.gif</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.jpg</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.png</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.js</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.html</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.xml</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.json</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.map</url-pattern>  
    </servlet-mapping>  
    <!--Using Spring MVC, configuring DispatcherServlet is the first step. DispatcherServlet is a Servlet, so you can configure multiple DispatcherServlet-->  
    <!--DispatcherServlet is the front controller, which is configured in the web.xml file. To intercept matching requests, Servlet interception and matching rules must be defined by themselves, and the intercepted requests are distributed to the target Controller (the Action we wrote) for processing according to certain rules. -->  
    <servlet>  
        <servlet-name>DispatcherServlet</servlet-name><!--During the initialization of DispatcherServlet, the framework will look for a configuration file named [servlet-name]-servlet.xml in the WEB-INF folder of the web application , the beans defined in the generated file. -->  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <!--Indicates the file name of the configuration file, instead of using the default configuration file name, the dispatcher-servlet.xml configuration file is used. -->  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <!--Where <param-value>**.xml</param-value> A variety of writing methods can be used here-->  
            <!--1, do not write, use the default value: /WEB-INF/<servlet-name>-servlet.xml-->  
            <!--2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>-->  
            <!--3、<param-value>classpath*:dispatcher-servlet.xml</param-value>-->  
            <!--4, multiple values ​​are separated by commas-->  
            <param-value>classpath:spring/dispatcher-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup><!-- is the startup sequence, let this servlet start with the Servletp container. -->  
    </servlet>  
    <servlet-mapping>  
        <!--The name of this servlet is dispatcher, and there can be multiple DispatcherServlets, which are distinguished by their names. Each DispatcherServlet has its own WebApplicationContext context object. Saved at the same time in the ServletContext and the Request object.-->  
        <!--ApplicationContext is the core of Spring. We usually interpret Context as context. I think it's easier to understand with "container". ApplicationContext is "application container" :P, Spring puts beans in this In the container, when needed, use the getBean method to take it out -->  
        <servlet-name>DispatcherServlet</servlet-name>  
        <!--Servlet interception matching rules can be defined by yourself. When the mapping is @RequestMapping("/user/add"), for example, which URL is suitable for interception? -->  
        <!--1, intercept *.do, *.htm, for example: /user/add.do, this is the most traditional way, the simplest and most practical. Will not cause static files (jpg, js, css) to be blocked. -->  
        <!--2, intercepting /, for example: /user/add, can achieve the popular REST style. Many Internet-type applications like this style of URL. Disadvantages: It will cause static files (jpg, js, css) to be blocked and not displayed properly. -->  
        <url-pattern>/</url-pattern> <!-- will intercept requests with "/" in the URL. -->  
    </servlet-mapping>  

    <welcome-file-list><!--Specify welcome page-->  
        <welcome-file>login.html</welcome-file>  
    </welcome-file-list>  
    <error-page> <!--When the system has a 404 error, jump to the page nopage.html-->  
        <error-code>404</error-code>  
        <location>/nopage.html</location>  
    </error-page>  
    <error-page> <!--When java.lang.NullPointerException occurs in the system, jump to the page error.html-->  
        <exception-type>java.lang.NullPointerException</exception-type>  
        <location>/error.html</location>  
    </error-page>  
    <session-config><!--Session timeout configuration, in minutes-->  
        <session-timeout>360</session-timeout>  
    </session-config>  
</web-app>  

 

Reprinted from http://blog.csdn.net/u010796790

 

Guess you like

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