Springmvc a kind of idea to support multiple views (jsp,velocity,freemarker, etc.) at the same time __jsp

Source: Internet
Author: User
Tags locale

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&LT;SET&LT;STRING&GT;,VIEWR
    
    Esolver> ();

    Private Viewresolver defaultviewresolver = null;
    public map<set<string>, viewresolver> Getviewresolvermap () {return viewresolvermap; public void Setviewresolvermap (MAP&LT;SET&LT;STRING&GT;, 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 "> &LT;MAP&G
                T <entry> <key> <set> <value&gt ;. 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="/&
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.