org.springframework.web.servlet.PageNotFound - 在名为'SpringMVC'的DispatcherServlet中找不到带有URI [/ controller /]的HTTP请求的映射

时间:2014-03-03 16:04:57

标签: java spring spring-mvc

我是Spring技术的新手。当我尝试使用Tomcat运行项目时,我创建了一个Spring MVC项目,我在eclipse控制台中遇到以下错误。请帮助我,我在谷歌搜索但尝试了几个选项,但无能为力。我不知道哪里出了问题......

错误:

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/controller/] in DispatcherServlet with name 'SpringMVC'

HomeController.java

@Controller
  public class HomeController {

@RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
   }

   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("SpringWeb")Student student, 
   ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());

      return "result";
   }

  }

的web.xml

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml</param-value>
 </context-param>

 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

用SpringMVC-servlet.xml中

  <mvc:annotation-driven />
  <context:component-scan base-package="com.spring.controller" />
  <resources mapping="/resources/**" location="/resources/" />
  <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
  </beans:bean>

1 个答案:

答案 0 :(得分:-1)

您没有控制器的映射。现在,您只有/student GET/addStudentPOST。即使您将@RequestMapping("/controller")添加到控制器类,它也无法正常工作,因为/controller仍然没有映射。 如您所见,映射将适用于/controller/student/controller/addStudent。我建议您将请求网址更改为/student/addStudent,或者如果您需要此/controller网址,则应该有其他方法,如下所示

@RequestMapping("controller")
public String anyMethodName(Model model) {
    //code goes here
    return "anythink";
}