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>
<Servlet-Name> dispathercontext </servlet-Name>
<Servlet-class>
Org. springframework. Web. servlet. dispatcherservlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

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.
<Init-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>
<! -- Configuration file name -->
</Param-value>
</Init-param>

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

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.
<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>
 
For versions earlier than servlet 2.3 that do not support listeners, You need to register the Servlet
<Servlet>
<Servlet-Name> contextloader </servlet-Name>
<Servlet-class>
Org. springframework. Web. Context. contextloaderservlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
 
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
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>
/WEB-INF/dispatcherContext-servlet.xml,
<! -- Classpath *: specifies that the compiled class directory is the same as the SRC root directory in IDE. -->
Classpath *: hibernatecontext. xml
</Param-value>
</Context-param>
 
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">
<Props>
<Prop key = "/<! -- URL template -->. Do "> <! -- Controller <bean> id --> </prop>
</Props>
</Property>
</Bean>

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">
<Props>
<Prop key = "/Hello. Do"> homeaction </prop>
</Props>
</Property>
</Bean>

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"/>
</Bean>
 

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.