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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12351777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:32:17  来源:igfitidea点击:

No mapping found for HTTP request with URI [/myappname/] in DispatcherServlet with name 'appServlet'

javaspringspring-mvctiles

提问by woyaru

I have got error No mapping found for HTTP request with URI [/myappname/] in DispatcherServlet with name 'appServlet'when I was starting my project on JBoss. This is issue has occurred after resolving another issue described here: "No Session found for current thread" after changing access method to the session

No mapping found for HTTP request with URI [/myappname/] in DispatcherServlet with name 'appServlet'在 JBoss 上开始我的项目时遇到错误。这是在解决此处描述的另一个问题后发生的问题:将访问方法更改为会话后,“未找到当前线程的会话”

Before everything was working fine. I am using Apache Tiles 2. I am reading few similar questions but I can't find working solution.

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

This is my DispatcherServlet Contextfile without Hibernate configuration:

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

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

My web.xmlwhole file:

我的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>

And this is my IndexController:

这是我的IndexController

@Controller
public class IndexController {

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

What is wrong with my configuration at this moment?

目前我的配置有什么问题?

回答by shazinltc

I think your are missing <mvc:annotation-driven/>which is required to read the @Controllerand @RequestMappingannotations. You can read more on this hereand here.

我认为您缺少<mvc:annotation-driven/>阅读@Controller@RequestMapping注释所需的内容。您可以在此处此处阅读有关此内容的更多信息

回答by woyaru

The solutio is to keep in DispatcherServlet Contextfile both annotation-driventags like here:

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

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

I haven't found similar solution so I am gooing to leave it here.

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

回答by Kisanagaram

Have this in your spring servlet.xml file

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

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

and

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

and

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

In your controller class, use

在您的控制器类中,使用

 @Controller
 public class IndexController {..}

and it works!

它有效!

回答by UtResolver

I was facing the same exception and was trying all above solutions but nothing helped but I did get a pointer the issue was because the class was not generated in the target class folder and it was due to incorrect Maven Project Structure which was leading to no compilation of Java controller class.

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

Check if you have below structure and then if class is generated in target->classfolder then only try above suggestions.

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

Project
  src
    main
      java
      resources
    test
      java
      resources

回答by Viveksuriyan

Make sure that your .java file is under src/main/java instead src/main/resources

确保您的 .java 文件在 src/main/java 而不是 src/main/resources