SpringMVC中HandlerMapping的三种配置方式
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <!--文件名springmvc-servlet.xml中的“springmvc”与web.xml的<servlet-name>springmvc</servlet-name>中的“springmvc”对应 --> <beans> <!-- HanderMapping-有3种访问Controller的方式 --> <!--方式一 根据简单URL查找Controller--> <!-- <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index">indexController</prop> </props> </property> </bean> <bean id="indexController" class="controller.IndexController"></bean> --> <!--方式二 根据beanname找到对应Conntroller --> <!-- <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean> <bean name="/index" class="controller.IndexController"></bean> --> <!--方式三 根据控制类的类名访问Controller,访问时类的类名需要小写(加不加.do均可) http://192.168.100.47:8080/springmvc/indexController --> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <bean class="controller.IndexController"></bean> </beans>
controller
package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class IndexController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("index.jsp"); mav.addObject("message", "Hello Spring MVC"); return mav; } }