Spring MVC Series: (2) View parser, URL Mapper, adapter

Source: Internet
Author: User



1. Default URL mapper, adapter, and view resolver

SPRINGMVC provides the default URL mapper, adapter, and view resolver.

In the previous article, the Springmvc-helloworld.xml content is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spring-mvc . xsd "> <bean name="/helloworld.action "class=" com.rk.web.action.HelloWorldAction "></bean> </ Beans>


The above configuration is the same as the following configuration:

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "    xmlns:mvc=" Http://www.springframework.org/schema/mvc "     xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"     xsi:schemalocation= "         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd "><!--  Controllers (programmers)  -->     <bean name= "/helloworld.action"  class= "Com.rk.web.action.HelloWorldAction" > </bean>    <!--  Mapper (frame)  -->      < Bean class= "ORG.SPRINGFRamework.web.servlet.handler.BeanNameUrlHandlerMapping "></bean>                   <!--  Adapters (frames)  -->       <bean class= " Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter "></bean>                   <!--  View parser (frame)  -->      <bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "></bean>         </beans>



2. View Resolver internalresourceviewresolver

Function: Parse the actual path of the view logical name

The Modelandview object can encapsulate the true view pathname or encapsulate the logical name of the view path.

(1) Modifying the code of the Action class

Code for the Helloworldaction class:

By the original

Modelandview.setviewname ("/jsp/success.jsp");

Change into

Modelandview.setviewname ("Success");


The Complete Helloworldaction.java

Package Com.rk.web.action;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 Helloworldaction implements Controller {public Modelandview HandleRequest (httpservletrequest request,httpservletresponse response) throws Exception {Modelandview Modelandview = new Modelandview (); Modelandview.addobject ("Message", "This is my first SPRINGMVC application"); modelandview.setviewname ("Success"); return Modelandview;}}


(2) Make changes in the configuration file

Add the following sections to the Springmvc-helloworld.xml content:

<!--view parser (frame)--><bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/jsp/" ></property><property name= "suffix" value= ". JSP" ></ Property></bean>



The Complete Springmvc-helloworld.xml

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "    xmlns:mvc=" Http://www.springframework.org/schema/mvc "     xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"     xsi:schemalocation= "         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd ">    <bean name= "/helloworld.action"  class= "com.rk.web.action.HelloWorldAction" ></bean><!--  View parser (frame)  --><bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >< Property name= "prefix" &Nbsp;value= "/jsp/" ></property><property name= "suffix"  value= ". JSP" ></property> </bean></beans>


3, Mapper Mapping

"What kind of request is given to action"


3.1, Beannameurlhandlermapping

Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping (CORE)

The name attribute of the <bean> tag corresponding to the programmer-defined action as the request path

<!--registration Controller (programmer)--><bean name= "/helloworld.action" class= "Com.rk.web.action.HelloWorldAction" ></ bean><!--Registration Mapper (Handler package) (frame)--><bean class= " Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "></bean>


3.2, simpleurlhandlermapping

Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

The/delete.action and/update.action and/find.action request paths are referred to the <bean> tag-ID action, which is

Multiple paths correspond to the same action

<!--registration Controller (programmer)--<bean id= "Helloworldid" class= "Com.rk.web.action.HelloWorldAction" ></bean> <!--registration Mapper (Handler package) (frame)--><bean class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping "><property name=" mappings "><props><prop key="/delete.action ">helloworldID</prop>< Prop key= "/update.action" >helloworldid</prop><prop key= "/find.action" >helloworldID</prop> </props></property></bean>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/87/61/wKioL1fe9KXC4ghwAAMNGy1WFWQ684.gif "title=" 003. GIF "alt=" Wkiol1fe9kxc4ghwaamngy1wfwq684.gif "/>



4, adapter Adapter


Action Implementation Controller Interface


Package Com.rk.web.action;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 Helloworldaction implements Controller {public Modelandview HandleRequest (httpservletrequest request,httpservletresponse response) throws Exception {Modelandview Modelandview = new Modelandview (); Modelandview.addobject ("Message", "This is my first SPRINGMVC application"); modelandview.setviewname ("Success"); return Modelandview;}}


Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter

<!--adapter (frame)--><bean class= "Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" >< /bean>




Spring MVC Series: (2) View parser, URL Mapper, adapter

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.