Reputation: 7357
I'm trying to write a simple SpringMVC
example. I have:
appContext
:
<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.mycompany.app"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
</beans>
web.xml
:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/mywp/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
FirstController.java
:
package com.mycompany.app;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class FirstController
{
@RequestMapping("/mywp/hellact")
public static String firstAction()
{
System.out.println("This is the first controller");
return "Hello action";
}
}
But when I deploy the compiled war on the JBoss 7.0.2
and try to get access to localhost:8080/mywp/hellact
I get a 404
error. There are no any exceptions thrown into the server logs. What's wrong?
The server log:
15:03:15,333 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-4) Loading XML bean definitions from ServletContext resource [/WEB-INF/appContext.xml]
15:03:15,543 INFO [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-4) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
15:03:15,653 INFO [org.springframework.web.context.ContextLoader] (MSC service thread 1-4) Root WebApplicationContext: initialization completed in 390 ms
15:03:15,703 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/mywp]] (MSC service thread 1-4) Initializing Spring FrameworkServlet 'dispatcher'
15:03:15,703 INFO [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-4) FrameworkServlet 'dispatcher': initialization started
15:03:15,703 INFO [org.springframework.web.context.support.XmlWebApplicationContext] (MSC service thread 1-4) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Dec 26 15:03
14]; parent: Root WebApplicationContext
15:03:15,723 INFO [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-4) FrameworkServlet 'dispatcher': initialization completed in 20 ms
15:03:15,723 INFO [org.jboss.web] (MSC service thread 1-4) registering web context: /mywp
15:03:15,753 INFO [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) Deployed "mywp.war"
As you can see, there is no any error was occured.
Upvotes: 2
Views: 141
Reputation: 1424
Try to change your RequestMapping
from :
@RequestMapping("/mywp/hellact")
To :
@RequestMapping("/hellact")
You don't need to mention the Name of your WebApp in the RequestMapping
Value.
Upvotes: 1
Reputation: 28519
You're missing the <annotation-driven />
annotation
<context:component-scan base-package="com.mycompany.app"/>
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
Though, I see by your xsd version that you're using spring-mvc 2.5, if so, the following config should work for you
<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="com.mycompany.app.controllers.FirstController "/>
</beans>
Upvotes: 2