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