Java Spring MVC控制器';这不适用
我正在尝试编写一个简单的Java Spring MVC控制器';这不适用,java,spring,spring-mvc,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.springfra
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>
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";
}
}
但是当我在jboss7.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"
如您所见,没有发生任何错误。您缺少
注释
<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版本中看到您正在使用SpringMVC2.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>
尝试将您的
请求映射更改为:
@请求映射(“/mywp/hellact”)
致:
@请求映射(“/hellact”)
您不需要在RequestMapping
值中提及您的WebApp的名称
<?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>