如何在Spring MVC上使用Itext打印pdf报告?

时间:2016-03-03 07:53:09

标签: java spring-mvc pdf printing

我一步一步地关注this tutorial,但由于某种原因无法让它发挥作用。

我完成了导游所说的步骤。但是当我调试它时,我发现控制器没有正确转发:

return new ModelAndView("pdfView", "listBooks", listBooks);

我在那行代码中看到:source is not found.

由于属性文件设置,它应该转到PDFBuilder类: EDIT1这里是观点属性:     pdfView。(class)= iText.PDFBuilder

此外,最终错误为.../pdfView.jsp not foundnull,如果我要遵循该教程,则该错误甚至不存在。

有人有任何建议吗?

我已按照说明将.properties文件放在src文件夹中。

EDIT1: 1)这是我的spring-mvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="iText" />

   <bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="order" value="1"/>
        <property name="basename" value="views"/>
    </bean>

    <bean id="viewResolver2"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="2"/>
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

2)我使用jetty 6.1.26在本地运行应用程序

3)这是完整的错误:

2016-03-03 11:18:14.560:WARN:org.apache.jasper.servlet.JspServlet:PWC6117: File "C:[path_to_directory]\src\main\webapp\WEB-INF\views\jsp\pdfView.jsp" not foundnull

我应该注意,这也是一个maven项目,我基本上是在我的项目中实现这个解决方案。我只与视图和控制器中的原始教程不同。我从我自己的视图中实现了调用,我只添加了这个方法:

@RequestMapping(value = "/downloadPDF", method = RequestMethod.GET)
    public ModelAndView downloadExcel() {
        // create some sample data
        List<Book> listBooks = new ArrayList<Book>();
        listBooks.add(new Book("Spring in Action", "Craig Walls", "1935182358",
                "June 29th 2011", 31.98F));
        listBooks.add(new Book("Spring in Practice", "Willie Wheeler, Joshua White",
                "1935182056", "May 16th 2013", 31.95F));
        listBooks.add(new Book("Pro Spring 3",
                "Clarence Ho, Rob Harrop", "1430241071", "April 18th 2012", 31.85F));
        listBooks.add(new Book("Spring Integration in Action", "Mark Fisher", "1935182439",
                "September 26th 2012", 28.73F));

        // return a view which will be resolved by an excel view resolver
        return new ModelAndView("pdfView", "listBooks", listBooks);
    } 

到我自己现有的控制器。

EDIT2

这是我的spring-web-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Scan the JavaConfig -->
    <context:component-scan base-package="[package]].form.config" />

    <bean name="/welcome.htm" 
        class="[package].form.web.UserInfoController" />
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- max upload size in bytes -->
        <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
        <!-- max size of file in memory (in bytes) -->
        <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

    </bean>
</beans>

0 个答案:

没有答案