Spring MVC tutorial ing processor Handler Mapping

Source: Internet
Author: User

I. Analysis of ing processors in spring mvc, The ing processor can map web requests to the correct processor. spring has many built-in ing processors, and we can also customize the ing processor. The following example shows the three most commonly used ing processors in spring: BeanNameUrlHandlerMapping (default). The official recommendation is to use the default er SimpleUrlHandlerMappingControllerClassNameHandlerMapping. Quick Case demonstration: <! -- Bean name url processor ing --> <bean id = "beanNameUrlHandlerMapping" class = "org. springframework. web. servlet. handler. beanNameUrlHandlerMapping "> <property name =" order "value =" 1 "/> </bean> <! -- Simple url processor ing --> <bean id = "simpleUrlHandlerMapping" class = "org. springframework. web. servlet. handler. simpleUrlHandlerMapping "> <property name =" mappings "> <props> <prop key ="/u9vip. action "> u9Controller </prop> </props> </property> <property name =" order "value =" 2 "/> </bean> <! -- Controller class name processor ing --> <bean id = "controllerClassNameHandlerMapping" class = "org. springframework. web. servlet. mvc. support. controllerClassNameHandlerMapping "> <property name =" order "value =" 3 "/> </bean> it is necessary to understand the following key points before the code is copied: (1) the ing processor can send requests to the HandlerExecutionChain, and the processor execution link must contain the processor capable of processing the request (essentially, the processor is dynamically added to the processor chain, can be understood Based on the working principle of the filter), and the processor link can also contain a series of interceptors. (2) the two most common spring processors mentioned above inherit from the AbstractHandlerMapping class, so they have the attributes of the parent class. Ii. Example: BeanNameUrlHandlerMapping: Create the SpringMVC02 project and import relevant jar packages. Quick: Link: http://pan.baidu.com/s/1c0h6RGO extract password: 2xqe com.springsource.javax.servlet.jsp.jstl-1.1.2.jarcom.springsource.org.aopalliance-1.0.0.jarcom.springsource.org.apache.commons.logging-1.1.1.jarcom.springsource.org. apache. taglibs. standard-1.1.2.jarorg.springframework.aop-3.0.0.RELEASE.jarorg.springframework.asm-3.0.0.RELEASE.jarorg.springframework.beans-3.0.0.RELEASE.jarorg.springframework.context.su Pport-3.0.0.RELEASE.jarorg.springframework.context-3.0.0.RELEASE.jarorg.springframework.core-3.0.0.RELEASE.jarorg.springframework.expression-3.0.0.RELEASE.jarorg.springframework.web.servlet-3.0.0.RELEASE.jarorg.springframework.web-3.0.0.RELEASE.jar copy code Step 1: Create a back-end controller package: com. u9vip. springmvc. controller Class Name: U9Controller. java, the Code is as follows: package com. u9vip. springmvc. controller; import javax. servlet. http. httpServle TRequest; import javax. servlet. http. httpServletResponse; import org. springframework. web. servlet. modelAndView; import org. springframework. web. servlet. mvc. abstractController; public class U9Controller extends AbstractController {@ Override protected ModelAndView handleRequestInternal (HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {// http://www.u9vip.com U9 Community return null;} copy code steps 2. Configure web. xml with the following code: <? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <! -- Configure spring core servlet --> <servlet-name> action </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <! -- If the url-pattern is set to/without the file suffix, other static files (such as js and css) cannot be accessed. If the configuration is *. do does not affect access to static files *. the action configuration intercepts all. for requests with a suffix of action, note: Here, our servlet-name is configured with action, when we configure "application context. The name should be action-servlet.xml --> <servlet-mapping> <servlet-name> action </servlet-name> <url-pattern> *. action </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app> copy code Step 3: configure the action-servlet.xml, the Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework.org/schema/context" xmlns: p = "http://www.springframework.org/schema/p" 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-3.0.xs D http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <bean id =" u9Controller "name ="/u9.action "class =" com. u9vip. springmvc. controller. U9Controller "> </bean> <! -- View parser --> <bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver"> <! -- Prefix name --> <property name = "prefix" value = "/WEB-INF/U9VIP/"> </property> <! -- Suffix --> <property name = "suffix" value = ". jsp "> </property> </bean> </beans> copy code Step 4: Create U9FirstSpringMVC under the WEB-INF/U9VIP directory. jsp, the main code is as follows: <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">

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.