first to use Springmvc to be an entry-level HelloWorld program.
Create a new project, import the required Jar package, details are as follows:
Configuration 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/ spring-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>
Write Controller
Package Com.tgb.controller;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.modelandview;import Org.springframework.web.servlet.mvc.controller;public class Helloworldcontroller implements Controller {@ Overridepublic Modelandview HandleRequest (httpservletrequest request, httpservletresponse response) throws Exception { System.out.print ("---hellogghhyy---"); String hello= "Hello Ghy"; return new Modelandview ("/welcome", "result", Hello);}}
Configuration Spring - s Ervlet.xml
<bean name= "/test/helloworld" class= "Com.tgb.web.controller.HelloWorldController"/><bean id= " Viewresolver "class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property Name= "prefix" value= "/" ></property> <property name= "suffix" value= ". JSP" ></property>< /bean>
Write page
Let's analyze the implementation process below.
1, the client sends an HTTP request to the Web server, the Web server parses the HTTP request, If the request mapping path (specified in Web. xml) matches the Dispatcherservlet, the Web container will forward the request to Dispatcherservlet.
2. After receiving this request, Dipatcherservlet will find the processor (Handler) processing the request based on the requested information (including URL, HTTP method, request packet header and request parameter cookie, etc.) and handlermapping configuration.
3-4, Dispatcherservlet according to handlermapping find corresponding handler, the processing right to handler (handler will be specific processing to package), Then the specific handleradapter to the handler to make a specific call.
5, Handler will return a Modelandview () object to Dispatcherservlet after the data processing is completed.
6. Handler return Modelandview () just a logical view is not a formal view, Dispatchersevlet transforms a logical view into a real views view by Viewresolver.
7, dispatcher through the model to parse out the parameters in the Modelandview () and finally show the full view and return to the client.
this is Springmvc The working principle .
Springmvc is a simple and powerful front-end frame .
Learn how SPRINGMVC works