未找到带有 URI 的 HTTP 请求的映射.... 在 DispatcherServlet 中,名称为 [英] No mapping found for HTTP request with URI.... in DispatcherServlet with name

查看:42
本文介绍了未找到带有 URI 的 HTTP 请求的映射.... 在 DispatcherServlet 中,名称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了几乎所有关于 stackoverflow 的相关文章,但我无法解决我的问题.

I checked out nearly every relevant article on stackoverflow already, but I just cant fix my problem.

代码如下:web.xml:

Here is the code: web.xml:

   <display-name>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> 
    </listener>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>/</url-pattern>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

spring-servlet.xml:

spring-servlet.xml:

<context:component-scan base-package="com.mycompany.elso" />
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>   

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

我的控制器:

public class myController {
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message); 
    }
}

网页/index.jsp:

Web Pages/index.jsp:

<html>
<head>
    <title>Spring 3.0 MVC Series</title>
</head>
<body>
    <a href="hello.html">Say Hello</a>
</body>
</html>

网页/WEB-INF/jsp/hello.jsp:

Web Pages/WEB-INF/jsp/hello.jsp:

<html>
<head>
    <title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
    ${message}
</body>
</html>

因此,当我启动应用程序时,index.jsp 已正确加载,但是当我单击 href 导航到 hello.jsp 时,出现 404 错误并且服务器日志显示:

So when i launch the appication the index.jsp is loaded correctly but when i click on the href to navigate to hello.jsp i got a 404 error and the server log says:

No mapping found for HTTP request with URI [/Elso/hello.html] in DispatcherServlet with name 'spring'

我已经查了几十篇这样的文章,但我就是找不到错误,有人知道这是什么吗?

I've checked out dozens of articles like that, but I just can't find the mistake, anybody has any idea what could it be?

推荐答案

你可以尝试在你的 myController 类之上添加一个 @Controller 注释和尝试以下 url //my/hello.html.这是因为 org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping 在 myController 类中的每个 RequestMapping 前面加上了 /my.

You could try and add an @Controller annotation on top of your myController Class and try the following url /<webappname>/my/hello.html. This is because org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping prepends /my to each RequestMapping in the myController class.

这篇关于未找到带有 URI 的 HTTP 请求的映射.... 在 DispatcherServlet 中,名称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