SpringMVC 2:非注解常见HandlerMapping和Controller
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
相关推荐
JudeJoo 2020-08-21
meleto 2020-08-15
小鱿鱼 2020-06-26
MicroBoy 2020-06-17
zhongliwen 2020-06-13
咻pur慢 2020-05-27
小鱿鱼 2020-05-08
neweastsun 2020-05-04
牧场SZShepherd 2020-02-21
凯哥Java 2020-02-16
neweastsun 2020-01-10
melonjj 2020-01-05
zmysna 2020-07-04
玫瑰小妖 2020-07-18
dlutbob 2020-07-04
zhongliwen 2020-06-13
qingjiuquan 2020-06-07