springmvc (3) controller Configuration Summary

1. corresponding to url bean (ur found by direct the corresponding name or id bean, url is the case of a name or id bean)

    <!--配置handlerMapping-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <bean id="/hello.do" class="com.app.controller.HelloController"/>

 

The above configuration, access hello.do will find /hello.do ID for the bean, this method is only suitable for small applications.

2. url dispensing bean (beanName find the corresponding through url, then find the corresponding through beanName bean)

 <!--配置handlerMapping-->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <!--key对应url请求名 value对应处理器的id-->
                <prop key="/hello.do">helloController</prop>
            </props>
        </property>
    </bean>
    <bean id="helloController" class="com.app.controller.HelloController"/>

 

Such a configuration can use wildcards, access /hello.do, spring will allocation request response processor, if the key value to .do * (wildcard), then all requests ending .do will be processed helloController .

3.url bean configuration

    <!--配置handlerMapping-->
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <bean id="helloController" class="com.app.controller.HelloController"/>

 

The above configuration, access hello * .do will be allocated to the implementation of helloController

<!--配置handlerMapping-->
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <bean id="helloController" class="com.app.controller.HelloControlleController"/>

 

The above configuration, access hellocontroller * .do will be allocated to the implementation of helloController

<!--配置handlerMapping-->
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <bean id="helloController" class="com.app.controller.HelloControlleControll"/>

 

The above configuration, access hellocontrollercontroll * .do will be allocated to the implementation of helloController

To be sure the above experiment, this link is a java file name that is determined by the Controller of the name, rather than the bean name to determine if java file name is a controller at the end of the last, then the link will be removed by the end of the controller's lowercase for the remainder of the beginning, if the file does not end controller, it would be the beginning of all lowercase name of the link.

4. Notes

springmvc.xml
  <context: Scan-base- Component Package = "com.app.web" /> 

Java codes 
@Controller 
public  class HelloController in { 
    @RequestMapping ( "/ Hello" )
     public ModelAndView Hello (the HttpServletRequest Request, the HttpServletResponse Response) { 
        ModelAndView ModelAndView = new new ModelAndView ();
         // package to be displayed in the data view 
        modelAndView.addObject ( "MSG", "SpringMVC the Hello" );
         // attempt name 
        modelAndView.setViewName ( "Hello");   //    / the WEB-INF / jsp / hello.jsp 
        return  modelAndView;
    } 
}

 

Guess you like

Origin www.cnblogs.com/yuby/p/11005820.html