使用spring mvc

时间:2016-05-10 08:24:07

标签: java excel spring maven spring-mvc

我是java开发人员,我使用spring mvc框架我想从jsp页面导出数据(从controler发送)我按照本教程工作 http://www.codejava.net/frameworks/spring/spring-mvc-with-excel-view-example-apache-poi-and-jexcelapi

但是我收到了这个错误:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'excelView': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Font

当我想使用“excel lib”poi-3.9.jar并将其作为依赖项放在pom.xml中

当我做“maven install”时,我收到了这个错误:

Failed to execute goal on project SpringMvcJdbcTemplate: Could not resolve dependencies for project net.codejava.spring:SpringMvcJdbcTemplate:war:1.0: Failed to collect dependencies at org.apache.poi:poi:jar:3.9: Failed to read artifact descriptor for org.apache.poi:poi:jar:3.9: Could not transfer artifact

org.apache.poi:poi:pom:3.9 from/to central 
(https://repo.maven.apache.org/maven2): 
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target -> [Help 1]

所以我以常规的方式添加库,它似乎很好,但是当我运行应用程序时,我得到了我在开头的错误

这是我的代码:

CONTROLER:

@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
public ModelAndView downloadExcel() {
    // create some sample data
   // return a view which will be resolved by an excel view resolver
    return new ModelAndView("excelView", "listContactings", listContact);
}

MvcConfigiration:

@Bean
    public ViewResolver getViewResolver(){
        ResourceBundleViewResolver resolver = new ResourceBundleViewResolver();
        resolver.setOrder(1);
        resolver.setBasename("views");
        return resolver;
    }
    @Bean
    public ViewResolver getViewResolver2(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setOrder(2);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

views.proprties:

excelView.(class)=net.codejava.spring.ExcelBuilder

ExcelBuilder.java:

package net.codejava.spring;

import java.util.Date;
import java.util.List;
import java.util.Map;

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

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import net.codejava.spring.model.Contacting;

public class ExcelBuilder extends AbstractExcelView {

    @Override
    protected void buildExcelDocument(Map<String, Object> model,
            HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // get data model which is passed by the Spring container
        List<Contacting> listContactings = (List<Contacting>) model.get("listContactings");

        // create a new Excel sheet
        HSSFSheet sheet = workbook.createSheet("Java Books");
        sheet.setDefaultColumnWidth(30);

        // create style for header cells
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("Arial");
        style.setFillForegroundColor(HSSFColor.BLUE.index);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
       font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.WHITE.index);
        style.setFont(font);

        // create header row

        HSSFRow header = sheet.createRow(0);

        header.createCell(0).setCellValue("contacting_id");
        header.getCell(0).setCellStyle(style);

        header.createCell(1).setCellValue("user_id");
        header.getCell(1).setCellStyle(style);

        header.createCell(2).setCellValue("subject");
        header.getCell(2).setCellStyle(style);

        header.createCell(3).setCellValue("location");
        header.getCell(3).setCellStyle(style);

        header.createCell(4).setCellValue("content");
        header.getCell(4).setCellStyle(style);

        header.createCell(5).setCellValue("department");
        header.getCell(5).setCellStyle(style);



        header.createCell(6).setCellValue("status");
        header.getCell(6).setCellStyle(style);




        header.createCell(7).setCellValue("contacting_date");
        header.getCell(7).setCellStyle(style);


        header.createCell(8).setCellValue("house_Number");
        header.getCell(8).setCellStyle(style);

        header.createCell(9).setCellValue("Urgency");
        header.getCell(9).setCellStyle(style);

        header.createCell(10).setCellValue("is_inspector");
        header.getCell(10).setCellStyle(style);

        header.createCell(11).setCellValue("inspectorStatus");
        header.getCell(11).setCellStyle(style);



        header.createCell(12).setCellValue("stringDate");
        header.getCell(12).setCellStyle(style);

        header.createCell(13).setCellValue("openedBy");
        header.getCell(13).setCellStyle(style);

        // create data rows
        int rowCount = 1;

        for (Contacting Contacting : listContactings) {
            HSSFRow aRow = sheet.createRow(rowCount++);
            aRow.createCell(0).setCellValue(Contacting.getContacting_id());
            aRow.createCell(1).setCellValue(Contacting.getUser_id());
            aRow.createCell(2).setCellValue(Contacting.getSubject());
            aRow.createCell(3).setCellValue(Contacting.getLocation());
            aRow.createCell(4).setCellValue(Contacting.getContent());
            aRow.createCell(5).setCellValue(Contacting.getdepartment());
            aRow.createCell(6).setCellValue(Contacting.getStatus());
            aRow.createCell(7).setCellValue(Contacting.getContacting_date());
            aRow.createCell(8).setCellValue(Contacting.getHouse_Number());
            aRow.createCell(9).setCellValue(Contacting.getUrgency());
            aRow.createCell(10).setCellValue(Contacting.getIs_inspector());
            aRow.createCell(11).setCellValue(Contacting.getInspectorStatus());
            aRow.createCell(12).setCellValue(Contacting.getStringDate());
            aRow.createCell(13).setCellValue(Contacting.getOpenedBy());



        }
    }
}

我认为库中的问题使用但我尝试了很多版本和许多方法来使用它但我总是会遇到错误。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试使用此依赖项并执行

  

mvn clean install

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>

我强烈建议当你开始使用maven作为依赖解析器时,继续使用它并且不要混合你自己和maven的库,因为你将来会发现问题。

尝试使用该版本执行,我们将看到异常,也许我们需要从poi依赖项中排除一些jar,因为已经包含