在DispatcherServlet中找不到带有URI [/ myappname /]的HTTP请求的映射,名称为“appServlet”

时间:2012-09-10 12:30:11

标签: java spring spring-mvc tiles

当我在JBoss上启动项目时,我遇到了错误No mapping found for HTTP request with URI [/myappname/] in DispatcherServlet with name 'appServlet'。在解决此处描述的另一个问题后出现此问题:"No Session found for current thread" after changing access method to the session

在一切正常之前。我正在使用Apache Tiles 2.我正在阅读几个类似的问题,但我找不到可行的解决方案。

这是我没有Hibernate配置的DispatcherServlet Context文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<tx:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="finances.webapp" />

<!-- Handles HTTP GET requests for resources by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/tiles-definitions.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</beans:bean>

我的web.xml整个文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml
    </param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

这是我的IndexController

@Controller
public class IndexController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model, Principal principal) {
        return "index";
    }
}

此时我的配置有什么问题?

5 个答案:

答案 0 :(得分:10)

我认为您缺少阅读<mvc:annotation-driven/>@Controller注释所需的@RequestMapping。您可以详细了解herehere

答案 1 :(得分:0)

解决方案是在DispatcherServlet Context文件中保留两个annotation-driven标记,如下所示:

<annotation-driven />
<tx:annotation-driven />

我没有找到类似的解决方案,所以我将把它留在这里。

答案 2 :(得分:0)

在你的spring servlet.xml文件中有这个

xmlns:mvc="http://www.springframework.org/schema/mvc
xmlns:tx="http://www.springframework.org/schema/tx"

http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

<mvc:annotation-driven />
<tx:annotation-driven /> 

在您的控制器类中,使用

 @Controller
 public class IndexController {..}

它有效!

答案 3 :(得分:0)

我遇到了同样的例外,正在尝试以上所有解决方案,但没有任何帮助 但我确实得到了一个指针,问题是因为该类没有在目标类文件夹中生成,而是由于Maven项目结构不正确导致无法编译Java控制器类。

检查您是否具有以下结构,然后如果在target->class文件夹中生成了类,则只尝试以上建议。

Project
  src
    main
      java
      resources
    test
      java
      resources

答案 4 :(得分:0)

确保您的.java文件位于src / main / java而不是src / main / resources下