I have a similar problem with 404 HTTP status in Spring 3.1 MVC application
But I did not make the same mistakes as this poster, but I still get 404.
Web.xml file:
<?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_2_5.xsd" version="2.5"> <display-name>Test</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
I can load index.jsp, but not the actual controller.
file dispatcher-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scan base-package="com.test" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
controller
package com.test.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.ui.Model; @Controller public class BenchController { @RequestMapping(value = "/bench", method = RequestMethod.GET) @ResponseBody public String welcome(Model model) { return "yo"; }
}
I can go to localhost: 8080 / Test / index.jsp, but localhost: 8080 / Test / bench returns 404.
I use eclipse if this helps.
EDIT 1:
This is logging.properties. I can not find any of the magazines except the magazine.
handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
Interestingly, I'm not sure where the catalina.base variable is defined.
EDIT 2:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>Test</artifactId> <version>0.1</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.1.0.RELEASE</spring.version> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webXml>/WebContent/WEB-INF/web.xml</webXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> </dependencies> </project>
I do not see log4j in the dependency tree, even if it is in my pom file.
EDIT 3:
This is how I created the project in eclipse:
- New Maven project (m2 eclipse plugin)
- I chose Simple Project
- The completed group id / artificat and the selected war as my packaging.
- I right-clicked on my project in the project folder window and selected Properties
- I clicked on the project face and converted it when prompted
- I selected Dynamic Web Module and clicked ok
As soon as I did all this, I started adding a dispatcher, updated pom using spring, etc.
EDIT 4:
Okay, I'm finally leaving. After commenting out these lines in web.xml:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
I was able to directly deploy the application through tomcat manager. Now i get
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
It seems to be trying to find dispatcher-servlet.xml in the / WEB -INF folder. Having looked at the war file, I cannot find the file at all. Every time I put the servlet xml file in this folder, it seems to be deleted. I can put it in any other folder, but for some reason, when I do this, it still searches for the file under / WEB -INF even after updating web.xml to link to the new location.
EDIT 5:
Here is the update of my 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_2_5.xsd " version = "2.5">
<display-name>Test</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/applicationContext.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>WEB-INF/classes/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> </web-app>
After explicitly adding the location of applicationContext and the servlet manager. catalina.out says:
Mapped URL path [/bench] onto handler 'benchController' Mapped URL path [/bench.*] onto handler 'benchController' Mapped URL path [/bench/] onto handler 'benchController' Mapped URL path [/bench] onto handler 'benchController' Mapped URL path [/bench
But when switching to localhost: 8080 / Test / bench - it still returns 404. I cannot use index.jsp when deploying via tomcat manager, but I can if I deploy through eclipse.
Log:
127.0.0.1 - - [20/Feb/2012:14:41:13 -0800] "GET /Test/ HTTP/1.1" 200 182 127.0.0.1 - - [20/Feb/2012:14:41:19 -0800] "GET /Test/ HTTP/1.1" 200 182 127.0.0.1 - - [20/Feb/2012:14:41:24 -0800] "GET /Test/bench HTTP/1.1" 404 1012
DECISION:
I followed the instructions below and inside the project folder, from the command line I launched mvn eclipse: eclipse -Dwtpversion = 2.0 so that eclipse can correctly import the project. I am working on figuring out how to do this through eclipse to avoid having to manually do this every time.