MVC framework of spring (XVI)

MVC framework tutorial

Spring web MVC framework provides a model view control architecture and components that can be used to develop flexible and loosely coupled web applications. The MVC pattern leads to the separation of different aspects of the application (input logic, business logic, and UI logic) while providing loose coupling between these elements.

DispatcherServlet

The spring web model view control (MVC) framework is designed around the dispatcherservlet, which is used to process all HTTP requests and responses. The request processing workflow of spring web MVC dispatcherservlet is shown in the following figure:

The following is the event sequence corresponding to the incoming HTTP request from the dispatcher servlet:

All the components mentioned above, namely handlermapping, controller and viewresolver, are part of webapplicationcontext, which is an extension of ApplicationContext with some additional features necessary for web applications.

Maven dependency:

Simple example:

(1) Write hellocontroller java

(2) Modify web xml

(3) Write helloweb servlet XML (Note: the file should be placed in the WEB-INF directory, otherwise it will not be found. Of course, you can also specify the file path)

a. Put it in the WEB-INF directory and it will be automatically found and installed

b. Put it in the Src / main / resource directory. The content is as follows

Add: the reason why this folder is called helloweb servlet Because the XML file name is prefixed with the web application name by default, followed by the servlet.

< URL pattern > there are several forms of regular matching interception paths?

The difference between / and / *: < URL pattern > / < / url pattern > will not match * JSP, i.e.: * JSP does not enter the dispatcherservlet class of spring< URL pattern > / * < / url pattern > will match * JSP, when the JSP view is returned, the dispatcher servlet class of spring will be entered again, resulting in no corresponding controller, so a 404 error will be reported.

In short, about web Tips on URL mapping of XML: < URL pattern > / < / url pattern > will match a path URL such as / login, and will not match a pattern of * Suffix URLs like JSP < URL pattern > / * < / url pattern > will match all URLs: path URLs and suffix URLs (including / login, *. JSP, *. JS and *. HTML)

URL pattern explanation:

1. 首先/这个是表示默认的路径,及表示:当没有找到可以匹配的URL就用这个URL去匹配。
2. 在springmvc中可以配置多个DispatcherServlet,比如: 配置多个DispatcherServlet有/和/*,先匹配的是/*这个
3. 当配置相同的情况下,DispathcherServlet配置成/和/*的区别
<一>/:使用/配置路径,直接访问到jsp,不经springDispatcherServlet
<二> /*:配置/*路径,不能访问到多视图的jsp

当我在客户端调用URL:/user/list然后返回user.jsp视图,当配置的是/:DispathcherServlet拿到这个请求然后返回对应的controller,
然后依据Dispather Type为Forward类型转发到user.jsp视图,即就是请求user.jsp视图(/user/user.jsp),此时Dispather没有拦截/user/user.jsp,
因为此时你配置的是默认的/,就顺利的交给ModleAndView去处理显示了。
当配置的是/*:DispathcherServlet拿到这个请求然后返回对应的controller,然后通过Dispather Type通过Forward转发到user.jsp视图,
即就是请求user.jsp视图(/user/user.jsp),此时Dispather已经拦截/user/user.jsp,Dispatcher会把他当作Controller去匹配,没有匹配到就会报404错误。
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>