Spring MVC Configuration Controller Detailed

Source: Internet
Author: User

In Springmvc, there are many ways to configure a controller, such as a simple summary

The first URL corresponds to the bean
If you want to use this type of configuration, you need to configure the following styles in XML:

<!--means to map the requested URL and bean name to  class= " Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "/>  class

Above configuration, Access /hello.do will look for ID/hello.do Bean, this kind of configuration application is not too wide, the website is big, controller more, this way to be exhausted

Second, assigning a bean to a URL
Use a unified configuration collection to map the controller that corresponds to each URL:

    <!--most common mapping configuration--      <!--<prop key= "/hello*.do" >helloController</prop>-->       Class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >       <property name= " Mappings ">        <props>         <prop key="/hello.do ">helloController</prop>        </props>       </property>      </bean>      class= "test. Hellocontroller "></bean>  

This type of configuration can also use wildcards, when accessing/hello.do, spring will assign the request to Hellocontroller for processing, which is better than the first, you can use wildcard characters for universal matching, but the practicality is not very large

The third type of annotation @controller

First you need to configure the annotations in the configuration file:

<!--enable Spring annotations--  <context:component-scanbase-package = "Test"/>  class

Using annotations @org.springframework.stereotype.controller markup on writing classes This is a controller object.
Use @requestmapping ("/hello.do") to specify the path of the method corresponding to the processing, here is a simple example, there will be more complex configuration

The code classes are as follows:

     Packagetest; Importjava.util.Date; Importjavax.servlet.http.HttpServletRequest; ImportJavax.servlet.http.HttpServletResponse; Importorg.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"; }      }  

Spring MVC Configuration Controller Detailed

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.