The development of spring MVC Simple Web application (i)
----------
Add dependencies, create a configuration file
An important part of Spring MVC is dispatcherservlet. As the name suggests, it is primarily responsible for distributing requests to the appropriate handlers for them to process these requests. This is the only servlet that needs to be configured in the Web deployment descriptor. It acts as a front-end controller for the spring MVC framework, and every Web request must pass it, so it can manage the entire request processing process.
When a Web request is sent to the Spring MVC application, Dispatcherserlvet receives the request and then organizes the different components configured in the spring's Web application context to process the request.
1. Adding dependencies
In order to develop a Web application with spring MVC, you must copy the Spring-webmvc.jar to the Web-inf/lib directory. If it's a MAVEN project, we
Can add dependencies, such as: XML code <dependency> <groupId>org.springframework</groupId> <artifactid>spri Ng-webmvc</artifactid> <version>3.0.5.RELEASE</version> </dependency>
2. Create a configuration file
For spring MVC applications, it's just a matter of defining a Dispatcherservlet instance acting as the spring MVC front-end controller. XML code <web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://java.sun.com/xmlns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <display-name>Court Reservation System</display-name> <servlet> < servlet-name>court</servlet-name> < servlet-class> org.springframewrok.web.servlet.dispatcherservlet </servlet-class> <load-on-startup>1</load-on-startup> </ servlet> <servlet-mapping> <servlet-name>court</servlet-name> <url-pattern>*.htm</url-pattern> </ servlet-mapping> </web-app>
Note that the URL extension is arbitrary, but in order to avoid disclosing the implementation technology to the Web application's users, you typically choose to use a htm,html or similar name as the extension.
The name of the servlet has an important purpose for dispatcherservlet to determine the spring MVC configuration file to load. By default, it looks for a configuration file with the servlet name plus the-servlet.xml suffix as the filename. You can also explicitly specify a configuration file in the contextconfiglocation servlet parameter. With the above settings, the court servlet will load the spring MVC configuration file court-servlet.xml by default. This file should be a standard spring bean configuration file.
In order for spring to load a configuration file other than Court-servlet.xml, the servlet listener Contextloaderlistener must be defined in Web.xml.
By default, it loads the bean configuration file/web-inf/applicationcontext.xml, but you can specify your own profile in the context parameter contextconfiglocation. You can also specify multiple profiles, using commas or spaces as delimiters. XML code <web-app ... > <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/court-service.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> ... </web-app>
Note that Contextloaderlistener loads the specified bean configuration file into the root application context, and each dispatcherservlet
The instance loads its configuration file into its own application context and references the root application context as its parent context. Therefore, the context that each Dispatcherserlvet instance loads can access or even overwrite the bean declared in the root application context (but in turn is not possible). However, contexts loaded by the Dispatcherservlet instance are not accessible to each other.