Tomcat Spring MVC:请求映射不起作用

时间:2014-04-26 15:04:41

标签: java tomcat spring-mvc

我正在尝试使用Spring MVC建立一个项目。启动服务器时,一切都运行良好。 (例如,调用控制器的PostConstruct方法)。

无论如何,当我尝试在浏览器中访问测试页时,我在控制台中收到404错误: 2014年4月26日下午4:51:35 org.springframework.web.servlet.DispatcherServlet noHandlerFound Warnung:在DispatcherServlet中找不到带有URI [/ SUP-Verwaltung / test]的HTTP请求的映射,名称为“dispatcherServlet”

控制器:

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";
}
}    

的web.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>

我希望这就是你需要的一切。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

<mvc:annotation-driven />添加到mvc-config.xml并确保jsp文件hello.jsp位于WEB-INF / view

答案 1 :(得分:0)

您正在使用@Controller和@PostConstruct

所以使用它只需定义 <mvc:annotation-driven />

在你的mvc-config.xml文件中