Spring XML 视图解析器配置
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 InternalResourceViewResolver 的 Javadoc:
交换解析器的顺序。
From the Javadoc for InternalResourceViewResolver:
Swap the order of your resolvers.