首页 > 代码库 > Spring MVC系列:(2)视图解析器、URL映射器、适配器
Spring MVC系列:(2)视图解析器、URL映射器、适配器
1、默认的URL映射器、适配器和视图解析器
SpringMVC提供了默认的URL映射器、适配器和视图解析器。
在上一文中,springmvc-helloworld.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean name="/helloworld.action" class="com.rk.web.action.HelloWorldAction"></bean> </beans>
上面的配置与下面的配置是一样的:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 控制器(程序员) --> <bean name="/helloworld.action" class="com.rk.web.action.HelloWorldAction"></bean> <!-- 映射器(框架) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 适配器(框架) --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 视图解析器(框架) --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
2、视图解析器InternalResourceViewResolver
作用:解析视图逻辑名对应的真实路径
ModelAndView对象中即可以封装真实视图路径名,也可以封装视图路径的逻辑名。
(1)修改Action类的代码
HelloWorldAction类的代码:
由原来的
modelAndView.setViewName("/jsp/success.jsp");
改成
modelAndView.setViewName("success");
完整的HelloWorldAction.java
package com.rk.web.action; 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 HelloWorldAction implements Controller { public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", "这是我的第一个SpringMVC应用"); modelAndView.setViewName("success"); return modelAndView; } }
(2)在配置文件中进行修改
springmvc-helloworld.xml内容添加如下部分:
<!-- 视图解析器(框架) --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="http://www.mamicode.com/jsp/"></property> <property name="suffix" value="http://www.mamicode.com/.jsp"></property> </bean>
完整的springmvc-helloworld.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean name="/helloworld.action" class="com.rk.web.action.HelloWorldAction"></bean> <!-- 视图解析器(框架) --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="http://www.mamicode.com/jsp/"></property> <property name="suffix" value="http://www.mamicode.com/.jsp"></property> </bean> </beans>
3、映射器Mapping
【什么样的请求交给Action】
3.1、BeanNameUrlHandlerMapping
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping(核心)
将程序员定义的Action所对应的<bean>标签的name属性作为请求路径
<!-- 注册控制器(程序员) --> <bean name="/helloworld.action" class="com.rk.web.action.HelloWorldAction"></bean> <!-- 注册映射器(handler包)(框架) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
3.2、SimpleUrlHandlerMapping
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
/delete.action和/update.action和/find.action请求路径都交由<bean>标签为id的Action,即
多个路径对应同一个Action
<!-- 注册控制器(程序员) --> <bean id="helloworldID" class="com.rk.web.action.HelloWorldAction"></bean> <!-- 注册映射器(handler包)(框架) --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/delete.action">helloworldID</prop> <prop key="/update.action">helloworldID</prop> <prop key="/find.action">helloworldID</prop> </props> </property> </bean>
4、适配器Adapter
Action实现Controller接口
package com.rk.web.action; 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 HelloWorldAction implements Controller { public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", "这是我的第一个SpringMVC应用"); modelAndView.setViewName("success"); return modelAndView; } }
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
<!-- 适配器(框架) --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
Spring MVC系列:(2)视图解析器、URL映射器、适配器