`

velocity输出csv的一种做法

    博客分类:
  • java
阅读更多

 

使用spring mvc + velocity做项目时,输出csv文件做法是在controller自行调用httpResponse输出。

如果希望通过velocity输出csv内容,以下提供一种实现方式

 

 

1.新增ToCSVView,继承自VelocityView,重写rander方法,内容为设置http header与content type

public class ToCSVView extends VelocityView {
	
	/**
	 * Process the model map by merging it with the Velocity template.
	 * Output is directed to the servlet response.
	 * <p>This method can be overridden if custom behavior is needed.
	 */
	protected void renderMergedTemplateModel(
			Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {

		exposeHelpers(model, request);

		Context velocityContext = createVelocityContext(model, request, response);
		exposeHelpers(velocityContext, request, response);
		exposeToolAttributes(velocityContext, request);

		
		doRender(velocityContext, response);
	
		//以下为我们自行的修改
		String fileName = "data.csv";
		
		response.setContentType("text/plain;charset=gbk");
		response.setHeader("Content-Disposition", "attachment;filename=\""
				+ fileName + "\"");

		// 确保IE识别本次为下载文件
		response.setHeader("Content-Transfer-Encoding", "binary");
		response.setHeader("Cache-Control",
				"must-revalidate, post-check=0, pre-check=0");
		response.setHeader("Pragma", "public");
	}
}

 

2.新增ToCSVViewResolver,继承自VelocityViewResolver,重写loadView方法,后缀是csv进行渲染。

public class ToCSVViewResolver extends org.springframework.web.servlet.view.velocity.VelocityViewResolver implements Ordered {
	public static final String VIEW_CSV = ".csv"; 
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.view.AbstractCachingViewResolver#loadView(java.lang.String, java.util.Locale)
	 */
	@Override
	protected View loadView(String viewName, Locale locale) throws Exception {
		if(viewName.endsWith(VIEW_CSV)) {
			AbstractUrlBasedView view = buildView(viewName);
			return (View) getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName);
		}
		return null;
	}

 

3.ToCSVViewResolver重写buildView方法,使用我们的ToCSVView进行渲染,从而实现content-type和http头的修改

protected AbstractUrlBasedView buildView(String viewName) throws Exception {
		ToCSVView view = (ToCSVView) super.buildView(viewName);
		view.setVelocityFormatterAttribute(this.velocityFormatterAttribute);
		view.setDateToolAttribute(this.dateToolAttribute);
		view.setNumberToolAttribute(this.numberToolAttribute);
		if (this.toolboxConfigLocation != null) {
//			((VelocityToolboxView) view).setToolboxConfigLocation(this.toolboxConfigLocation);
		}
		return view;
	}

 

4.ToCSVViewResolver参数配置拷贝自velocity配置

<bean id="toCSVViewResolver" class="view.ToCSVViewResolver">
  <property name="prefix" value="/"></property>
  <property name="suffix" value=".vm"></property>
  <property name="exposeSpringMacroHelpers" value="true" />
  <property name="exposeRequestAttributes" value="true" />
  <property name="exposeSessionAttributes" value="true" />
  <property name="allowRequestOverride" value="true" />
  <property name="allowSessionOverride" value="true" />
  <property name="contentType" value="text/plain;charset=gbk" />
  <property name="order" value="5"></property>
 </bean>

 

5.检查dispatcher-servlet是否接收了csv后缀的请求,检查完毕就可以测试了~~写一个controller,页面为vm,打开后识别为下载文件

 

 test.vm文件

日期,数量
#foreach($info in $infoList)
$info.date,$info.count
#end

 

 

 

  

  • 大小: 12.4 KB
分享到:
评论

相关推荐

    Velocity模板引擎Velocity模板引擎

    Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎

    velocity实例

    velocity 代码生成工具,生成getter、setter、toString等方法。可以自己配置生成规则。

    Velocity-API.chm--采用官方文档制作-超值的Velocity API参考文档。

    Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提 供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。 Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)...

    Velocity1.4

    Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity

    Velocity+Struts 配置

    Velocity可用来从模板产生web 页面,SQL, PostScript以及其他输出。他也可用于一个独立的程序以产生源代码和报告,或者作为其它系统的一个集成组件。这个项目完成后,Velocity将为Turbine web 应用程序框架提供模板...

    velocity 用户手册 参考文档 详细讲解

    Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。 Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)...

    velocity-1.5.jar.zip( 359 k)

    Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。 Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生...

    Velocity 和 FreeMarker区别

    Velocity 和 FreeMarker区别 对于大部分的应用来说,使用 FreeMarker 比 Velocity 更简单,因为 Velocity 还必须编写一些自定义的

    Java进阶教程Velocity快速掌握模板引擎视频

    本课程从velocity engine也就是velocity引擎开始, 先讲解velocity的基本使用以及基础语法 , 然后再讲解velocity 的进阶内容velocity Tools , 以及velocity作为web项目的视图改如何使用 , 每一部分都会有一个综合案例...

    模板引擎 Velocity 官方最新版下载

    OSChina 就是采用 velocity 作为页面模板 Velocity是一个基于java的模板引擎...Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提 供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。

    Velocity框架学习教程

    Velocity教程

    Velocity学习指南(中文版).rar

    Velocity可用来从模板产生web 页面,SQL,PostScript以及其他输出。他也可用于一个独立的程序以产生源代码和报告,或者作为其它系统的一个集成组件。这个项目完成后,Velocity将为Turbine web 应用程序框架提供模板...

    velocity标签 、velocity教程

    打开网址:http://www.web-tag.net/all_17.htm 就是velocity标签大全 、教程 或下载CHM格式帮助文档

    Velocity用户手册---中文版

    Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。 Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)...

    velocity语法

    注:在velocity中使用$2.5这样的货币标识是没有问题得的,因为velocity中的变量总是以一个大写或者小写的字母开始的。 (2) 变量规范的写法 ${name} ,也可以写成:$name。提倡用前面的写法。 例如:你希望通过一个...

    velocity 电子书

    velocity 电子书velocity 电子书velocity 电子书

    velocity为空判断

    在使用velocity作为视图层时候,经常会碰到为空的判断,这里整理了velocity为空判断

    JAVAEE Velocity例子工程

    JAVAEE Velocity例子工程,一个简单的Velocity例子,用的都是代码,没有用Velocity配置文件

    Velocity入门教程(Spring集成Velocity)

    Velocity入门教程,语法,Velocity布局,Spring框架集成Velocity

    velocity的学习资料

    关于velocity的学习资料,velocity教程和velocity用户手册

Global site tag (gtag.js) - Google Analytics