Spring MVC XmlViewResolver example
2015-09-13 16:27
429 查看
In Spring MVC,
In above case, it loads the view beans from “
“id” is the “view name” to resolve.
“class” is the type of the view.
“url” property is the view’s url location.
File :
XmlViewResolveris used to resolve “view name” based on view beans in the XML file. By default,
XmlViewResolverwill loads the view beans from
/WEB-INF/views.xml, however, this location can be overridden through the “
location” property :
<beans ...> <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location"> <value>/WEB-INF/spring-views.xml</value> </property> </bean> </beans>
In above case, it loads the view beans from “
/WEB-INF/spring-views.xml“. See
XmlViewResolverexample :
1. Controller
A controller class, returns a view, named “WelcomePage“.
//... public class WelcomeController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView model = new ModelAndView("WelcomePage"); return model; } }
2. XmlViewResolver
Register theXmlViewResolverin the Spring’s bean configuration file, loads the view beans from “
/WEB-INF/spring-views.xml“.
<beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> <!-- Register the bean --> <bean class="com.mkyong.common.controller.WelcomeController" /> <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location"> <value>/WEB-INF/spring-views.xml</value> </property> </bean> </beans>
3. View beans
The “view bean” is just a normal Spring bean declared in the Spring’s bean configuration file, where“id” is the “view name” to resolve.
“class” is the type of the view.
“url” property is the view’s url location.
File : spring-views.xml
<beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd"> <bean id="WelcomePage" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/WEB-INF/pages/WelcomePage.jsp" /> </bean> </beans>
How it works ?
When a view name “WelcomPage” is returned by controller, the
XmlViewResolverwill find the bean id “
WelcomPage” in “
spring-views.xml” file, and return the corresponds view’s URL “
/WEB-INF/pages/WelcomPage.jsp” back to the
DispatcherServlet.
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- XML 与 JSON 优劣对比
- As3.0 xml + Loader应用代码
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- C#针对xml基本操作及保存配置文件应用实例
- asp下查询xml的实现代码
- sqlserver FOR XML PATH 语句的应用
- 使用sp_xml_preparedocument处理XML文档的方法
- C#中的Linq to Xml详解
- C#操作XML文件实例汇总
- SQL Server中的XML数据进行insert、update、delete
- SQL Server中的XML数据进行insert、update、delete操作实现代码
- 关于SQLServer2005的学习笔记 XML的处理
- C#通过DataSet读写xml文件的方法
- C#实现基于XML配置MenuStrip菜单的方法