一尘不染

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

spring-mvc

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

这是代码: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); 
    }
}

网页/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:

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

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


阅读 409

收藏
2020-06-01

共1个答案

一尘不染

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

2020-06-01