SPRINGMVC Configuring the Web. xml file (listing common configurations)

Source: Internet
Author: User

Configuration of common Web. xml

1. Spring Framework solves string encoding problem: Filter characterencodingfilter(filter-name)
2. config listener contextloaderlistener(listener-class) in Web. XML
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.
3. Deploy ApplicationContext XML file:contextconfiglocation(Param-name under Context-param)
4, Dispatcherservlet is the front controller, 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 to deal with.
dispatcherservlet(Servlet-name, Servlet-class, Init-param, Param-name (contextconfiglocation), Param-value)
During the initialization of 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

(1) Configure filter filters

1<?xml version= "1.0" encoding= "UTF-8"?>2<web-app version= "2.5"3Xmlns= "Http://java.sun.com/xml/ns/javaee"4Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee6http//java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">7 8<!--solve the problem of Chinese garbled characters from pages in spring frame9 The Spring Framework provides us with filters  characterencodingfilter  Ten This filter is for each browser request to filter, and then added on top of the parent class does not have the ability to process character encoding.  OnewhichencodingTo set the encoding format,forceencodingUsed to set whether to ignore the request.getcharacterencoding () method, set totrueThe previous encoding format is enforced and is set as needed. - A<filter> -<filter-name>Characterencodingfilter</filter-name> -<filter-class>Org.springframework.web.filter.CharacterEncodingFilter</filter-class> the<init-param> -<param-name>encoding</param-name>//Used to specify a specific character set -<param-value>UTF-8</param-value> -</init-param> +<init-param> -<param-name>forceencoding</param-name>//true: Use Encoding;false regardless of whether the request specifies a character set: If request has a character set specified, the encoding is not used +<param-value>true</param-value> A</init-param> at</filter> -<filter-mapping> -<filter-name>Characterencodingfilter</filter-name> -<url-pattern>/* </url-pattern>  - </filter-mapping>  

Configuration node Details:

<filter> : Specify a filter

<filter-name> : Specify a name for the filter that cannot be empty

<filter-class> : Specify the full qualified class name of the filter

<init-param> : Specifying initialization parameters for filters

<param-name> : Specify the name of the parameter

<param-value> : Specifying the value of a parameter

<filter-mapping> : Used to set a resource that filter is responsible for intercepting

<filter-name> : Used to set the registered name of the filter, which must be a filter name declared in the <filter> element

<url-pattern> : Set the request path blocked by filter (the URL style associated with the filter)

(2) servlet configuration

1<servlet>2<servlet-name>Dispatcherservlet</servlet-name>//Specify a servlet name3<servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class>//Specifying the class full path of the servlet4<init-param>5<param-name>contextconfiglocation</param-name>//initialization parameter name6<param-value>Classpath:spring/dispatcher-servlet.xml</param-value>//Initialize parameter values7</init-param>8<load-on-startup>1</load-on-startup>//Specify the order in which servlets are loaded when the Web container starts9</servlet>Ten<servlet-mapping> One<servlet-name>Dispatcherservlet</servlet-name>//servlet Name A<url-pattern>/</url-pattern>//Map path -</servlet-mapping>

Configuration node Details:

1) Configuring Dispatcherservlet with SPRINGMVC is the first step, Dispatcherservlet is a servlet, so you can configure multiple Dispatcherservlet

2) Dispatcherservlet is the front controller, 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.

3) <servlet>: during Dispatcherservlet initialization, the framework looks for the Web-inf folder under the WEB app named [Servlet-name]-servlet.xml Configuration file that generates the bean defined in the file.

v) <servlet-name> : servlet name

5) <servlet-class> : servlet class full path

6) <param-name> : Initialize parameter name

7) <param-value> : Initialize parameter values

8) <load-on-startup> : Specifies the order in which the servlet is loaded when the Web App starts

9) <url-pattern> : Mapping path

(3) Specify the Welcome page configuration

1 <welcome-file-list>2     <welcome-file>hello.jsp</welcome-file>  // Specify Welcome page 3 </welcome-file-list>

(4) Listener configuration

1 <listener>2       <listerner-class>  Org.springframework.web.context.ContextLoaderListener</listener-class>3 </ Listener>

(5) Session Timeout configuration

<session-config>      <session-timeout></session-timeout></session-config>

(6) Configuration error page

1) Configure Error-page by error code

1 <!--configured when a 404 error occurs on the system, jump to error handling page notfound.jsp-->2 <error-page>3        < error-code>404</error-code>4        <location>/notfound.jsp</ Location>5 </error-page>

2) Configure Error-page by type of exception

<!--configured to jump to error handling page when System java.lang.NullException (that is, null pointer exception) Error.jsp--><error-page>       < exception-type>java.lang.NullException</exception-type>       <location>/ error.jsp</location></error-page>

Finally, a simple Web. XML is configured to implement the SPEINGMVC framework

1<?xml version= "1.0" encoding= "UTF-8"?>2<web-app version= "2.5"3Xmlns= "Http://java.sun.com/xml/ns/javaee"4Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee6http//java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">7<welcome-file-list>8<welcome-file>hello.jsp</welcome-file>9</welcome-file-list>Ten<servlet> One<servlet-name>Springmvc</servlet-name> A      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> -<load-on-startup>1</load-on-startup> -</servlet> the<servlet-mapping> -<servlet-name>Springmvc</servlet-name> -<url-pattern>/</url-pattern> -</servlet-mapping> +</web-app>

There is a need to add to your reading of the Daniel will give some advice!
If you feel that reading this article is helpful to you, please click " recommend " button, your " recommendation " will be my biggest writing motivation! Alternatively you can choose "Follow Me", can be very convenient to find me!
The copyright of this article is owned by the author and the blog Park, the source website: https://www.cnblogs.com/welcome you reprint, but without the author's consent, reprinted article must be in the article page obvious location to the author and the original text connection, otherwise reserve the right to pursue legal responsibility!

SPRINGMVC Configuring the Web. xml file (listing common configurations)

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.