8. Spring MVC4.1-ContentNegotiatingViewResolver

翻译 2015年11月19日 21:52:09
  • ContentNegotiatingViewResolver支持在Spring MVC下输出不同的格式;
  • ContentNegotiatingViewResolver是ViewResolver的一个实现;
  • ContentNegotiatingViewResolver使用request的媒体类型,根据扩展名选择不同的view输出不同的格式;
  • ContentNegotiatingViewResolver不是自己处理view,而是代理给不同的ViewResolver来处理不同的view;
  • 本例演示根据扩展名获得不同的页面;
  • jsp
    • view:WEB-INF/views/demoObj.jsp
    • viewResolver:UrlBasedViewResolverInternalResourceViewResolver
  • pdf
    • view:PdfView
    • viewResolver:PdfViewResolver
  • json
    • view:MappingJackson2JsonView
    • viewResolver:JsonViewResolver
  • xml
    • view:MarshallingView
    • viewResolver:XmlViewResolver
  • xls
    • view:XlsView
    • viewResolver:XlsViewResolver

8.2 示例

8.2.1 添加依赖

  • 依赖

    • json依赖03点睛Spring MVC 4.1-REST已添加
    • xml
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring-framework.version}</version>
    </dependency>
    • pdf
    <dependency>
      <groupId>com.lowagie</groupId>
      <artifactId>itext</artifactId>
      <version>4.2.1</version>
    </dependency>
    
    • xls
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.10-beta2</version>
    </dependency>

8.2.2 定制view和viewResolve

8.2.2.1 pdf

8.2.2.1.1 view
 package com.wisely.view;

import java.awt.Color;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.wisely.domain.DemoObj;

public class PdfView extends AbstractPdfView {

    @Override
    protected void buildPdfDocument(Map<String, Object> model,
            Document document, PdfWriter writer, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        DemoObj demoObj = (DemoObj) model.get("demoObj");

        PdfPTable table = new PdfPTable(2);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.getDefaultCell().setBackgroundColor(Color.lightGray);

        table.addCell("ID");
        table.addCell("NAME");

        table.addCell(demoObj.getId().toString());
        table.addCell(demoObj.getName());

        document.add(table);
    }

}
8.2.2.1.2 viewResolver
package com.wisely.viewresolver;

import java.util.Locale;

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

import com.wisely.view.PdfView;

public class PdfViewResolver implements ViewResolver{

    @Override
    public View resolveViewName(String viewName, Locale locale)
            throws Exception {
        return new PdfView();
    }


}

8.2.2.2 xls

8.2.2.2.1 view
package com.wisely.view;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.wisely.domain.DemoObj;

public class XlsView extends AbstractExcelView{

    @Override
    protected void buildExcelDocument(Map<String, Object> model,
            HSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
         DemoObj demoObj = (DemoObj) model.get("demoObj");

            Sheet sheet = workbook.createSheet("sheet1");
            CellStyle style = workbook.createCellStyle();
            style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.index);
            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
            style.setAlignment(CellStyle.ALIGN_CENTER);
            Row row = null;
            Cell cell = null;
            int rowCount = 0;
            int colCount = 0;

            // 创建头部
            row = sheet.createRow(rowCount++);

            cell = row.createCell(colCount++);
            cell.setCellStyle(style);
            cell.setCellValue("ID");

            cell = row.createCell(colCount++);
            cell.setCellStyle(style);
            cell.setCellValue("NAME");


            // 创建数据
            row = sheet.createRow(rowCount++);
            colCount = 0;
            row.createCell(colCount++).setCellValue(demoObj.getId());
            row.createCell(colCount++).setCellValue(demoObj.getName());
            for (int i = 0; i < 3; i++)
                sheet.autoSizeColumn(i, true);

    }

}
8.2.2.2.2 viewResolver
package com.wisely.viewresolver;

import java.util.Locale;

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

import com.wisely.view.XlsView;

public class XlsViewResolver implements ViewResolver{

    @Override
    public View resolveViewName(String viewName, Locale locale)
            throws Exception {
        return new XlsView();
    }

}

8.2.2.3 json

8.2.2.3.1 view

使用的是MappingJackson2JsonView

8.2.2.3.2 viewResolver
package com.wisely.viewresolver;

import java.util.Locale;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

public class JsonViewResolver implements ViewResolver{

    @Override
    public View resolveViewName(String viewName, Locale locale)
            throws Exception {
        MappingJackson2JsonView view = new MappingJackson2JsonView();
        view.setPrettyPrint(true);
        return view;
    }

}

8.2.2.4 xml

8.2.2.4.1 DemoBean的额外配置
package com.wisely.domain;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "demo")
public class DemoObj {
    private Long id;
    private String name;

    public DemoObj() {
        super();
    }

    public DemoObj(Long id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    @XmlElement
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

}
8.2.2.4.2 view

使用的是MarshallingView

8.2.2.4.3 viewResolver
package com.wisely.viewresolver;

import java.util.Locale;

import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.xml.MarshallingView;

import com.wisely.domain.DemoObj;

public class XmlViewResolver implements ViewResolver {

