4gus71n
4gus71n

Reputation: 4093

Spring PageNotFound and URLMapping in web.xml

I have been reading a lot of questions like this, but I'm not getting out of the trouble, and I'm starting to think that the problem is tomcat playing around. First of all, here are my web.xml, my *-servlet.xml and my controller.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name>Spring REST Server</display-name>
<description>Spring REST Server</description>
<context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:env/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
            classpath:config/applicationContext.xml
            classpath:config/kimboo-servlet.xml
</param-value>
</context-param>
<context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>Kimboo</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring MVC Dispatcher Servlet -->
<servlet>
  <servlet-name>kimboo</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:config/applicationContext.xml
      classpath:config/kimboo-servlet.xml
    </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>kimboo</servlet-name>
  <url-pattern>/home/</url-pattern>
</servlet-mapping>
</web-app>

This is my kimboo-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="ar.com.kimboo.server.ui.controller" />

<mvc:annotation-driven />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/AppClient/pages/" />
    <property name="suffix" value=".html" />
</bean>

And this is my controller

@Controller
@RequestMapping(value = "/home")
public class HomeController {

@RequestMapping(value = "/")
public String home() {
    return "login";
}

@RequestMapping(value = "/main")
public String main() {
    return "main";
}

    @RequestMapping(value = "/about")
public String main() {
    return "about";
}
}

This is a pretty simple example, I don't know why isn't working. I have readed a lot of questions like this, here and in another sites, I don't know what I'm missing.

By the way, in the controller I tried all the convinations to match the url; put "/home/" or "home/" at the class level, and put "login" or "main" at method level.

The only thing that works is when I hit localhost:8080/myServer/home/. I tried using "/home/" in the url-pattern of the web.xml instead "/home/". I also tried use "/" and "/", but is the same.

The only way that this works is when in the url-pattern of the web.xml I use

<url-pattern>/home/</url-pattern>
<url-pattern>/home/main/</url-pattern>
<url-pattern>/home/login/</url-pattern>

Then I can hit all the url's. I don't know what to do, maybe this is a tomcat 7 problem?

Upvotes: 0

Views: 717

Answers (3)

timsat
timsat

Reputation: 1

Look at this question: Basic Spring MVC config: PageNotFound using InternalResourceViewResolver. The reason is your mapping which is too wide and interfere to JspServlet mapping.

Upvotes: 0

Harish
Harish

Reputation: 36

you were not configured mapping for controller and annotation handler.Add this below bean configuration in the kimboo-servlet.xml and also add the respective jars.It may work.

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

<bean id="simpleUrlMapping"  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
         <value>
            /home/**=homeController 
        </value>
    </property>
</bean>

Upvotes: 1

ArunM
ArunM

Reputation: 2314

Rather than this being a TomCat problem I suspect this is a problem with how you have annotated the controller. Please replace your controller annotations as follows

  @Controller
public class HomeController {

@RequestMapping(value = "/home")
public String home() {
    return "login";
}

@RequestMapping(value = "/home/main")
public String main() {
    return "main";
}

    @RequestMapping(value = "home/about")
public String main() {
    return "about";
}

}

This is only a guess. Sorry if I got it wrong. I remember seeing this problem in REST services. Maybe its the same.Dont have time to test it myself now.

Upvotes: 0

Related Questions