2

I'm having a strange problem with Spring MVC. I have a simple controller like this:

@Controller
@RequestMapping("admin")
public class AdminController {

@RequestMapping(value = "", method = RequestMethod.GET)
public String home() {
    return "home";
}

When I run my server and access the url: localhost/admin I get a 404 error. The view home.jsp exists and should be rendered. When I check my spring event log this is what shows up:

DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/admin]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /admin
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String be.roots.buildinginspector.web.controller.AdminController.home()]
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'adminController'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/admin] is: -1
DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [home]] in DispatcherServlet with name 'appServlet'
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'domainOfExpertise' of type [be.roots.buildinginspector.business.model.DomainOfExpertise] to request in view with name 'home'
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'org.springframework.validation.BindingResult.domainOfExpertise' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'home'
DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [home] in InternalResourceView 'home'
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/home]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /home
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/home]
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/home] in DispatcherServlet with name 'appServlet'

Everything is handled correctly but instead of just showing the view, the DispatcherServlet makes a new GET request to the url of the requested view name.

My web.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/spring/config-core-business.xml
                 classpath*:/spring/config-app-security.xml
    </param-value>
</context-param>

<!-- Spring Security filter -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring/appServlet/config-core-web.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Relevant spring context parts (config-core-web.xml):

<resources mapping="/resources/**" location="../../../resources" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources
     in the /WEB-INF/views directory -->
<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>
Geoffrey De Vylder
  • 3,963
  • 7
  • 36
  • 56
  • I had the same problem, tank you, by the way, i can see you are reading Apress.Pro.Spring.3.Apr.2012. – OJVM Oct 19 '12 at 14:05
  • possible duplicate of [No mapping found for HTTP request with URI \[/WEB-INF/pages/apiForm.jsp\]](http://stackoverflow.com/questions/1266303/no-mapping-found-for-http-request-with-uri-web-inf-pages-apiform-jsp) – C. Ross Feb 03 '13 at 22:18

5 Answers5

3
@Controller
@RequestMapping("admin")
public class AdminController {

@RequestMapping(method = RequestMethod.GET)
public String home() {
    return "home";
}

remove 'value' property of @RequestMapping for home() function.

Sachin J
  • 2,081
  • 12
  • 36
  • 50
1

After all it turned out this was a tomcat-related issue. For some reason when I recreated the configuration in my IDE the error was resolved. Thanks for your help.

Geoffrey De Vylder
  • 3,963
  • 7
  • 36
  • 56
  • For future visitors, see [No mapping found for HTTP request with URI](http://stackoverflow.com/a/2923511/16487). – C. Ross Feb 03 '13 at 22:17
0

I think this is servlet mapping problem in web.xml. Change it in web.xml to /admin addreses only. Perhaps now you have:

<url-pattern>*</url-pattern>

change it to:

<url-pattern>/admin/*</url-pattern>
alexey28
  • 5,170
  • 1
  • 20
  • 25
  • My is /, which has been recommended on a couple of sites I've visited – Geoffrey De Vylder May 10 '12 at 13:09
  • @geoffreydv: yes you do. But than all requests will be processed with spring DispatcherServlet. So you don't have to surprise that you have two calls of DispatcherServlet. And, ok, you will define controller that process /home url and return "newurl". It still go to Dispatcher servlet. What next will you do? – alexey28 May 10 '12 at 13:30
0

The request mapping annotation defined over your methode restrict your controller to responds to requests starting with "/admin/home".

I would apply the following modifications :

@Controller
@RequestMapping("/admin")
public class AdminController {

   @RequestMapping(method = RequestMethod.GET)
   public String home() {
      return "home";
   }
}
Daniel Lavoie
  • 1,852
  • 1
  • 16
  • 19
0

Try this:

@RequestMapping(value = "admin",
        method = {RequestMethod.GET, RequestMethod.POST })
CamelTM
  • 1,225
  • 12
  • 17