Brief introduction Springmvc
SPRINGMVC is a dispatcherservlet-based MVC framework that each request is first visited by Dispatcherservlet, Dispatcherservlet is responsible for forwarding each request to the corresponding Handler,handler processing and then returning the corresponding view and model, the returned view and model can not be specified, That is, you can return only the model or only the view or none of the returns.
Dispatcherservlet is inherited from the HttpServlet, since SPRINGMVC is based on Dispatcherservlet, then we first configure the Dispatcherservlet, So that it can manage what we want it to manage. The HttpServlet is declared in the Web. xml file.
let's start with an example and learn Spring MVC .
Create a new dynamic Web website.
introduce the corresponding Jar Package ( Springmvc the package and Spring package is exactly the same)
Configuration Web. xml:
<!--Spring MVC configuration-<servlet> <servlet-name>SpringMVC</servlet-name> < Servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!-- You can customize the location and name of the Servlet.xml configuration file, which defaults to the Web-inf directory, with the name <!--[<servlet-name>]- Servlet.xml, such as Spring-servlet.xml- <!--<init-param>--> <!--<param-name> Contextconfiglocation</param-name>--> <!--<param-value>/web-inf/spring-servlet.xml</ param-value> Default- <!--</init-param>- <load-on-startup>1</load-on-startup > </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name > <url-pattern>/</url-pattern> </servlet-mapping>
S Pring MVC -servlet.xml Configuration
The name Spring-servlet is because the <servlet-name> tag in the above Web. XML is Spring (<servlet-name>spring</servlet-name >), plus the "-servlet" suffix to form the Spring-servlet.xml file name, if changed to Springmvc, the corresponding file name is Springmvc-servlet.xml.
S Pring MVC -servlet.xml
<!--enable Spring MVC annotations-- <mvc:annotation-driven></mvc:annotation-driven><!-- Set the jar package for the class that uses the annotations-- <context:component-scan base-package= "Com.hjy.testSpringMVC" ></context: component-scan><!--The path resolution to the turn page. Prefix: prefix, suffix: suffix--><bean id= "viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" prefix "value="/"> </property><property name= "suffix" value= ". JSP" ></property></bean>
Dispatcherservlet will use some special beans to process request requests and generate corresponding view returns.
With regard to the return of the view, the controller is only responsible for returning a value, and then exactly what view is returned, is controlled by the view parser, the common view resolver in the JSP is Internalresourceviewresovler, it will require a prefix and a suffix
In the view parser above, if the controller returns BLOG/INDEX, the view after parsing through the view parser is/blog/index.jsp.
The following is an example of how the annotations are not used:
Web. XML configuration does not need to be changed.
Controller :
public class Helloworldcontroller implements controller{@Overridepublic Modelandview HandleRequest ( HttpServletRequest arg0,httpservletresponse arg1) throws Exception {System.out.println ("-------Hello World---------" ); return new Modelandview ("/welcome");}}
Modify S Pring MVC -servlet.xml Configuration
<!--enable Spring MVC annotations-- <!--<mvc:annotation-driven></mvc:annotation-driven> Set the jar package for the class that uses the annotations <context:component-scan base-package= "Com.hjy.testSpringMVC" ></context: component-scan>--><bean name= "/test1/helloworld" class= "Com.tgb.web.controller.HelloWorldController" ></bean><!--The path resolution to the turn page. Prefix: prefix, suffix: suffix--><bean id= "viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" prefix "value="/"> </property><property name= "suffix" value= ". JSP" ></property></bean>
Visit: Http://localhost:8080/SpringMVC/test1/helloworld
The above is the use of Spring MVC A simple example of implementation!
Additional: Modify the configuration file path and name
The default way of storing the configuration file address
To modify a path:
Modify Web. XML :
<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*:config/ springmvc-servlet.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>
Execution Result:
Summarize:
These are just an application Spring MVC Simple Introduction, is the introduction of it, but also to explain, for a strange knowledge first we do not need to see how deep, simple learning, summed up, in fact, it is so one thing, sometimes, will and will not, understand and do not understand the difference is just that you have not touched, used only!
SPRINGMVC Introductory Study (i) Environment Construction + Example Demonstration