spring mvc 多视图配置
来源:互联网 发布:java 判断小数 编辑:程序博客网 时间:2024/04/27 13:49
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";@Overridepublic 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_jspviewName = 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"><beanclass="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"><beanclass="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"><beanclass="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
- Spring MVC多视图配置
- Spring MVC多视图配置
- spring mvc 多视图配置
- spring mvc 多视图配置
- spring MVC多视图配置
- spring mvc多视图解释,配置详解
- Spring MVC配置多视图解析器
- Spring MVC多视图
- Spring MVC 中自定义视图 @Component 及 配置 多个视图解析器 Excel视图
- 在spring MVC中配置多个视图
- Spring MVC配置多个视图解析器(FreeMarker,JSP)
- Spring MVC 3.1多视图协商配置(json、xml、freemarker)
- Spring MVC 多视图协商配置(json、xml、freemarker)
- Spring MVC 3.1多视图协商配置(json、xml、freemarker)
- Spring mvc 配置多视图 jsp freemarker velocity等
- Spring MVC多视图配置(jsp, freemarker, Velocity)
- Spring MVC 多个视图解析器配置
- Spring MVC Rest Json,XML多视图解析 配置
- Android4.X 的SIM卡信息的初始化过程
- 将excel数据通过plsql导入oracle的问题
- 加载资源进度条
- 一些资料书签(备份)
- 栈(C++实现)
- spring mvc 多视图配置
- 手机短信记录丢失如何恢复
- HTML +JavaScript 正则表达式来验证信息
- Windows 下单机最大TCP连接数
- 强制td不换行
- 快速排序算法
- 限制UITextField的输入字数
- compile/link prebuilt nonstatic java library failed?
- Windows 7 IIS (HTTP Error 500.21 - Internal Server Error)解决