SpringMVC同時支援多視圖(JSP,Velocity,Freemarker等)的一種思路實現__JSP

來源:互聯網
上載者:User

第一種方式:

在基於SpringMVC的項目中有時需要同時使用多種視圖格式,如jsp,velocity及freemarker等,通過不同的請求路徑配置規則,映射到不同的視圖檔案。下面我提供一種思路,通過視圖模板檔案尾碼名的方式來進行處理。例如:

@RequestMapping(value = "/hello") 
public String jspTest(HttpServletRequest request, ModelMap map) { 
    return "demo.jsp"; 

將通過”demo.jsp”的尾碼自動使用jsp的視圖解析器。

 

下面描述下詳細思路:

  一.自訂視圖解析器

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;/** * 自訂視圖解析(通過配置實現多視圖整合,如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 defaultViewResolver.resolveViewName(viewName, locale);       }       // to allow for ViewResolver chaining       return null;    }            private Map<Set<String>,ViewResolver> viewResolverMap = new HashMap<Set<String>,ViewResolver>();        private ViewResolver defaultViewResolver = null;    public Map<Set<String>, ViewResolver> getViewResolverMap() {        return viewResolverMap;    }    public void setViewResolverMap(Map<Set<String>, ViewResolver> viewResolverMap) {        this.viewResolverMap = viewResolverMap;    }    public ViewResolver getDefaultViewResolver() {        return defaultViewResolver;    }    public void setDefaultViewResolver(ViewResolver defaultViewResolver) {        this.defaultViewResolver = defaultViewResolver;    }}

在自訂視圖解析器(其實是視圖中轉器)中,通過對視圖檔案的尾碼判斷(而不是請求地址的尾碼)而返回給不同的視圖解析器處理。

  二.視圖映射配置dispather-servlet.xml

 

<bean id="viewResolver" class="com.jeedev.common.web.springmvc.view.JeeDevViewResolver">        <property name="defaultViewResolver" ref="beanNameViewResolver"/>        <property name="viewResolverMap">            <map>                <entry>                    <key>                        <set>                            <value>.jsp</value>                        </set>                    </key>                    <ref bean="jspViewResolver"/>                </entry>                <entry>                    <key>                        <set>                            <value>.vm</value>                            <value>.htm</value>                        </set>                    </key>                    <ref bean="velocityViewResolver"/>                </entry>                <entry>                    <key><value>.ftl</value></key>                    <ref bean="freeMarkerViewResolver"/>                </entry>            </map>        </property>    </bean>        <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>        <property name="prefix" value="/WEB-INF/jsp/"/>        <!--<property name="suffix" value=".jsp"/>-->    </bean>        <!--  Velocity ViewResolver Configuration -->    <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="resourceLoaderPath" value="/&
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.