SpringMVC:多视图[JSP,JSON,Excel,XML]混合输出__XML配置方式
简介:
SpringMVC:多视图[JSP,JSON,Excel,XML]混合输出__XML配置方式
maven 依赖
<!--XML视图-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!--XML视图(xstream)-->
<!--<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
<dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3_min</artifactId>
<version>1.1.4c</version>
</dependency>
<dependency>
<groupId>xmlpull</groupId>
<artifactId>xmlpull</artifactId>
<version>1.1.3.1</version>
</dependency>-->
<!--excel视图-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<!-- Spring MVC配置【Restful】开始 -->
<servlet>
<servlet-name>SpringMVC-Rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC-Rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--Spring MVC配置【Restful】结束-->
视图解析器
<!-- ContentNegotiatingViewResolver -->
<!-- 根据确定出的不同MIME名,使用不同视图解析器解析视图 -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<!-- 设置优先级 -->
<property name="order" value="1"/>
<!-- 设置默认的MIME类型,如果没有指定拓展名或请求参数,则使用此默认MIME类型解析视图 -->
<property name="defaultContentType" value="text/html" />
<!-- 是否不适用请求头确定MIME类型 -->
<property name="ignoreAcceptHeader" value="true"/>
<!--使用拓展名类型,不注释以下三行将使用参数(type)的方法访问-->
<!-- 是否根据路径拓展名确定MIME类型 -->
<!-- <property name="favorPathExtension" value="false" />-->
<!-- 是否使用参数来确定MIME类型 -->
<!-- <property name="favorParameter" value="false"/>-->
<!-- 上一个属性配置为true,我们指定type请求参数判断MIME类型 -->
<!--<property name="parameterName" value="type" />-->
<!-- 根据请求参数或拓展名映射到相应的MIME类型 -->
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
<entry key="excel" value="application/vnd.ms-excel"></entry>
</map>
</property>
<!-- 设置默认的候选视图,如果有合适的MIME类型,将优先从以下选择视图,找不到再在整个Spring容器里寻找已注册的合适视图 -->
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
<!-- JAXB XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.newcapec.cloudpay.service.base.RspBaseVO</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
<!-- Excel View -->
<bean class="com.newcapec.cloudpay.controller.util.CodeExcelView"></bean>
</list>
</property>
</bean>
<!-- View Resolver for JSPs -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="requestContextAttribute" value="rc"/>
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 资源映射 -->
<!-- <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> -->
<!-- <mvc:resources location="/js/" mapping="/js/**" /> -->
自定义Excel解析器
import com.newcapec.cloudpay.service.base.RspBaseVO;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
* @Title: 自定义返回结果-Excel视图
* @ClassName: com.newcapec.cloudpay.controller.util.CodeExcelView.java
* @Description:
*
* @Copyright2016-2017 - Powered By 研发中心
* @author: 王延飞
* @date: 2018-01-129:19
* @version V1.0
*/
publicclassCodeExcelViewextendsAbstractExcelView {
@Override
protectedvoidbuildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
RspBaseVOrspBaseVO= (RspBaseVO) model.get("RspBaseVO");
HSSFSheetsheet= workbook.createSheet("返回结果");//创建一页
HSSFRowheader= sheet.createRow(0);//创建第一行
header.createCell(0).setCellValue("返回码");
header.createCell(1).setCellValue("返回信息");
HSSFRowrow= sheet.createRow(1);
row.createCell(0).setCellValue(rspBaseVO.getCode());
row.createCell(1).setCellValue(rspBaseVO.getMsg());
}
}
实体
- import javax.xml.bind.annotation.XmlRootElement;
/**
* @Title: 返回响应值--基类
* @ClassName:RspBaseVO.java
* @Description:
*
* @Copyright2016-2017 - Powered By 研发中心
* @author: 王延飞
* @date:2018年1月12日 上午07:43:41
* @version V1.0
*/
@XmlRootElement
publicclassRspBaseVO {
private String code; // 返回码
private String msg; // 返回信息
// private String data; // 业务参数
public String getCode() {
return code;
}
publicvoidsetCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
publicvoidsetMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return"RspBaseVO [code=" + code + ", msg=" + msg + "]";
}
}
controller
/***
* .json(?type=json) -- 返回JSON
* .excel(?type=excel) -- 返回Excel
* .xml(?type=xml) -- 返回XML
* 为空-- 返回JSP
*/
@RequestMapping(value="/multiview/{code}/{msg}", method=RequestMethod.GET)
public String multiViewTest(@PathVariable String code,@PathVariable String msg, Model model) {
RspBaseVOrspBaseVO=newRspBaseVO();
rspBaseVO.setCode(code);
rspBaseVO.setMsg(msg);
model.addAttribute("RspBaseVO",rspBaseVO);
return"/com/test/codeTest";
}
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>多视图混合输出-状态码</title>
</head>
<body>
code : ${RspBaseVO.code} <br/>
msg : ${RspBaseVO.msg}
</body>
</html>