سؤال

I am trying to output some model data to a pdf using spring-mvc. It is not working and I was wondering if someone could offer some advice.

I have a spring-servlet.xml file that includes the following:

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

<bean id="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="order" value="2"/>
    <property name="location">
        <value>/WEB-INF/spring-pdf-views.xml</value>
    </property>
</bean>

In the spring-pdf-views.xml file I have this:

<bean id="MyPDF" class="com.example.MyPDFView"/>

This is my MyPDFView class:

public class MyPDFView extends AbstractPdfView {

    @Override
    protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        @SuppressWarnings("unchecked")
        Map<String, String> data = (Map<String, String>) model.get("modelData");

        Table table = new Table(2);
        table.addCell("Date");
        table.addCell("Name");
        table.addCell(data.get("modelData.dateValue"));
        table.addCell(data.get("modelData.nameValue"));

        document.add(table);
    }
}

Finally in my controller I have:

@RequestMapping(value="/pdfInformation", method=RequestMethod.POST)
public ModelAndView showPDF(ModelMap model, PDFInfo pdfInfo, BindingResult result) {
        return new ModelAndView("MyPDF", model);
}

The problem I am seeing in the output is that it never gets to the xmlViewResolver. It is trying to render the MyPDF as a JSTL View. This is from my logs:

org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'MyPDF'; URL [/WEB-INF/view/MyPDF.jsp]] in DispatcherServlet with name 'spring'

What am I missing?

هل كانت مفيدة؟

المحلول

From the Javadoc for InternalResourceViewResolver:

Note: When chaining ViewResolvers, an InternalResourceViewResolver always needs to be last, as it will attempt to resolve any view name, no matter whether the underlying resource actually exists.

Swap the order of your resolvers.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top