What kinds of SPRINGMVC controller configuration methods are there?

Source: Internet
Author: User

The first URL corresponding to the bean if you want to use this kind of configuration, you need to do the following style configuration in XML<!--means to map the requested URL and bean name--<beanclass="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean name="/hello.do" class="test. Hellocontroller"></bean>above configuration, Access/hello.do will be looking for ID/hello.do beans, which are only suitable for small application systems The second is to use a unified configuration collection for the URL allocation bean, mapping the corresponding controller for each URL<!--most common mapping configuration--<!--<prop key="/hello*.do">helloController</prop>--> <beanclass="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="Mappings"> <props> <prop key="/hello.do">helloController</prop> </props> </property> </bean> <bean name="Hellocontroller" class="test. Hellocontroller"></bean>Such a configuration can also use wildcards to access/hello.do, Spring will assign the request to Hellocontroller to handle the third URL matching bean if the controller name specification is defined, you can also use the following configuration<!--handing hello*.do to Hellocontroller-<beanclass="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <bean name="Hellocontroller" class="test. Hellocontroller"></bean>The fourth annotation first opens the annotation in the configuration file<!--enable Spring annotations--<context:component-scanBase-package="Test"/> <beanclass="Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>use annotations @org.springframework.stereotype.controller tags on writing classes This is a controller object using @requestmapping ("/hello.do"specifies the path of the method corresponding to processing, here is just a simple example, there will be more complex configuration code classes are as follows: Package test;  Import Java.util.Date;  Import Javax.servlet.http.HttpServletRequest;  Import Javax.servlet.http.HttpServletResponse;  Import org.springframework.web.bind.annotation.RequestMapping; // http://localhost: 8080/spring/hello.do?user=java@org. Springframework.stereotype.Controller Public classhellocontroller{@SuppressWarnings ("Deprecation") @RequestMapping ("/hello.do")       PublicString Hello (httpservletrequest request,httpservletresponse response) {Request.setattribute ("User", Request.getparameter ("User") +" -"+NewDate (). toLocaleString ()); return "Hello"; }  }

The Web. XML configuration file is as follows:<?xml version="1.0"encoding="UTF-8"? ><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="Http://java.sun.com/xml/ns/javaee"xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"Id="webapp_id"version="3.0"> <display-name>02springmvc_hello</display-name> <!--Spring MVC Configuration Dispatcherservlet This class takes over all requests. -<servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*. Do</url-pattern> </servlet-mapping></web-app>The  MVC-servlet.xml configuration file is as follows: <?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsd "><!--Configuring the Handlermapper mapper<beanclass="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>Configure controller Custom controllers<bean name="/he.do" class="Cn.sxt.controller.HelloController"/> <beanclass="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="Mappings"> <props>key corresponds to URL value corresponding to the ID of the custom controller<prop key="/he.do">helloController</prop> </props> </property> </bean> <bean id="Hellocontroller" class="Cn.sxt.controller.HelloController"/>-<beanclass="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <!--configuration request and processor side of the function and @requestmapping ("/hello.do") is consistent and must take the controllerclassnamehandlermapping name above to work. You can also use the ID request as Hello*.do will be matched-<!--<bean Name="hello.do" class="Cn.sxt.controller.HelloController"/>--> <bean id="Hellocontroller" class="Cn.sxt.controller.HelloController"/> <!--The implementation of configuring the Handeradapter adapter Simplecontrollerhandleradapter is also very simple, that is, the HandleRequest method for implementing a controller-<beanclass="Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--Configure the Internalresourceviewresolver--and <beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"ID="Internalresourceviewresolver"> <!--prefix--<property name="prefix"Value="/web-inf/jsp/"/> <!--suffix--<property name="suffix"Value=". JSP"/> </bean></beans>

What kinds of SPRINGMVC controller configuration methods are there?

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.