Patrick Zinner
Patrick Zinner

Reputation: 364

Tomcat Spring MVC: Request Mapping does not work

I'm trying to set up a Project with Spring MVC. Everything works quite well while starting the server. (e.g. PostConstruct methods of Controllers are called).

Anyway, when I try to access a testpage in my browser, I get a 404 and this error in the console: Apr 26, 2014 4:51:35 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound Warnung: No mapping found for HTTP request with URI [/SUP-Verwaltung/test] in DispatcherServlet with name 'dispatcherServlet'

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

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>

I hope this is everything you need. Thanks in advance.

Upvotes: 0

Views: 1860

Answers (2)

ravi ranjan
ravi ranjan

Reputation: 6119

you are using @Controller and @PostConstruct

so for using it just define <mvc:annotation-driven />

in your mvc-config.xml file

Upvotes: 0

geoand
geoand

Reputation: 63991

Add <mvc:annotation-driven /> to your mvc-config.xml and make sure that the jsp file hello.jsp is under WEB-INF/view

Upvotes: 1

Related Questions