异常java.lang.NoSuchMethodError

时间:2012-06-19 14:39:05

标签: java exception spring-mvc

我正在尝试在服务器中上传pdf文件。我在控制器中使用以下代码块:

 @RequestMapping(value = /submit, method = RequestMethod.POST)
 public String upload(UploadItem uploadItem, BindingResult result, HttpServletRequest request, HttpSession session) {

   //some code here

   String name = request.getServletContext().getRealPath("/pdf/" + filename);
   File dest = new File(name);
   try {
        file.transferTo(dest);
   }catch(Exception e){
        System.err.println(e);
   }

   return "redirect:/details";

我这样做是为了将pdf存储到pdf文件中。在我的localhost工作正常,但当我在服务器上执行此操作时,我采取以下例外:

exception

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext;
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:839)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext;
frontend.controller.EsteemRatingsController.handleFormUpload(EsteemRatingsController.java:113)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

如果我删除控制器类中上面提供的行正在工作(ofcource没有上传pdf)。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:21)

在servlet 3.0中引入了request.getServletContext()方法。确保您的容器/库支持该版本。

编辑:tomcat 6只有servlet 2.5,见http://tomcat.apache.org/whichversion.html

可以自动装配:ServletContext and Spring MVC

public class Xxxx{
    @Autowired
    ServletContext context;

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public String upload(UploadItem uploadItem, BindingResult result, HttpServletRequest request, HttpSession session) {

       //some code here

       String name = context.getRealPath("/pdf/" + filename);
...