    @Override
    public View resolveViewName(String viewName, Locale locale)
            throws Exception {
        MarshallingView view = new MarshallingView();
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setClassesToBeBound(DemoObj.class);
        marshaller.setPackagesToScan("com.wisely.domain");
        view.setMarshaller(marshaller);
        return view;
    }

}

8.2.2.5 jsp

8.2.2.5.1 view

WEB-INF/views/demoObj.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
        id/name <br/>
        ${demoObj.id}/${demoObj.name}
</body>
</html>
8.2.2.5.2 viewResolver

UrlBasedViewResolverInternalResourceViewResolver

8.2.3 配置resolver

package com.wisely;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import com.wisely.interceptors.DemoInteceptor;
import com.wisely.viewresolver.JsonViewResolver;
import com.wisely.viewresolver.PdfViewResolver;
import com.wisely.viewresolver.XlsViewResolver;
import com.wisely.viewresolver.XmlViewResolver;

@Configuration
@ComponentScan("com.wisely")
@EnableWebMvc
public class DemoMVCConfig extends WebMvcConfigurerAdapter {

    //在此---jsp viewResolver
    @Bean
    public UrlBasedViewResolver viewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    // 注册自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInteceptor());
    }

    // 自定义拦截器
    @Bean
    public DemoInteceptor demoInteceptor() {
        return new DemoInteceptor();
    }

    // 静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
    }

    // 文件上传设置
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(1000000);
        return multipartResolver;
    }

    // 在此---配置ContentNegotiationManager,在无后缀名情况下默认为jsp view resolver
    @Override
    public void configureContentNegotiation(
            ContentNegotiationConfigurer configurer) {
        configurer.ignoreAcceptHeader(true).defaultContentType(
                MediaType.TEXT_HTML);
    }

    // 在此---配置ContentNegotiatingViewResolver,通过此代理到不同的viewResolover
    @Bean
    public ViewResolver contentNegotiatingViewResolver(
            ContentNegotiationManager manager) {

        ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
        resolver.setContentNegotiationManager(manager);
        List<ViewResolver> resolvers = new ArrayList<ViewResolver>();

        resolvers.add(xmlViewResolver());
        resolvers.add(jsonViewResolver());
        resolvers.add(viewResolver());// jsp view resolver
        resolvers.add(pdfViewResolver());
        resolvers.add(xlsViewResolver());

        resolver.setViewResolvers(resolvers);
        return resolver;
    }

    //在此---xml viewResolver
    @Bean
    public ViewResolver xmlViewResolver() {
        return new XmlViewResolver();
    }

    //在此---json viewResolver
    @Bean
    public ViewResolver jsonViewResolver() {
        return new JsonViewResolver();
    }
    //在此---pdf viewResolver
    @Bean
    public ViewResolver pdfViewResolver() {
        return new PdfViewResolver();
    }
    //在此---excel viewResolver
    @Bean
    ViewResolver xlsViewResolver() {
        return new XlsViewResolver();
    }

}

8.2.4 演示控制器

package com.wisely.web;

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

import com.wisely.domain.DemoObj;

@Controller
public class ContentController {
    @RequestMapping(value = "/getdemo",method = RequestMethod.GET)
    public String getDemo(Model model){
        DemoObj demoObj = new DemoObj(333l, "WYF");
        model.addAttribute("demoObj",demoObj);
        return "demoObj";

    }

}

8.2.5 结果

8.2.5.1 jsp

8.2.5.2 json

8.2.5.3 pdf

8.2.5.4 xml

相关文章推荐

spring mvc ContentNegotiatingViewResolver 根据路径后缀,选择不同视图

理论 public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport implements ViewR...

spring mvc4.2 ContentNegotiatingViewResolver 根据路径后缀,选择不同视图

spring mvc配置文件:

Spring MVC 之 ContentNegotiatingViewResolver-02_(ajax+json格式的输入和输出)

原文:http://starscream.iteye.com/blog/1067606   Spring mvc处理json需要使用jackson的类库,因此为支持json格式的输入输出需要先修改...

Spring mvc ContentNegotiatingViewResolver 根据路径后缀,选择不同视图

Spring mvc通过ContentNegotiatingViewResolver 实现根据路径后缀,选择不同视图

Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析

Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析

3.Spring MVC 4.1-@RequestMapping

2.1 @RequestMapping @RequestMapping是SpringMVC的核心注解,负责访问的url与调用方法之间的映射;@RequestMapping可以放在类和方法上; ...

6. Spring MVC 4.1-服务器端推送

6.1 服务器端推送 SSE(server send event)是一种服务器端向浏览器推送消息的技术,而不是我们常规的浏览器像server请求然后响应;当我们需要使...

Spring MVC 4.1+Mybatis ajax 异步返回Json 415问题解决方案

Spring MVC4.1+ Mybatis 使用ajax 异步返回Json 环境配置,以及使用过程中注意问题,参考示例代码,415问题的解决方案。...

spring mvc4.1.x如何使用s:mvcUrl

spring4.1的新特性,s:mvcURL如何使用,以及遇到错误如何进行修改

4. Spring MVC 4.1-REST

4.1 REST REST:Representational State Transfer;REST是一种数据导向web service,相对于SOAP是一种操作操作和处理导向的web servic...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:8. Spring MVC4.1-ContentNegotiatingViewResolver
举报原因:
原因补充:

(最多只允许输入30个字)