我是Spring技术的新手。当我尝试使用Tomcat运行项目时,我创建了一个Spring MVC项目,我在eclipse控制台中遇到以下错误。请帮助我,我在谷歌搜索但尝试了几个选项,但无能为力。我不知道哪里出了问题......
org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/controller/] in DispatcherServlet with name 'SpringMVC'
@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";
}
}
<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>
<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>
答案 0 :(得分:-1)
您没有控制器的映射。现在,您只有/student
GET
和/addStudent
为POST
。即使您将@RequestMapping("/controller")
添加到控制器类,它也无法正常工作,因为/controller
仍然没有映射。
如您所见,映射将适用于/controller/student
和/controller/addStudent
。我建议您将请求网址更改为/student
和/addStudent
,或者如果您需要此/controller
网址,则应该有其他方法,如下所示
@RequestMapping("controller")
public String anyMethodName(Model model) {
//code goes here
return "anythink";
}