当前位置: 首页 > 面试题库 >

在名为[duplicate]的DispatcherServlet中找不到URI为…的HTTP请求的映射

涂羽
2023-03-14
问题内容

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

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:

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

myController:

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

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

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 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错误,服务器日志中显示:

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

我已经检查了数十篇类似的文章,但我只是找不到错误,不知道会是什么?


问题答案:

你可以尝试@ControllermyController类的顶部添加注释,然后尝试以下url /<webappname>/my/hello.html。这是因为org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping预规划/我在班级myController的每个RequestMapping。



 类似资料: