Spring MVC Basic Learning

Source: Internet
Author: User

SPRINGMVC is a module of the spring framework that does not need to be integrated through the middle tier.
SPRINGMVC is a web framework based on MVC design pattern, mvc-model-view-controller:mvc the server side into M, V, c three components, each module is responsible for their own business, to achieve the separation of data, business logic and view display. Modularity of the software. The model processing data belongs to the business logic, the controller completes the data and the business dispatch, the view completes views display.

Several important components of the SPRINGMVC:
Dispatcherservlet: Front controller, mainly receives the front-end data, through the handlermapping to find the corresponding handler (through Handleradapter adaptation). Dispatcherservlet data is dispatched to the appropriate handler execution. Handler (i.e., instance of controller in SPRINGMVC) execution gets Modelandview, returned to Dispatcherservlet. Then Dispatcherservlet calls Viewresolver for view resolution, Viewresolver parsing is done to get view back to Dispatcherservlet, and finally dispatcherservlet render data to JSP /jstl and so on returned to the customer.
Handlermapping Mapper: Finds handler based on the requested URL.
Handleradapter adapter: Through the adapter dispatcherservlet you do not need to implement all handler calling rules, just perform a call to Handleradapter. Follow specific rules (Handleradapter required rules) to perform handler
Processor handler (requires siege lion development): Write handler according to Handleradapter requirements to do, so that the adapter can go to the correct execution Handlerhandler. is the back-end controller of the Dispatcherservlet Front controller, under the control of Dispatcherservlet handler to the specific user request processing. Since handler involves specific user business requests, the siege lions are generally required to develop according to business requirements Handler
The View parser viewresolver (no siege lion development), provided by the framework: parse the view and resolve it to a true view based on the logical view name. Viewresolver is responsible for generating the results of the view view, the view resolver first according to the logical view name to the physical view name is a specific page address, then generate the View object, and finally render the view to show the processing results through the page to the user.
Views view (Requires siege lion development jsp ...)

SPRINGMVC development of XML configuration methods:
1. Configuring the front-end controller (dispatcherservlet) in Web. xml
<servlet>
<!--Configuring the front-end controller (dispatcherservlet)--
<!--configuration Name&class--
<!--<init-param> is a map of the SPRINGMVC configuration file in Web. XML--
<!--<servlet-mapping> give all of the. Action end URLs to Dispatcherservlet resolution--
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--contextconfiglocation: Specifies the load location of the SPRINGMVC configuration, and if not specified, the default
Download Web-inf/servlet name-servlet.xml (springmvc-servlet.xml)
-
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--Load-on-startup: Indicates that the servlet starts with the service;
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
The first type: *.action or *.do, accessed with an. Action or *.do end is parsed by Dispatcherservlet
The second:/, so the address of the access is resolved by Dispatcherservlet, the resolution of the static file needs to be configured not to let Dispatcherservlet parse
Use this method to implement restful style URLs
The Third Kind:/*, this configuration is not correct, using this configuration, eventually to forward to a JSP page,
will still be interpreted by Dispatcherservlet JSP address, can not find handler based on JSP page, will error.
-
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--SPRINGMVC Front Controller configuration-
2. Configure the spring MVC configuration file
Spring MVC configuration files are specified by <servlet><init-param></init-param></servlet>
Configuring the processor adapter in SPRINGMVC
<!--processor adapter All processor adapters implement Handleradapter interface--
<bean class= "Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
3, development handler (and Controller)
You need to implement the Controller (Com.springmvc.controller) interface Implementation HandleRequest Method: Method Prototype: Modelandview HandleRequest (httpservletrequest Req
HttpServletResponse Res) throws Exception
Return to Modelandview interface
4, Configuration Handler
<!--configuration Handler--
<bean id= "ItemsController1" name= "/queryitems_test1.action" class= "Com.springmvc.controller.ItemsController01" />

5. Configure the View resolver
<!--view parser resolves JSP parsing by default using Jstl tag, classpath with Jstl
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<!--Configure JSP path prefixes--
<property name= "prefix" value= "/web-inf/jsp/"/>
<!--Configure the suffix of the JSP path--
<property name= "suffix" value= ". jsp"/>
</bean>
6, Configuration handlermapping: A mapper can have multiple URLs and controller mapping, can also exist multiple handlermapping mapper, URL and controller is a one-to-a relationship, The controller and the URL can be a one-to-many relationship. The controller can handle multiple URL requests, and the URL specifies a controller to execute, or it causes confusion.
<!--simple URL mapping--
<bean class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name= "Mappings" >
<props>
<!--URL mapping for itemsController1, URL is/queryitems1.action--
<prop key= "/queryitems1.action" >itemsController1</prop>
<prop key= "/queryitems2.action" >itemsController1</prop>
<prop key= "/queryitems3.action" >itemsController2</prop>
</props>
</property>
</bean>
7, Configuration Dispatcherserlvet.properties
Path in Org.springframework.web.servlet.DispatcherSerlvet.properties

Second, the use of annotations

Spring MVC Basic Learning

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.