The Spring MVC configuration file Web. xml file is detailed

Source: Internet
Author: User

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" XM Lns: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 "> <!--in the spring framework, how to solve the problem of encoding strings from the page?      Let's take a look at the spring framework to provide us with a filter characterencodingfilter this filter is for each browser request filtering, and then added on top of the parent class does not have the function of processing character encoding. Where encoding is used to set the encoding format, forceencoding is used to set whether to ignore the request.getcharacterencoding () method, set to true to force overwrite the previous encoding format. -<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> </fi Lter-mapping> < When using spring in a!--project, there is no beanfactory in the Applicationcontext.xml configuration file, and the class in the business layer The files directly referencing spring container-managed beans can be used in the following ways-<!--1, configuring listeners in Web. contextloaderlistener--> <!-- The role of Contextloaderlistener is to automatically assemble ApplicationContext configuration information when the Web container is started.    Because it implements the Servletcontextlistener interface, when the Web. XML configures this listener, when the container is started, it executes the method it implements by default.    The Contextloader class is associated with Contextloaderlistener, so the entire load configuration process is done by Contextloader.    The first paragraph of its API description shows 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 the second segment of the interface, Contextloader creates a Xmlwebapplicationcontext such a class, it implements the interface is webapplicationcontext->configurablewebapplicationcontext-> Applicationcontext-> beanfactory so all the beans in spring are made up of this classTo create Iuploaddatafilemanager UploadManager = (iuploaddatafilemanager) contextloaderlistener.getcurrentwebapplication Context (). Getbean ("UploadManager");--> <listener> <listener-class>org.springframework.web.context . Contextloaderlistener</listener-class> </listener> <!--2, deploy ApplicationContext XML Files--<!--such as    If you do not write any parameter configuration information in Web. XML, the default path is "/web-inf/applicationcontext.xml, and the name of the file created under the Web-inf directory must be applicationcontext.xml. If you want to customize the file name, you can add the Contextconfiglocation this context parameter to the Web. xml: Specify the corresponding file name in <param-value> </param-value>    If there are multiple XML files, they can be written together and separated by the "," number.    You can also use wildcards like this applicationcontext-*.xml, for example, there are applicationcontext-ibatis-base.xml in that directory,    Applicationcontext-action.xml,applicationcontext-ibatis-dao.xml and other documents will be loaded together. The Contextloader class is associated with Contextloaderlistener, so the entire load configuration process is done by Contextloader. -<context-param> <param-name>contextConfigLocation</param-name> &LT;PARAM-VALUE&G T;classpath:spring/applicationcontext.xml</param-value> </context-param> <!--if your dispatcherservlet intercepts "/", in order to achieve restful style, All requests are intercepted, and access to static files such as *.js,*.jpg is intercepted. -<!--scenario One: Activate Tomcat's defaultservlet to handle static files--<!--to write in front of the Dispatcherservlet, let Defaultservlet intercept the request first, So the request will not enter spring, I think the performance is the best. -<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css& lt;/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-mapp ing> <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-p        attern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet-mapping> <ser    Vlet-name>default</servlet-name> <url-pattern>*.xml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.json</ur l-pattern> </servlet-mapping> <servlet-mapping> &LT;SERVLET-NAME&GT;DEFAULT&LT;/SERVLET-NAME&G        T <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 a front controller that is configured in the Web. xml file. Interception of matching requests, servlet intercept matching rules to be self-defined, the interception of requests, according to certain rules distributed to the target controller (we write action) to deal with. -<servlet> <servlet-name>DispatcherServlet</servlet-name><!--initialization process in Dispatcherservlet , the framework looks for a configuration file named [Servlet-name]-servlet.xml] in the WEB app's Web-inf folder, generating the bean defined in the file. -<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--means The file name of the configuration file, without using the default profile name, and using the Dispatcher-servlet.xml configuration file. -<init-param> <param-name>contextConfigLocation</param-name> <!--its <param-value>**.xml</param-value> here can be used in a variety of ways-<!--1, do not write, using the default value:/web-inf/<servlet-name> -servlet.xml--> <!--2, <param-value>/web-inf/classes/dispatcher-servlet.xml</param-value>--&gt            ; <!--3, <param-value>classpath*:d ispatcher-servlet.xml</param-value>--&Gt <!--4, multiple values separated by commas--<param-value>classpath:spring/dispatcher-servlet.xml</param-value> & Lt;/init-param> <load-on-startup>1</load-on-startup><!--is the boot sequence that enables the servlet to start with the SERVLETP container. -</servlet> <servlet-mapping> <!--the name of this servlet is dispatcher and can have multiple dispatcherservlet, which is by name to the district Points. Each dispatcherservlet has its own Webapplicationcontext context object. The .--> <!--ApplicationContext in the ServletContext and request objects is the core of spring, which we usually interpret as context, and I want to use the "container" To make it easier to understand, ApplicationContext is the "container of application": P,spring put the bean in this container, and, when needed, take the Getbean method out-<servlet-name> Dispatcherservlet</servlet-name> <!--servlet intercept matching rules can be self-defined, and when mapped to @requestmapping ("/user/add"), for example, What kind of URL is appropriate to intercept? -<!--1, intercept *.do, *.htm, for example:/user/add.do, this is the most traditional way, the simplest is the most practical. does not cause static files (JPG,JS,CSS) to be intercepted. -<!--2, intercept/, for example:/user/add, can achieve the now very popular rest style. Many Internet-type applications like this style of URLs. Cons: Causes 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-&LT;WELCOME-FILE&GT;LOGIN.HTML&L T;/welcome-file> </welcome-file-list> <error-page> <!--When the system comes up with 404 errors, jump to page nopage.html--> &L T;error-code>404</error-code> <location>/nopage.html</location> </error-page> < Error-page> <!--When the system appears java.lang.NullPointerException, jump to page error.html--> <exception-type> Java.lang.nullpointerexception</exception-type> <location>/error.html</location> </error-pa Ge> <session-config><!--Session timeout configuration, in minutes--<session-timeout>360</session-timeout> < /session-config></web-app>

The Spring MVC configuration file Web. xml file is detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.