Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 如何在SpringMVC中显示pdf文件_Spring Mvc_Itext - Fatal编程技术网

Spring mvc 如何在SpringMVC中显示pdf文件

Spring mvc 如何在SpringMVC中显示pdf文件,spring-mvc,itext,Spring Mvc,Itext,我想在按下按钮或链接时显示pdf文件,这是pdf文件的位置:/opt/tomcat/pdf/test.pdf 请帮帮我,我试过很多方法,但都不行 我创建了一个这样的链接 <a href="/admin-teknikal/viewpdf/${asas.foto_istlsi}">${asas.foto_istlsi}</a> Apr 13, 2016 11:19:36 PM org.apache.catalina.core.StandardWrapperValve inv

我想在按下按钮或链接时显示pdf文件,这是pdf文件的位置:/opt/tomcat/pdf/test.pdf

请帮帮我,我试过很多方法,但都不行

我创建了一个这样的链接

<a href="/admin-teknikal/viewpdf/${asas.foto_istlsi}">${asas.foto_istlsi}</a>
Apr 13, 2016 11:19:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/admin-teknikal] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry.<init>()] with root cause
java.lang.NoSuchMethodException: org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:104)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
但我得到了这样的错误

<a href="/admin-teknikal/viewpdf/${asas.foto_istlsi}">${asas.foto_istlsi}</a>
Apr 13, 2016 11:19:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/admin-teknikal] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry.<init>()] with root cause
java.lang.NoSuchMethodException: org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:104)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
2016年4月13日11:19:36 PM org.apache.catalina.core.StandardWrapper
严重:路径为[/admin teknikal]的上下文中Servlet[mvc dispatcher]的Servlet.service()引发异常[请求处理失败;嵌套异常为org.springframework.beans.beanInstationException:无法实例化bean类[org.springframework.web.Servlet.config.annotation.ResourceHandlerRegistry]:未找到默认构造函数;嵌套异常为java.lang.NoSuchMethodException:org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry.()],并带有根本原因
java.lang.NoSuchMethodException:org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry.()
位于java.lang.Class.getConstructor0(Class.java:3082)
位于java.lang.Class.getDeclaredConstructor(Class.java:2178)
位于org.springframework.beans.BeanUtils.InstanceClass(BeanUtils.java:105)
位于org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:138)
位于org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
位于org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:104)
位于org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
位于org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
位于org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
位于org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
位于org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
位于org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
位于org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
位于org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
位于org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
位于javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
位于org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
位于javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

正如我在评论中所说的,使用SpringMVC服务文件系统中的文件没有什么特别之处(这是基于@balusc的想法)。但如果您真的想使用Spring MVC特定的解决方案,您可以为此配置资源处理程序:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
      .addResourceHandler("/pdf/**")
      .addResourceLocations("file:/opt/tomcat/pdf/");
 }

您可以在@eugen的博客文章中找到更多信息:

这里没有什么特别的(和Spring MVC特有的):您需要提供此文件并正确设置标题(至少,
内容类型
)。互联网上有很多关于静态文件服务的文章(大部分时间是图片)。我已经更新了我的帖子,我听从了你的建议,但出现了一个错误,我是一个春天的初学者。我需要您的帮助您在错误的位置配置注册表(它不应该在控制器中)。请参阅文档: