我正在尝试使用 Spring MVC 建立一个项目。启动服务器时一切正常。 (例如调用 Controller 的 PostConstruct 方法)。
无论如何,当我尝试在我的浏览器中访问一个测试页面时,我得到一个 404 并且在控制台中出现这个错误: 2014 年 4 月 26 日下午 4:51:35 org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:在名称为“dispatcherServlet”的 DispatcherServlet 中找不到带有 URI [/SUP-Verwaltung/test] 的 HTTP 请求的映射
Controller :
package com.patrickzinner.controller;
@Controller
public class TestController {
@PostConstruct
public void init() {
// this method is called
System.out.println("test");
}
@RequestMapping("/test")
public String test() {
// when i enter localhost:8080/SUP-Verwaltung/test, i get the error
return "hello";
}
}
网络.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SUP Verwaltung</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
mvc-config.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.patrickzinner.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
希望这就是您所需要的一切。 提前致谢。
最佳答案
添加<mvc:annotation-driven />
到您的 mvc-config.xml 并确保 jsp 文件 hello.jsp 在 WEB-INF/view 下
关于java - Tomcat Spring MVC : Request Mapping does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23312666/