First, preface:
Hello everyone, Spring3 mvc is a very good MVC framework, since it was released in the 3.0 release, now there are more and more teams choosing SPRING3 MVC. SPRING3 MVC structure is simple, should that sentence simple is beautiful, and he is strong and flexible, performance is also excellent.
The official download URL is: http://www.springsource.org/download (this article uses the Spring 3.0.5 version)
Struts2 is also an excellent MVC architecture, with many advantages such as good structure. But here is the disadvantage, Struts2 because the use of value stack, OGNL expression, struts2 tag library, etc., will lead to the performance of the application degradation. STRUTS2 multi-layer interceptors, multi-instance action performance are very good. You can refer to an article I wrote about the comparison between spring MVC and Struts2 and Servlets http://elf8848.iteye.com/admin/blogs/698217
Advantages of SPRING3 MVC:
1, Spring3 MVC Learning difficulty is less than the struts2,struts2 of unnecessary functions too much. Oh, of course this is not the deciding factor.
2, Spring3 MVC is easy to write a good performance program, Struts2 to be careful to write good performance program (refers to the MVC part)
3, Spring3 MVC flexibility is you can not imagine, Spring's extensibility is legendary, Spring3 MVC certainly will not lag behind, not because of the use of the MVC framework and feel any restrictions.
STRUTS2 's many advantages: a little ... (hehe, isn't it unfair.) )
Many articles at the beginning of the total to blow some cows, to attract the reader's attention, the reader's appetite to adjust, so that everyone is interested in looking back. There is no exception to this article. But make sure you don't regret it when you see it.
second, the core class and interface:
Let's take a look at some important interfaces and classes. Now do not know what they are do not matter, first mixed face ripe, for later know them to play a foundation.
Dispatcherservlet--Front Controller
Handlermapping interface--mapping of processing requests
Implementation classes for the Handlermapping interface:
simpleurlhandlermapping mapping a URL to a controller through a configuration file
defaultannotationhandlermapping maps a URL to a controller class via annotations
Handleradapter interface--mapping of processing requests
The Annotationmethodhandleradapter class, through annotations, maps a URL to the method of the Controller class
Controller interface--controllers
Since we have used the @controller annotation, the class that added the @controller annotation annotation can assume the responsibility of the Controller (Action),
So we don't have to use this interface.
Handlerinterceptor interface--Interceptor
Without graphs, we implement this interface ourselves to complete the work of the Interceptor.
Implementation class for the Viewresolver interface
The Urlbasedviewresolver class passes a configuration file and gives a view name to the
Internalresourceviewresolver class, than the above class, joined the JSTL support
View interface
Jstlview class
Localresolver interface
Handlerexceptionresolver interface--Exception handling
Simplemappingexceptionresolver Implementation Class
Modelandview class
No diagram.
Third, core flowchart
This picture is my personal painting, there is not rigorous place, we deal with the look. It's better than not watching.
iv. Description of Dispatcherservlet
With spring MVC, configuring Dispatcherservlet is the first step.
Dispatcherservlet is a servlet, so you can configure multiple dispatcherservlet.
The Dispatcherservlet is the predecessor controller, which 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.
The "so-and-so rule": is different from the implementation class of which handlermapping interface you are using.
First example: XML code <web-app> <servlet> <servlet-name>example</servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>*.form</url-pattern> </servlet-mapping> </web-app>
<load-on-startup>1</load-on-startup> is the boot sequence that enables the servlet to start with the SERVLETP container.
<url-pattern>*.form</url-pattern> will intercept the *.form end of the request.
<servlet-name>example</servlet-name> This servlet's name is example, and can have multiple dispatcherservlet, which are distinguished by their name. Each dispatcherservlet has its own Webapplicationcontext context object. At the same time save the ServletContext and request object, about key, explained later.
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.
Second Example: XML code <servlet> <servlet-name>springmvc</ servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> contextconfiglocation</param-name> < param-value>classpath*:/springmvc.xml</param-value> </ init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Indicates the file name of the profile, does not use the default profile name, and uses the Springmvc.xml configuration file.
Where <param-value>**.xml</param-value> can use a variety of writing
1, do not write, use the default value:/web-inf/<servlet-name>-servlet.xml
2. <param-value>/WEB-INF/classes/springMVC.xml</param-value>
3. <param-value>classpath*:springMVC-mvc.xml</param-value>
4. Multiple values are separated by commas
servlet intercept matching rules can be self-defined, and the servlet intercepts which URLs are appropriate.
When mapping to @requestmapping ("/user/add"):
1, interception *.do, such as:/user/add.do, the disadvantage: all the URLs to. Do end. Does not affect access to static files.
2, interception/app/*, such as:/app/user/add, the disadvantage: the URL of the request must contain/app, @RequestMapping ("/user/add") do not need to include/app.
3, interception/, for example:/user/add, disadvantage: The jpg,js,css static file access is also blocked not normal display. There is a solution behind it.
4, intercept/*, you can go to the action, but forwarded to the JSP is intercepted again, can not access to the JSP.
v. Description of the parent context
If you use the listener listener to load the configuration, the listener listener is typically used in struts+spring+hibernate projects. as follows