spring mvc 多视图配置

1、创建MultiViewResover类

package org.pzy.module.result_view.view_resolver;

import java.util.Locale;
import java.util.Map;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

public class MultiViewResover implements ViewResolver {

	private Map<String, ViewResolver> resolvers;

	/**
	 * 默认视图类型为jsp视图
	 */
	private static final String DEFAULT_VIEW_TYPE = "jsp";

	@Override
	public View resolveViewName(String viewName, Locale locale) throws Exception {
		int n = viewName.lastIndexOf("_"); // 获取
		// viewName(modelAndView中的名字)看其有没有下划线
		String suffix = MultiViewResover.DEFAULT_VIEW_TYPE;
		// 默认使用jsp视图
		if (n != -1) {
			// 有的话截取下划线后面的字符串 这里一般是jsp,ftl,vm与配置文件中的<entry key="ftl">的key匹配
			suffix = viewName.substring(n + 1);
			// 取下划线前面的部分 那时真正的资源名.比如我们要使用hello.jsp 那viewName就应该是hello_jsp
			viewName = viewName.substring(0, n);
		}
		// 根据下划线后面的字符串去获取托管的视图解析类对象
		ViewResolver resolver = resolvers.get(suffix);

		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;
	}

}


2、配置spring mvc的视图转换规则(dispacher-servlet)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<mvc:annotation-driven />
	<context:component-scan base-package="org.pzy" />

	<mvc:resources location="/resources/" mapping="/resources/**" />

	<bean id="velocityConfig"
		class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
		<!-- Velocity模版文件存放路径(spring mvc会在该路径下寻找) -->
		<property name="resourceLoaderPath" value="/WEB-INF/vm" />
		<!-- Velocity配置文件 -->
		<property name="configLocation" value="classpath:velocity/velocity.properties" />
	</bean>

	<bean id="viewResolver"
		class="org.pzy.module.result_view.view_resolver.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="/" />
						<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>


3、编写测试controller类

package org.pzy.module.result_view.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("index")
public class IndexController {

	@RequestMapping(value = { "/test/{test}" }, method = RequestMethod.GET)
	public String test(@PathVariable String test, Map<String, Object> result) {
		result.put("flag", test);
		// 不加任何后缀,使用jsp视图
		return "index";
		
		// 使用jsp视图
		// return "index_jsp" ;

		// 使用velocity视图
		// return "index_vm";

		// 使用freemark视图
		// return "index_ftl"
	}
}

4、分别编写velocity/freemark/jsp的测试页面,测试效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值