我想在按下按钮或链接时显示pdf文件,这是pdf文件的位置:/opt/tomcat/pdf/test.pdf
请帮助我,我尝试了很多方法,但不能
我已经创建了像这样的链接
<a href="/admin-teknikal/viewpdf/${asas.foto_istlsi}">${asas.foto_istlsi}</a>
单击链接时,将显示pdf文件
这是我的控制器
@Controller
public class viewpdf {
@RequestMapping(value = "/viewpdf/{foto_istlsi}", method = RequestMethod.GET)
public void addResourceHandlers(ResourceHandlerRegistry registry,
@PathVariable("foto_istlsi") String pdf) {
registry
.addResourceHandler("/files/**")
.addResourceLocations("file:/opt/tomcat/pdf/"+pdf);
}
}
但是我收到了这样的错误
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)
答案 0 :(得分:0)
正如我在评论中所说,使用Spring MVC从文件系统提供文件没有什么特别的(这是how I do it myself;基于来自@balusc的ImageServlet
的想法。但是如果你真的想使用Spring MVC特定的解决方案,你可以为它配置资源处理程序:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/pdf/**")
.addResourceLocations("file:/opt/tomcat/pdf/");
}
您可以在@eugen的博文中找到更多信息:Serve Static Resources with Spring