BeanNameUrlHandlerMapping:
这是默认的处理方式,controller配置的name属性为url
<bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <bean name="/spring.do" class="controller.SpringController"></bean>
SimpleUrlHandlerMapping:
这种方式controller的<bean>中需要id属性,在配置是通过<props>可以把多个url映射到同一controller上,访问http://127.0.0.1:8080/SprintMVC/mvc2.do或者mvc1都可以放访问
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/mvc1.do">spring</prop> <prop key="/mvc2.do">spring </prop> <prop key="/mvc3.do"> spring</prop> </props> </property> </bean> <bean id="spring" class="controller.SpringController"></bean>
ControllerClassNameHandlerMapping:
根据类名称进行截取,删除Controller后缀,将前面的名字转为小写SpringController转换后为spring,该配置的后,访问的url为http://127.0.0.1:8080/SprintMVC/spring.do
<bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <bean class="controller.SpringController"></bean
如果多个HandlerMapping都配置时,会从上到下执行,也可以设置Mapping的优先级,使用order属性,让其从下而上执行
<bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="order" value="3"></property> </bean> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/mvc1.do">spring</prop> <prop key="/mvc2.do">spring </prop> <prop key="/mvc3.do"> spring</prop> </props> </property> <property name="order" value="2"></property> </bean> <bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> <property name="order" value="1"></property> </bean>
AbstractCommandController:
在构造方法里绑定实体类,url访问时携带参数http://127.0.0.1:8080/SprintMVC/command.do?id=1&age=15&name=zhangsan
public class CommandController extends AbstractCommandController { @SuppressWarnings("deprecation") public CommandController() { //绑定实体类 super.setCommandClass(Person.class); super.setCommandName("person"); } @Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { Person p = (Person) command; System.out.println(p); return null; } }为了方便,Mapping都配置为ControllerClassNameHandlerMapping