Learn how SPRINGMVC works

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.