Spring MVC configuration-anatomy

Source: Internet
Author: User

1. Configure the distributor
Dispatcherservlet is the entry of spring MVC
All requests entering the spring web are passed through dispatcherservlet
You need to register dispatcherservlet in Web. xml
<Servlet-Name> dispathercontext </servlet-Name>
Org. springframework. Web. servlet. dispatcherservlet
<Load-on-startup> 1 </load-on-startup>

Spring will try to read the configuration file when loading dispatcherservlet
The default configuration file is located in the same path of Web. XML as the registered servlet.
Keep up with the servlet registration name-servlet. xml
For example, if the above servlet Registration Name Is dispatchercontext, the default
Configuration File Name bit: dispatcherContext-servlet.xml

Of course, you can also specify that the configuration file needs to set initialization parameters when registering the servlet.
<Param-Name> contextconfiglocation </param-Name>
<! -- Configuration file name -->

After registering dispatcherservlet, you should also specify a URL template with Spring Processing
<Servlet-Name> dispathercontextservlet </servlet-Name>
<URL-pattern> *. DO </url-pattern>

In this way, all request. Do processing will be handled by spring.

As the program grows bigger and bigger, the <bean> in the configuration file becomes more and more complex and the relationship becomes more and more complex.
Difficult to maintain. In this case, you should consider splitting the configuration file into multiple
To enable spring to read these configuration files and detect their changes
Register the configuration file Reader
For servlet 2.3 and later standards and web containers support listeners
You can register a listener in Web. xml.
Org. springframework. Web. Context. contextloaderlistener
For versions earlier than servlet 2.3 that do not support listeners, You need to register the Servlet
<Servlet-Name> contextloader </servlet-Name>
Org. springframework. Web. Context. contextloaderservlet
<Load-on-startup> 1 </load-on-startup>
After the configuration file reader is successfully registered, you need to set the configuration file list
Set the global parameter contextconfiglocation
Set to configuration file list separated by commas note path
<Param-Name> contextconfiglocation </param-Name>
<! -- Classpath *: specifies that the compiled class directory is the same as the SRC root directory in IDE. -->
Classpath *: hibernatecontext. xml
2. Configure the handing Responder (handlermapping)
When dispatcherservlet receives the request, it will ask handlermapping
Controller corresponding to the request
Beannameurlhandlermapping spring default ing responder searches for controllers to process requests based on the <bean> name attribute
<Bean id = "urlmapping"
Class = "org. springframework. Web. servlet. handler. beannameurlhandlermapping"/>

Simpleurlhandlermapping is the most common ing responder in spring. You can set its ings to obtain more flexible
Controller Search Mechanism
<Bean id = "urlmapping"
Class = "org. springframework. Web. servlet. handler. simpleurlhandlermapping">
<Property name = "mappings">
<Prop key = "/<! -- URL template -->. Do "> <! -- Controller <bean> id --> </prop>

Commonspathmaphandlermapping applies the new features after jdk1.5 and maps them using annotations in the controller.
Add @ org. springframework. Web. servlet. handler. commonsattributes. pathmap ("/path. Do") to the main class ")
<Bean id = "urlmapping"
Class = "org. springframework. Web. servlet. handler. Metadata. commonspathmaphandlermapping"/>
3. Configure the Controller)
After dispatcherservlet receives the request, it uses handlermapping to ask the processing controller corresponding to the request.
Find the desired <bean> Processing request in the dispatcherContext-servlet.xml
When beannameurlhandlermapping is used to map the responder, each processing controller should ensure the name of <bean>
The property is the requested URL template, for example:
<Bean name = "/home. Do" class = "<! -- Package name -->. homecontroller "/>
When simpleurlhandlermapping ing responder is used, each processing controller should ensure the ID of <bean>
The property corresponds to mappings in simpleurlhandlermapping, for example:
<Bean id = "homeaction" class = "<! -- Package name -->. homecontroller "/>

<Bean id = "urlmapping"
Class = "org. springframework. Web. servlet. handler. simpleurlhandlermapping">
<Property name = "mappings">
<Prop key = "/Hello. Do"> homeaction </prop>

When the commonspathmaphandlermapping ing responder is used
* @ Org. springframework. Web. servlet. Handler.
Commonsattributes. pathmap ("/Hello. Do ")
Public class hellocontroller
Extends abstractcommandcontroller {

4. Configure viewresolver)
<Bean id = "viewresolver" class = "org. springframework. Web. servlet. View. internalresourceviewresolver">
<Property name = "viewclass" value = "org. springframework. Web. servlet. View. jstlview"/>
<Property name = "prefix" value = "/WEB-INF/JSP/"/>
<Property name = "suffix" value = ". jsp"/>

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.