- SimpleUrlHandlerMapping中使用key和value的对应关系,定义的url与bean的对应关系,最终会被解析设置到类中的urlmap变量中。而SimpleUrlHandlerMapping类重写了父类AbstractHandlerMapping的initApplicationContext方法
示例
- dispatcher-servlet.xml
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- /hello 路径的请求交给 id 为 helloController 的控制器处理-->
<prop key="/hello">helloController</prop>
</props>
<!-- 直接传入路径请求和控制器id-->
<value>
/welcome.htm=welcomeController
/*/welcome.htm=welcomeController
/helloGuest.htm=helloGuestController
</value>
</property>
</bean>
<!-- 注册控制类对象 -->
<bean id="helloController" class="controller.HelloController"></bean>
- HelloController.java
public class HelloController implements Controller {
public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception { ModelAndView mav = new ModelAndView("index.jsp"); return mav; } } |