概述

基于spring-mvc自定义视图,以BeanNameViewResolver作为解析器,以满足特殊需求。

本文以输出多个pdf文件的压缩文件,供前台下载的需求为例;但是不提供服务层实现。

实现

创建AbstractView的实现类

package cn.sinobest.jzpt.zfba.fzyw.xzfy.dfcl.view;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView;
import cn.sinobest.jzpt.zfba.fzyw.xzfy.dfcl.domain.DFCL;
import cn.sinobest.jzpt.zfba.fzyw.xzfy.dfcl.service.IZipService;
@Component("dfclView")
public class DFCLView extends AbstractView {
@Resource(name = "fzyw.xzfy.service.zip")
private IZipService zipService;
@Override
protected void renderMergedOutputModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
/* 1. 设置响应内容类型 */
response.setContentType("Application/Octet-stream;charset=utf-8");
/* 2. 将文件名加入响应头 */
String zipName = "材料压缩包" + System.currentTimeMillis() + ".zip";
zipName = new String(zipName.getBytes(), "ISO-8859-1");
response.addHeader("Content-Disposition", "attachment; filename="
+ zipName);
/* 3. 输出文件内容 */
@SuppressWarnings("unchecked")
List<DFCL> dfclList = (List<DFCL>) model.get("dfclList");
zipService.write(response.getOutputStream(), dfclList);
/* 4. 关闭流 */
response.getOutputStream().flush();
response.getOutputStream().close();
}
}

说明:

注解Component用于声明spring的bean

在Controller中返回视图bean的名称

private static final String VIEW_DEFAULT = "dfclView";
@RequestMapping("/download")
public String download(Model model, String writids) {
List<DFCL> dfclList = dfclService.getDFCL(writids);
model.addAttribute("dfclList", dfclList);
return VIEW_DEFAULT;
}

更多内容

注:下面的介绍多以项目框架中的配置为基础,具体实现类的代码不便贴出,权作参考

在spring-mvc中,controller返回的名称需要经过视图解析器的解析,解析器找到对应的视图,并调用对应的方法。

在项目中,一般在Controller的方法返回jsp的相对路径,即能在前台得到对应的内容,是因为webmvc-config.xml中存在下述的配置:

<bean class="cn.sinobest.jzpt.framework.mvc.localization.ExtInternalResourceViewResolver">
<!--bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"-->
<property name="prefix" value=""/>
<property name="suffix" value=""/>
</bean>

而详表配置文件,通常是在jsp返回后根据form的formdata属性异步请求的,对应的controller方法返回"xmlView";前台可以得到对应的xml,并渲染视图,是因为webmvc-config.xml中存在下述的配置:

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

BeanNameViewResolver解析器试图将返回的名称当作视图bean的name,而webmvc-config.xml中又配置了"xmlView"的视图:

<bean name="xmlView" class="cn.sinobest.jzpt.framework.mvc.legacysystemsmac.xml.LegacySystemsXMLView">
<property name="xmlTransformer">
<bean class="cn.sinobest.jzpt.framework.mvc.legacysystemsmac.xml.LegacySystemsXMLTransformer"/>
</property>
</bean>

具体研究详表配置文件的返回,就要看LegacySystemsXMLView和LegacySystemsXMLTransformer的源代码了。

当需要返回json的时候,controller方法一般返回"jsonView",是因为webmvc-config.xml中存在了BeanNameViewResolver的配置,还有"jsonView"的配置:

<bean name="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="encoding">
<value type="org.codehaus.jackson.JsonEncoding">UTF8</value>
</property>
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>

spring提供了一套View和ViewResolver的继承体系

参考spring自定义视图可以获取更多的内容。

基于BeanNameViewResolver解析器,自定义视图的更多相关文章

  1. SpringMVC系列(七)视图解析器和视图

    在springmvc.xml里面配置视图解析器 <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 --> <bean class="org ...

  2. Spring 梳理 - 视图解析器 VS 视图(View,ViewResolver)

    View View接口表示一个响应给用户的视图,例如jsp文件,pdf文件,html文件等 该接口只有两个方法定义,分别表明该视图的ContentType和如何被渲染 Spring中提供了丰富的视图支 ...

  3. SpringMVC 视图和视图解析器&表单标签

    视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView 对象.对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它 ...

  4. SpringMVC 视图解析器

    SpringMVC 视图解析器 还记得SpringMVC 快速入门中,dispatcher-servlet.xml 配置的视图解析器么.它是SpringMVC 的核心知识点.本章节比较简单,明白视图解 ...

  5. SpringMVC源码阅读:视图解析器

    1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将通过源码(基于Spring ...

  6. SpringMVC——视图和视图解析器

    请求处理方法执行完成后,最终返回一个 ModelAndView对象.对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它们装配成一个Mode ...

  7. springmvc自定义视图

    自定义视图 可以整合jfreechart.excel @RequestMapping("/testView") public String testView(){ System.o ...

  8. SpringMvc配置自定义视图

    1.在dispatcherServlet-servlet.xml配置自定义视图 <!-- 配置视图 BeanNameViewResolver 解析器: 使用视图的名字来解析视图 --> & ...

  9. Spring MVC 自定义视图

    实现View import org.springframework.stereotype.Component; import org.springframework.web.servlet.View; ...

随机推荐

  1. 洛谷 P2800 又上锁妖塔

    https://www.luogu.org/problem/show?pid=2800 题目背景 小D在X星买完了想要的东西,在飞往下一个目的地的途中,正无聊的他转头看了看身边的小A,发现小A正在玩& ...

  2. 【hdu3033】分组背包(每组最少选一个)

    [题意] 有S款运动鞋,一个n件,总钱数为m,求不超过总钱数且每款鞋子至少买一双的情况下,使价值最大.如果有一款买不到,就输出“Impossible". 1<=N<=100  1 ...

  3. 【51NOD-0】1049 最大子段和

    [算法]DP [题解]开long long…… #include<cstdio> #include<algorithm> #include<cstring> usi ...

  4. php trait 变量类型为数组时 不能被父类子类同时use

    直接上代码 --------------------------- trait T1 { public static $a=1; public static $b= []; public static ...

  5. 2、MySQL常见数据库引擎及比较?

    MySQL存储引擎简介 MySQL支持数个存储引擎作为对不同表的类型的处理器.MySQL存储引擎包括处理事务安全表的引擎和处理非事务安全表的引擎: MyISAM管理非事务表.它提供高速存储和检索,以及 ...

  6. 大话Spring Cloud

    研究了一段时间Spring Boot了准备向Spring Cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  7. linux c 执行新程序

    学习进程时,linu c上说新开的进程一般要执行另外一个程序,同时与父进程执行同一个程序没有意义 如下是如何执行一个新的程序 使用exec函数簇 exec函数簇包含如下函数

  8. 限制printk打印频率函数printk_ratelimit【转】

    转自:http://blog.csdn.net/lkkey80/article/details/45190095 版权声明:博文地址 http://blog.csdn.net/lkkey80?view ...

  9. 原始套接字&&数据链路层访问

    1. 原始套接字能力: (1) 进程可以读写ICMP,IGMP等分组,如ping程序: (2) 进程可以读写内核不处理协议字段的ipv4数据报:如OSPF等: (3) 进程可以使用IP_HDRINCL ...

  10. 手把手教你写Linux设备驱动---中断(三)--workqueue实现(基于友善之臂4412开发板) 【转】

    转自:http://blog.csdn.net/morixinguan/article/details/69680909 上节,我们讲到如何来实现tasklet小任务机制 http://blog.cs ...