Spring MVC多視圖配置

[java]
1.Spring配置的XML 
[html]
<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xsi:schemaLocation=" 
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd "> 
    <context:component-scan 
     base-package="com.spring.action"/> 
    <!-- 
    org.springframework.web.servlet.view.ResourceBundleViewResolver 
    用於多個視圖集成時,ResourceBundleViewResolver是經過解析資源文件來解析請求輸出文件的。 
    <property name="basename" value="views"></property>,即表示在/WEB-INF/classes路徑下有一個 
    views.properties文件,本例中views.properties的內容爲 
    welcome.(class)=org.springframework.web.servlet.view.velocity.VelocityView 
    welcome.url=welcome.vm 
    freemarker.(class)=org.springframework.web.servlet.view.freemarker.FreeMarkerView 
    freemarker.url=freemarker.ftl 
    --> 
    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> 
        <property name="basename" value="views"> 
        </property> 
        <!--
        <property name="order" value="0"></property>
        --> 
    </bean> 
    <!-- FreeMarker環境配置 --> 
    <bean id="freemarkerConfig" 
     
     class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
        <!-- freemarker模板位置 --> 
        <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/> 
    </bean> 
    <!-- velocity環境配置 --> 
    <bean id="velocityConfig"  
 
     class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 
        <!-- velocity配置文件路徑 --> 
        <property name="configLocation" value="/WEB-INF/velocity.properties"/> 
        <!-- velocity模板路徑 --> 
        <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/> 
    </bean> 
    <bean id="viewResolver" 
     class="com.spring.action.MultiViewResover"> 
        <property name="resolvers"> 
            <map> 
                <entry key="jsp"> 
                    <bean 
                     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> 
                </entry> 
                <entry key="ftl"> 
                    <bean 
                     class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
                        <property name="cache" value="true"/> 
                        <property name="prefix" value="/"/> 
                        <property name="suffix" value=".ftl"/>   
                    </bean> 
                </entry> 
                <entry key="vm"> 
                    <bean 
                     class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 
                        <property name="cache" value="true"/> 
                        <property name="prefix" value="/"/> 
                        <property name="suffix" value=".vm"/> 
                    </bean> 
                </entry> 
            </map> 
        </property> 
    </bean> 
     
</beans>  html

2 解析多視圖請求的類 java

 

[java]
package com.spring.action; 
 
import java.util.Locale; 
import java.util.Map; 
import org.springframework.web.servlet.View; 
import org.springframework.web.servlet.ViewResolver; 
 
/**
 * @author Jeson
 * @blog :http://www.gbsou.com
 * @date:Oct 20, 2009 8:39:27 AM
 * @version :1.0
 * 
 */ 
public class MultiViewResover implements ViewResolver { 
 
    private Map<String, ViewResolver> resolvers; 
 
    @Override 
    public View resolveViewName(String viewName, Locale locale) 
            throws Exception { 
        int n = viewName.lastIndexOf("_"); // 獲取 
                                            // viewName(modelAndView中的名字)看其有沒有下劃線 
        if (n == (-1)) 
            return null; // 沒有則直接返回空 
        // 有的話截取下劃線後面的字符串 這裏通常是jsp,ftl,vm與配置文件中的<entry key="ftl">的key匹配 
        String suffix = viewName.substring(n + 1); 
        // 根據下劃線後面的字符串去獲取託管的視圖解析類對象 
        ViewResolver resolver = resolvers.get(suffix); 
 
        // 取下劃線前面的部分 那時真正的資源名.好比咱們要使用hello.jsp 那viewName就應該是hello_jsp 
        viewName = viewName.substring(0, n); 
        if (resolver != null) 
            return resolver.resolveViewName(viewName, locale); 
        return null; 
    } 
 
    public Map<String, ViewResolver> getResolvers() { 
        return resolvers; 
    } 
 
    public void setResolvers(Map<String, ViewResolver> resolvers) { 
        this.resolvers = resolvers; 
    } 
web

3. action spring

[java]
package com.spring.action; 
 
import java.util.HashMap; 
import java.util.Map; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 
 
@Controller 
public class HelloWorldAction { 
 
    @RequestMapping(value = "/helloworld.do") 
    public ModelAndView hello() { 
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("hello_jsp"); 
        modelAndView.addObject("message", "Hello,SpringMvc"); 
        return modelAndView; 
    } 
 
    @RequestMapping(value = "/welcome.do") 
    public ModelAndView helloVm() { 
        // ModelAndView modelAndView=new ModelAndView(); 
        // modelAndView.setViewName("freemarker_ftl"); 
        // modelAndView.addObject("message", "Hello,Velocity"); 
        // return modelAndView; 
        Map map = new HashMap(); 
        return new ModelAndView("freemarker_ftl",  map); // 根據以前的解釋這裏就是去尋找 
                                                            // user.ftl資源 
 
    } 
 
    @RequestMapping(value = "/free.do") 
    public ModelAndView helloFreeMarker() { 
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("welcome"); 
        modelAndView.addObject("message", "Hello,Freemarker"); 
        return modelAndView; 
    } 
 
app

這樣 在頁面鍵入對應action就能夠訪問對應的視圖實現了
相關文章
相關標籤/搜索