Spring XML 视图解析器配置

发布于 2024-10-26 09:18:58 字数 2065 浏览 1 评论 0原文

我正在尝试使用 spring-mvc 将一些模型数据输出到 pdf 中。它不起作用,我想知道是否有人可以提供一些建议。

我有一个 spring-servlet.xml 文件,其中包含以下内容:

<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>

在 spring-pdf-views.xml 文件中,我有这个:

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

这是我的 MyPDFView 类:

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);
    }
}

最后在我的控制器中,我有:

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

我在输出中看到的问题是它永远不会到达 xmlViewResolver。它试图将 MyPDF 呈现为 JSTL 视图。这是来自我的日志:

org.springframework.web.servlet.DispatcherServlet - 渲染视图 [org.springframework.web.servlet.view.JstlView: name 'MyPDF'; DispatcherServlet 中名为“spring”的 URL [/WEB-INF/view/MyPDF.jsp]]

我缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆 2024-11-02 09:18:58

来自 InternalResourceViewResolver 的 Javadoc

注意:链接 ViewResolvers 时,InternalResourceViewResolver 始终需要放在最后,因为无论底层资源是否实际存在,它都会尝试解析任何视图名称。

交换解析器的顺序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文