Getting Started with Spring MVC

Source: Internet
Author: User

Springmvc is also called Spring Web MVC, which is the framework of the presentation layer. Spring MVC is part of the spring framework and is released after Spring3.0. What Spring Web MVC is:

Spring Web MVC is a lightweight web framework that implements the request-driven type of the Web MVC design pattern based on Java, that is, the idea of using the MVC architecture pattern, decoupling the WEB layer from responsibility, and based on the request-driven approach is to use the request-response model, The purpose of the framework is to help us simplify our development, and Spring Web MVC also simplifies our daily web development.

Spring structure diagram:

  

As you can see, Spring MVC is part of the web.

Spring MVC request process:

       

Interpretation of the request process:

 Request-------->dispatcherservler (Central scheduler/Front Controller)-----> handlermapping (Processor mapper)------> Returns an execution chain-----> Turn the execution chain over to the Handleradaptor (processor adapter)-----> Dispatch Handler (processor)-----> Modelandview-----> Turn the results over to Handleradaptor first, Then forwarded by Handleradaptor to Dispatcherservler--------->dispatcherservler dispatch viewresolver (view parser)----> Give response to Dispatcherservler-------> Find View---->view (response)

Spring MVC's Introductory case:

1. Import the jar package:     

2. Configure the front-end controller in Web. XML:

    

   Configuration applicationcontext.xml:

     

 3. Processor Mapper (handlermapping), processor adapter (handleradaptor), are system-configured, programmers do not need to manually configure:

    

  4. Configure the Processor:

    

5. View resolver (Viewresolver) system default:

    

6. Prepare the View:

     

About Urlpattern:

Preferably paired with *.do.

Cannot match/* ===[cannot access dynamic resources such as *.jsp]

Best not to match/= = = [cannot access static resources]

Have to be paired with/

If Url-pattern is paired with/*, you cannot access the dynamic resource, which appears 404. If you have a/, you cannot access a static resource and the picture cannot be displayed.

cannot be paired with */* Reason:

Dispatcherservlet will request to a dynamic page, that is, a jump request to a JSP page as a normal controller request. The central scheduler calls the processor mapper to find the appropriate processor for it. Of course I can't find it. JSP page will report 404 error

Conclusion: Dynamic resources will be intercepted (404 pages appear)

The solution is as follows:

Scenario One: Use the Tomcat default servlet solution

Scenario two: Using MVC's Default-servlet-handler

The access request to the static resource is mapped through handlermapping to the default Servlet request processor Defaultservletrequesthandler object. The processor calls Tomcat's Defaultservlet to handle access requests for static resources. Of course, you need to introduce MVC constraints

Method Three: Use MVC's resource to solve

After Spring3.0.4, spring defines the processor Resourcehttprequesthandler that is specifically designed to handle requests for static resources. <mvc:resources/> tags are added to resolve static resource unreachable issues.

Location: directory of static Resources

Mapping: Request for a resource

Note: TOMCAT7 support is required

 Profile Development--beannameurlhandlermapping

<!--Configuring processor Mapper--   class= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean>

 Profile Development--simpleurlhandlermapping

class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >  <property name= " Mappings ">  <props>  <prop key="/hello.do ">firstController</prop>   <prop key=" /sayhello.do ">firstController</prop>  </props>  </property>     </bean>
class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > <property name= "UrlMap" >   <map> <entry key= "/hello.do" > <value>firstController</value> </entry>    <entry key= "/sayhello.do" > <value>firstController</value> </entry>   </map>   </property> </bean>

Spring Web MVC benefits:

1. Clear Role Division: Front Controller ( DispatcherServlet ), request to processor mapping (handlermapping), processor adapter (handleradapter), view resolver (viewresolver), The processor or page controller (Controller), Validator (Validator), the Command object (the object that the command request parameter binds to is called the Commands object), and the Form object (the object that forms object provides to the form to display and submit to is called the form objects).

2, the Division of labor is clear, and the extension point is quite flexible, can easily expand, although almost no need;

3, because the command object is a Pojo, without inheriting the framework-specific API, you can use the command object directly as a business object;

4. Seamless integration with other spring frameworks is not available in other web frameworks;

5, adaptable, through the Handleradapter can support any class as a processor;

6, can be customized, handlermapping, viewresolver and so can be very simple customization;

7, powerful data validation, formatting, binding mechanism;

8. Using the mock object provided by spring can make the Web Layer unit test very simple;

9, localization, the interpretation of the theme of support, making it easier for us to international and theme switching.

10, the powerful JSP tag library, make JSP writing easier.

As for learning as a vessel, a Artemisia cannot be slowed down.

-----Untitled

  

Getting Started with Spring MVC

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.