java - Spring MVC Controller 未应用

标签 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 上部署编译后的 war 时并尝试访问 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"

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

最佳答案

您缺少<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>

关于java - Spring MVC Controller 未应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27656307/

相关文章:

java - 第三个参数 Java8 reduce on Object 中的字符串属性

java - 在 Spring boot 中添加超过 65535 个条目 jar

spring-mvc - Swagger - 默认情况下,Springfox 总是会生成一些响应消息 (401,403...)。我怎样才能删除它们?

Java - 解决大于内存限制的问题

java - 用Java解析包含XML片段的文件

java - LWJGL 无法使用 VBO 创建立方体

java - 单元测试依赖于另一个服务类的Spring引导服务类

java - Spring MVC 角色和管理员权限

java - Sonar : Instance methods should not write to "static" fields

mysql - org.hibernate.MappingException :Could not determine type for