SpringMVC同时支持多视图(JSP,Velocity,Freemarker等)的一种思路实现
本文内容纲要:
-一.自定义视图解析器
-二.视图映射配置dispather-servlet.xml
-三.控制器代码示例
-四.测试请求
在基于SpringMVC的项目中有时需要同时使用多种视图格式,如jsp,velocity及freemarker等,通过不同的请求路径配置规则,映射到不同的视图文件。下面我提供一种思路,通过视图模板文件后缀名的方式来进行处理。例如:
@RequestMapping(value="/hello")
publicStringjspTest(HttpServletRequestrequest,ModelMapmap){
return"demo.jsp";
}
将通过”demo.jsp”的后缀自动使用jsp的视图解析器。
下面描述下详细思路:
一.自定义视图解析器
packagecom.jeedev.common.web.springmvc.view;
importjava.util.HashMap;
importjava.util.Locale;
importjava.util.Map;
importjava.util.Set;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.springframework.web.servlet.View;
importorg.springframework.web.servlet.ViewResolver;
/**
*自定义视图解析(通过配置实现多视图整合,如jsp,velocity,freemarker,pdf,excel...)
*@authorhuligong
*
*/
publicclassJeeDevViewResolverimplementsViewResolver{
privatestaticLoglogger=LogFactory.getLog(JeeDevViewResolver.class);
publicViewresolveViewName(StringviewName,Localelocale)throwsException{
for(Map.Entry<Set<String>,ViewResolver>map:viewResolverMap.entrySet()){
Set<String>suffixs=map.getKey();
for(Stringsuffix:suffixs){
if(viewName.endsWith(suffix)){
ViewResolverviewResolver=map.getValue();
if(null!=viewResolver){
if(logger.isDebugEnabled()){
logger.debug("foundviewResolver'"+viewResolver+"'forviewName'"+viewName+"'");
}
returnviewResolver.resolveViewName(viewName,locale);
}
}
}
}
if(defaultViewResolver!=null){
returndefaultViewResolver.resolveViewName(viewName,locale);
}
//toallowforViewResolverchaining
returnnull;
}
privateMap<Set<String>,ViewResolver>viewResolverMap=newHashMap<Set<String>,ViewResolver>();
privateViewResolverdefaultViewResolver=null;
publicMap<Set<String>,ViewResolver>getViewResolverMap(){
returnviewResolverMap;
}
publicvoidsetViewResolverMap(Map<Set<String>,ViewResolver>viewResolverMap){
this.viewResolverMap=viewResolverMap;
}
publicViewResolvergetDefaultViewResolver(){
returndefaultViewResolver;
}
publicvoidsetDefaultViewResolver(ViewResolverdefaultViewResolver){
this.defaultViewResolver=defaultViewResolver;
}
}
在自定义视图解析器(其实是视图中转器)中,通过对视图文件的后缀判断(而不是请求地址的后缀)而返回给不同的视图解析器处理。
二.视图映射配置dispather-servlet.xml
<beanid="viewResolver"class="com.jeedev.common.web.springmvc.view.JeeDevViewResolver">
<propertyname="defaultViewResolver"ref="beanNameViewResolver"/>
<propertyname="viewResolverMap">
<map>
<entry>
<key>
<set>
<value>.jsp</value>
</set>
</key>
<refbean="jspViewResolver"/>
</entry>
<entry>
<key>
<set>
<value>.vm</value>
<value>.htm</value>
</set>
</key>
<refbean="velocityViewResolver"/>
</entry>
<entry>
<key><value>.ftl</value></key>
<refbean="freeMarkerViewResolver"/>
</entry>
</map>
</property>
</bean>
<beanid="beanNameViewResolver"class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<beanid="jspViewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/>
<propertyname="prefix"value="/WEB-INF/jsp/"/>
<!--<propertyname="suffix"value=".jsp"/>-->
</bean>
<!--VelocityViewResolverConfiguration-->
<beanid="velocityViewResolver"class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<propertyname="order"value="0"/>
<propertyname="contentType"value="text/html;charset=UTF-8"/>
<propertyname="requestContextAttribute"value="req"/>
</bean>
<!--VelocityConfiguration-->
<beanid="velocityConfig"class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<propertyname="configLocation"value="/WEB-INF/velocity.properties"/>
<propertyname="resourceLoaderPath"value="/"/>
</bean>
<!--FreeMarkerViewResolverConfiguration-->
<beanid="freeMarkerViewResolver"class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<propertyname="order"value="0"/>
<propertyname="viewClass"value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
</bean>
<!--FreeMarkerConfiguration-->
<beanid="freemarkerConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<propertyname="configLocation"value="/WEB-INF/freemarker.properties"/>
<propertyname="templateLoaderPath"value="/WEB-INF/ftl/"/>
<propertyname="freemarkerVariables">
<map>
<entrykey="xml_escape"value-ref="fmXmlEscape"/>
</map>
</property>
</bean>
<beanid="fmXmlEscape"class="freemarker.template.utility.XmlEscape"/>
这里我配置成所有返回.jsp的视图模板请求都转到JSP视图解析器jspViewResolver中去处理,返回.htm,.vm视图模板请求都转到velocity视图解析器中去处理。
三.控制器代码示例
packagecom.jeedev.demo.view;
importjavax.servlet.http.HttpServletRequest;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.ModelMap;
importorg.springframework.web.bind.annotation.RequestMapping;
/**
*多视图解析器支持示例
*@authorhuligong
*
*/
@Controller
@RequestMapping(value="/demo")
publicclassJeeDevViewResolverTestController{
privatestaticLoglogger=LogFactory.getLog(JeeDevViewResolverTestController.class);
@RequestMapping(value="/test1")
publicStringtest1(HttpServletRequestrequest,ModelMapmap){
logger.info("使用JSP视图解析器");
map.put("name","helloworld");
return"jspTest.jsp";
}
@RequestMapping(value="/test2")
publicStringtest2(HttpServletRequestrequest,ModelMapmap){
logger.info("使用Freemarker视图解析器");
map.put("name","helloworld");
return"hello.ftl";
}
@RequestMapping(value="/test3")
publicStringtest3(HttpServletRequestrequest,ModelMapmap){
logger.info("使用Velocity视图解析器");
map.put("name","helloworld");
return"/html/demo.htm";
}
}
四.测试请求
我在web.xml里给DispatcherServlet添加了/view/*,所以,只要在请求前加上/view就可以了。
http://localhost:8080/view/demo/test1
http://localhost:8080/view/demo/test2
http://localhost:8080/view/demo/test3
皆验证通过。
本文内容总结:一.自定义视图解析器,二.视图映射配置dispather-servlet.xml,三.控制器代码示例,四.测试请求,
原文链接:https://www.cnblogs.com/huligong1234/p/3515747.html