Spring MVC控制器未应用

时间:2014-12-26 11:10:18

标签: java spring spring-mvc

我试图写一个简单的SpringMVC示例。我有:

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

但是当我在JBoss 7.0.2上部署已编译的战争并尝试访问localhost:8080/mywp/hellact时,我收到404错误。服务器日志中没有任何异常。怎么了?

服务器日志:

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"

如您所见,没有发生任何错误。

2 个答案:

答案 0 :(得分:2)

您错过了<annotation-driven />注释

    <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"/>

尽管如此,我在你的xsd版本中看到你正在使用spring-mvc 2.5,如果是这样,以下配置应该适合你

<?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>

答案 1 :(得分:1)

尝试更改您的RequestMapping

  

@RequestMapping( “/ mywp / hellact”)

致:

  

@RequestMapping( “/ hellact”)

您无需在RequestMapping值中提及您的WebApp名称。

相关问题