SpringMVC configuration web. xml file (list common configurations), springmvcweb. xml

Source: Internet
Author: User

SpringMVC configuration web. xml file (list common configurations), springmvcweb. xml

Common web. xml configuration

1. Spring framework solves the string encoding problem: FilterCharacterEncodingFilter(Filter-name)
2. Configure the listener in web. xmlContextLoaderListener(Listener-class)
The function of ContextLoaderListener is to automatically assemble the configuration information of ApplicationContext when the Web container is started. Because it implements the ServletContextListener interface, the listener is configured in web. xml, and the implementation method is executed by default when the container is started.
3. Deploy the xml file of applicationContext:ContextConfigLocation(Param-name under context-param)
4. DispatcherServlet is a front-end controller and is configured in the web. xml file. To intercept matching requests, the Servlet intercept matching rules should be defined by yourself. The intercepted requests should be distributed to the Target Controller for processing according to the rules specified.
DispatcherServlet(Servlet-name, servlet-class, init-param, param-name (contextConfigLocation), param-value)
During DispatcherServlet initialization, the framework looks for a configuration file named [servlet-name]-servlet. xml under the WEB-INF folder of the web application to generate the bean defined in the file

(1) configure the filter.

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <web-app version = "2.5" 3 xmlns = "http://java.sun.com/xml/ns/javaee" 4 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 5 xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <! -- Solve the Chinese garbled characters in the strings sent from the page in the Spring framework. 9. The Spring framework provides a filter for us.CharacterEncodingFilter10 This filter is used to filter Each browser request, and then adds a function not available in the parent class to it to process character encoding. 11.EncodingUsed to set the encoding format,ForceEncodingUsed to set whether to ignore the request. getCharacterEncoding () method, setTrueOverwrite the previous encoding format and set it as needed. --> 12 <filter> 13 <filter-name>CharacterEncodingFilter</Filter-name> 14 <filter-class>Org. springframework. web. filter. CharacterEncodingFilter</Filter-class> 15 <init-param> 16 <param-name>Encoding</Param-name>// Specifies a specific character set17 <param-value>UTF-8</Param-value> 18 </init-param> 19 <init-param> 20 <param-name>ForceEncoding</Param-name>// True: encoding is used no matter whether a character set is specified in the request. false: If a character set is specified in the request, encoding is not used.21 <param-value>True</Param-value> 22 </init-param> 23 </filter> 24 <filter-mapping> 25 <filter-name>CharacterEncodingFilter</Filter-name> 26 <url-pattern>/*</Url-pattern> 27 </filter-mapping>

Configuration node details:

<Filter>: Specify a filter

<Filter-name>: Specify a name for the filter. The element cannot be blank.

<Filter-class>: Specifies the full qualified class name of the filter.

<Init-param>: Specify initialization parameters for the filter.

<Param-name>: Name of the specified parameter

<Param-value>: Value of the specified parameter

<Filter-mapping>: Used to set the resource intercepted by a Filter.

<Filter-name>: Used to set the Registration Name of the filter. The value must be the name of the filter declared in the <filter> element.

<Url-pattern>: Set the Request Path intercepted by the filter (the url style associated with the filter)

(2) Servlet Configuration

1 <servlet> 2 <servlet-name>DispatcherServlet</Servlet-name> // specify a servlet name 3 <servlet-class>Org. springframework. web. servlet. DispatcherServlet</Servlet-class> // specify the full servlet class Path 4 <init-param> 5 <param-name>ContextConfigLocation</Param-name> // initialize parameter name 6 <param-value>Classpath: spring/dispatcher-servlet.xml</Param-value> // initialization parameter value 7 </init-param> 8 <load-on-startup>1</Load-on-startup> // specify the servlet loading sequence 9 when the web Container starts. </servlet> 10 <servlet-mapping> 11 <servlet-name>DispatcherServlet</Servlet-name> // servlet name 12 <url-pattern>/</Url-pattern> // ing path 13 </servlet-mapping>

Configuration node details:

1) Using SpringMVC to configure DispatcherServlet is the first step. DispatcherServlet is a Servlet, so you can configure multiple DispatcherServlet

2) DispatcherServlet is a front-end controller and is configured in the web. xml file. To intercept matching requests, the Servlet intercept matching rules should be defined by themselves. The intercepted requests should be distributed to the Target Controller (the Action we wrote) for processing according to the rules.

3)<Servlet>:During DispatcherServlet initialization, the framework looks for a configuration file named [servlet-name]-servlet. xml under the WEB-INF folder of the web application to generate the bean defined in the file.

4)<Servlet-name>: Servlet name

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

6)<Param-name>: Initialization parameter name

7)<Param-value>: Initialization parameter value

8)<Load-on-startup>: Specify the Servlet loading sequence when the Web application starts.

9)<Url-pattern>: Ing path

(3) Specify the welcome page configuration

 

1 <welcome-file-list> 2 <welcome-file>Hello. jsp</Welcome-file> // specify the 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>100</session-timeout></session-config>

 

(6) configuration error page

1) Configure error-page through error codes

 

1 <! -- Configure the system to jump to the error handling page NotFound. jsp --> 2 <error-page> 3 <error-code> when a 404 error occurs.404</Error-code> 4 <location>/NotFound. jsp</Location> 5 </error-page>

 

2) Configure error-page through the exception type

<! -- Configure the system to jump to the error handling page error. jsp --> <error-page> <exception-type> when java. lang. NullException (NULL pointer exception) occurs.Java. lang. NullException</Exception-type> <location>/Error. jsp</Location> </error-page>

Finally, you can configure a simple web. xml to implement the SpeingMVC framework.

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5"  3     xmlns="http://java.sun.com/xml/ns/javaee"  4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  6     http://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>10   <servlet>11       <servlet-name>SpringMVC</servlet-name>12       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>13       <load-on-startup>1</load-on-startup>14   </servlet>15   <servlet-mapping>16       <servlet-name>SpringMVC</servlet-name>17       <url-pattern>/</url-pattern>18   </servlet-mapping>19 </web-app>

If you have any suggestions, please read them!
If you think it is helpful to read this article, click"Recommendation"Button, your"Recommendation"It will be my greatest motivation for writing! You can also chooseFollow me], You can easily find me!
The copyright of this article is shared by the author and the blog Park. source URL: copyright!

 

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.