【问题标题】:Using HttpServlet request and response objects with Spring MVC 3在 Spring MVC 3 中使用 HttpServlet 请求和响应对象
【发布时间】:2013-06-11 02:53:43
【问题描述】:

我在网上的 Spring MVC 示例中随处可见,我们可以在控制器的方法参数中使用 HttpServlet 请求和响应对象。但是当我使用它时。在下面的代码中给出。

  import org.springframework.stereotype.Controller;
  import org.springframework.ui.ModelMap;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.servlet.ModelAndView;

  @Controller
  public class StudentController {

  public void testSyntax(HttpServletRequest request, HttpServletResponse response)
  {
System.out.println("Inside testSyntax");
  }

  }

编译器抛出错误。 HttpServletRequest 请求无法解析为类型。 我正在使用 Spring MVC 3.0。谁能告诉我原因。

【问题讨论】:

  • 在控制台异常是 java.lang.ClassNotFoundException: HttpServletRequest

标签: spring-mvc spring-3


【解决方案1】:

您缺少几个 import 语句。

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

您从何处获取这些类通常因您使用的每个应用程序容器而异。我最喜欢的例子是 Tomcat:

<tomcat_base_dir>/lib/servlet-api.jar

【讨论】:

  • 感谢您的帮助,我是否必须显式添加 servlet.api jar?它不在 Spring MVC jars 中吗??
猜你喜欢
  • 2011-01-15
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
相关资源
最近更新 更多