[Spring MVC] learning notes -- learning from DispatcherServlet and springmvc
In the previous article, we introduced Servlet. This article mainly describes the DispatcherServlet used in MVC (inherited from HttpServlet ).
1. DispatcherServlet is declared in web. xml.
<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>/example/*</url-pattern> </servlet-mapping></web-app>
Note: We can also set in the code, specific reference http://docs.spring.io/spring/docs/4.0.6.RELEASE/spring-framework-reference/htmlsingle/#mvc-container-config
2. Some beans contained in DispatcherServlet
3. In addition to the bean provided by default in step 2, Spring MVC looks for[Servlet-name]-servlet. xmlFile (in step 1, you will find the example-servlet.xml file) to create the beans defined in this file.
If you need to modify the default bean, you can define it in it, and the default bean will be overwritten during loading.
4. You can configure the xml Path in step 3 through the initialization method in DispatcherServlet.
Alternatively, if you want to use a public xml file, you can configure context-param without setting the contextConfigLocation value.
<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>
5. To enable annotation under mvc[Servlet-name]-servlet. xmlAdd a row.
<?xml version=1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven /></beans>
Who can do SpringMVC -- DispatcherServlet description, a little more, trouble
With Spring MVC, configuring DispatcherServlet is the first step.
DispatcherServlet is a Servlet, so you can configure multiple dispatcherservlets.
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.
"XX rule": it varies depending on the implementation class of the HandlerMapping interface you use.
Let's take a look at the first example:
Xml Code
<Load-on-startup> 1 </load-on-startup> is the starting sequence, so that the Servlet can be started with the Servletp container.
<Url-pattern> *. form </url-pattern> intercepts the request at the end of *. form.
<Servlet-name> example </servlet-name> the Servlet name is example. Multiple dispatcherservlets can be created by name. Each DispatcherServlet has its own WebApplicationContext context object. The key in both the saved ServletContext and the Request object will be described later.
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.
Example 2:
Xml Code
Specifies the configuration file name. springMVC. xml is used instead of the default configuration file name.
<Param-value> **. xml </param-value> can be written in multiple ways.
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. Separate multiple values with commas
You can customize Servlet interception matching rules. Which URL is suitable for Servlet interception?
When @ ing is @ RequestMapping ("/user/add:
1. Intercept *. do, for example:/user/add. do. disadvantage: All URLs must end with. do. Access to static files is not affected.
2. Intercept/app/*, for example,/app/user/add. Disadvantages: The request url must contain/app, @ RequestMapping ("/user/add ") does not need to include/app.
3. Intercept/, for example,/user/add. Disadvantages: Access to jpg, js, and css static files is also blocked and cannot be displayed normally. There are solutions later.
4. Intercept/*. You can go to Action, but it is intercepted again when it is forwarded to jsp. You cannot access jsp .... Remaining full text>
How does spring mvc remove the-servlet after the configuration file?
"I just want to remove the servlet behind the jdbc-servlet.xml", to remove this, you need
<Servlet>
<Servlet-name> jdbc </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet> Add the namespace parameter of init-param, because the default <servlet-name>-servlet. xml in spring, you have to add parameters.