我正在开发一个Spring Boot应用程序,我试图使用XmlViewResolver使用itext从预定义的模板查看pdf。当我访问“ / welcome”和“ / invoice”时成功获取jsp页面,但是当我尝试“ / pdf”时失败。 请让我知道怎么了。为什么pdf Bean无法正常工作?
白标错误页面
此应用程序没有针对/ error的显式映射,因此您将其视为备用。
发生意外错误(类型=未找到,状态= 404)。
/WEB-INF/jsp/pdf.jsp
下面是代码
@Controller
public class MainController {
@RequestMapping("/welcome")
public String hello(Model model) {
model.addAttribute("msg", "XmlViewResolver Demo");
return "success";
}
@RequestMapping("/invoice")
public String getInvoiceInfo() {
return "invoice";
}
@RequestMapping("/pdf")
public String getPdfInfo() {
return "pdf";
}
}
JspConfig.java
@Component
public class JspConfig {
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/jsp/");
bean.setSuffix(".jsp");
bean.setOrder(2);
return bean;
}
}
XmlConfig.java
@Configuration
public class XmlConfig {
@Bean
public XmlViewResolver xmlViewResolver() {
XmlViewResolver resolver = new XmlViewResolver();
Resource resource = new ClassPathResource("xml/user-view.xml");
resolver.setLocation(resource);
resolver.setOrder(1);
return resolver;
}
}
user-view.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="invoice" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="WEB-INF/template/invoice.jsp" />
</bean>
<bean id="pdf" class="com.example.config.PdfView">
<property name="url" value="WEB-INF/template/invoiceTemplate.pdf" />
</bean>
PdfView.java
import com.lowagie.text.pdf.PdfStamper;
public class PdfView extends AbstractPdfStamperView implements MessageSourceAware {
@Override
protected void mergePdfDocument(Map<String, Object> model, PdfStamper stamper, HttpServletRequest request, response) throws Exception {
stamper.setFormFlattening(true);
String customerName = (String) model.get("customerName");
stamper.getAcroFields().setField("name", customerName);
stamper.close();
}
}