警告:在名为 DispatcherServlet 的调度器中未找到与 URI [ ] 相关的映射。

10 浏览
0 Comments

警告:在名为 DispatcherServlet 的调度器中未找到与 URI [ ] 相关的映射。

我有以下的web.xml文件:

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

dispatcher

org.springframework.web.servlet.DispatcherServlet

2

dispatcher

/

applicationContext.xml文件内容如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop"

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

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"

>

Dispatcher servlet文件内容如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop"

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

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

indexController

class="org.springframework.web.servlet.view.InternalResourceViewResolver"

p:prefix="/WEB-INF/jsp/"

p:suffix=".jsp" />

class="org.springframework.web.servlet.mvc.ParameterizableViewController"

p:viewName="index" />

控制器在com.mindedges.testapp包中,代码如下:

@Controller

@RequestMapping("/sample")

public class SampleController {

public String sample(Model model){

return "sample.jsp";

}

}

当我访问http://localhost:8084/TestApp/sample时,出现以下警告信息:

WARNING: No mapping found for HTTP request with URI [/TestApp/sample] in DispatcherServlet with name

并且在Spring启动时,我看不到Testapp/sample的处理器加载。

我猜测我的组件扫描没有起作用。为什么会这样?还有其他原因吗?

0
0 Comments

警告信息:在名为[ ]的DispatcherServlet中找不到与HTTP请求URI [ ]相对应的映射。

问题原因:请求映射的名称与尝试返回的JSP文件名称不一致。

解决方法:更改请求映射的名称,确保与尝试返回的JSP文件名称不同。

在进行Web开发时,我们经常会使用DispatcherServlet来处理HTTP请求,并将其映射到相应的处理程序方法。然而,有时候我们可能会遇到上述警告信息,提示在DispatcherServlet中找不到与HTTP请求URI相对应的映射。那么,为什么会出现这个问题呢?

问题的原因是请求映射的名称与尝试返回的JSP文件名称不一致。DispatcherServlet在处理HTTP请求时,会根据请求映射的名称来确定要调用的处理程序方法。然而,如果请求映射的名称与尝试返回的JSP文件名称相同,就会导致DispatcherServlet无法找到正确的映射,从而出现警告信息。

要解决这个问题,我们可以通过更改请求映射的名称,确保与尝试返回的JSP文件名称不同。以下是一个示例代码,展示如何更改请求映射的名称:

@Controller
@RequestMapping("/example")
public class ExampleController {
    @RequestMapping("/page")
    public String getPage() {
        return "examplePage"; // 返回的JSP文件名称为examplePage.jsp
    }
}

在上述示例中,我们将请求映射的名称设置为"/example/page",并将要返回的JSP文件命名为"examplePage.jsp"。通过这样的设置,我们确保了请求映射的名称与JSP文件名称不同,避免了警告信息的出现。

在实际开发中,我们还可以采用其他方法来解决这个问题,例如使用不同的请求映射路径或更改JSP文件的名称等。无论采用何种方法,关键是确保请求映射的名称与尝试返回的JSP文件名称不一致,这样就能避免DispatcherServlet找不到相应映射的问题。

当出现警告信息"WARNING: No mapping found for HTTP request with URI [ ] in DispatcherServlet with name"时,我们需要检查请求映射的名称和尝试返回的JSP文件名称是否一致。如果一致,需要更改请求映射的名称,确保与JSP文件名称不同,以解决该问题。通过这样的处理,我们能够正常地处理HTTP请求并返回相应的JSP页面。

0
0 Comments

问题的出现的原因是因为没有找到与URI [ ]相匹配的映射。这可能是因为配置文件中的视图解析器没有正确设置,或者没有正确扫描控制器。

解决方法是:

1. 确保视图解析器的配置正确。在配置文件中添加以下代码:


这会将视图名称与前缀(/WEB-INF/jsp/)和后缀(.jsp)结合起来,用于解析视图。

2. 确保sample.jsp文件位于/WEB-INF/jsp/sample.jsp路径下。

3. 在dispatcher配置文件中启用注解,包括以下代码:


4. 在dispatcher配置文件中添加组件扫描,以便将控制器注册到dispatcher servlet中。将以下代码放置在dispatcher-context.xml文件中:


5. 确保dispatcher-context.xml文件中没有空格,并且包含命名空间。可以参考提供的GitHub Gist中的配置示例。

6. 将sample.jsp文件移动到jsp文件夹下,并将代码更改为return "sample"。

7. 如果仍然无法解决问题,可以考虑加载dispatcher servlet时的顺序。通常应该先加载dispatcher servlet,然后才是其他组件。

以上是关于解决“WARNING: No mapping found for HTTP request with URI [ ] in DispatcherServlet with name”问题的方法。通过正确设置视图解析器、启用注解、添加组件扫描等步骤,可以解决此问题。同时,还需注意配置文件的正确位置和格式。

0