The first way:
In SPRINGMVC projects, there are times when you need to use multiple view formats, such as Jsp,velocity and Freemarker, to map to different view files through different request path configuration rules. Below I provide a way of thinking, through the view template file suffix name to deal with. For example:
@RequestMapping (value = "/hello")
Public String jsptest (httpservletrequest request, Modelmap map) {
return "demo.jsp";
}
The JSP's view parser is automatically used by the suffix "demo.jsp".
The following detailed ideas are described below:
A. Custom View parser
Package Com.jeedev.common.web.springmvc.view;
Import Java.util.HashMap;
Import Java.util.Locale;
Import Java.util.Map;
Import Java.util.Set;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Org.springframework.web.servlet.View;
Import Org.springframework.web.servlet.ViewResolver;
/** * Custom visual Graphic analysis (multi-view integration through configuration, such as Jsp,velocity,freemarker,pdf,excel ...) * @author Huligong * * */public class Jeedevviewresolver implements Viewresolver {private static Log logger =
Logfactory.getlog (Jeedevviewresolver.class); Public View resolveviewname (String viewName, Locale Locale) throws Exception {for (map.entry<set<string>,
Viewresolver> Map:viewResolverMap.entrySet ()) {set<string> Suffixs = Map.getkey (); for (String Suffix:suffixs) {if (viewname.endswith (suffix)) {viewresolver Viewresolver
= Map.getvalue ();
if (null!= viewresolver) { if (logger.isdebugenabled ()) {Logger.debug ("found Viewresolver '" + viewresolver
+ "' for ViewName '" + viewname+ "");
Return Viewresolver.resolveviewname (viewName, locale); }} if (Defaultviewresolver!= null) {return Defaultviewreso
Lver.resolveviewname (viewName, locale);
}//To allow for viewresolver chaining return null; Private map<set<string>,viewresolver> Viewresolvermap = new HASHMAP<SET<STRING>,VIEWR
Esolver> ();
Private Viewresolver defaultviewresolver = null;
public map<set<string>, viewresolver> Getviewresolvermap () {return viewresolvermap; public void Setviewresolvermap (MAP<SET<STRING>, viewresolver> viewresolvermap) {This.viewresol
Vermap = Viewresolvermap; } public VIewresolver Getdefaultviewresolver () {return defaultviewresolver; } public void Setdefaultviewresolver (Viewresolver defaultviewresolver) {this.defaultviewresolver = DefaultVi
Ewresolver; }
}
In the Custom view parser (in fact, the view relay), the view file is returned to the different views parser for processing, rather than the suffix of the request address.
two. View mapping configuration Dispather-servlet.xml
<bean id= "Viewresolver" class= "Com.jeedev.common.web.springmvc.view.JeeDevViewResolver" > <property name= " Defaultviewresolver "ref=" Beannameviewresolver "/> <property name=" Viewresolvermap "> <MAP&G
T <entry> <key> <set> <value> ;. jsp</value> </set> </key> <ref bean= "
Jspviewresolver "/> </entry> <entry> <key> <set> <value>.vm</value> <va lue>.htm</value> </set> </key> <re F bean= "Velocityviewresolver"/> </entry> <entry> <ke Y><value>.ftl</value></key> <ref bean= "Freemarkerviewresolver"/> </entry> </map> </property> </bean> <bean id= "Beannameview Resolver "class=" Org.springframework.web.servlet.view.BeanNameViewResolver "/> <bean" id= "cl ass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "Viewclass"
Org.springframework.web.servlet.view.JstlView "/> <property name= prefix" value= "/web-inf/jsp/"/> <!--<property name= "suffix" value= ". jsp"/>--> </bean> <!--Velocity Viewresolver Config Uration--> <bean id= "velocityviewresolver" class = "
Org.springframework.web.servlet.view.velocity.VelocityViewResolver "> <property name=" order "value=" 0 "/>
<property name= "ContentType" value= "Text/html;charset=utf-8"/> <property name= "Requestcontextattribute" value= "req"/> </bean> <!--Velocity Configuration-
> <bean id= "velocityconfig" class = "Org.springframework.web.servlet.view.velocity.VelocityConfigurer" > <property name= "configlocation" value= "/web-inf/velocity.properties"/> <property name= "Resource" Loaderpath "Value="/&