Spring XML View Resolver Configuration

家住魔仙堡 提交于 2019-12-23 18:43:58

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5393458/spring-xml-view-resolver-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!