当前位置: 首页 > 知识库问答 >
问题:

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

龙哲
2023-03-14

我已经查阅了几乎所有关于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); 
    }
}

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:

<html>
<head>
    <title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
    ${message}
</body>
</html>
No mapping found for HTTP request with URI [/Elso/hello.html] in DispatcherServlet with name 'spring'

共有1个答案

何辰沛
2023-03-14

您可以尝试在myController类的顶部添加@Controller注释,并尝试以下url/ /my/hello.html 。这是因为org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping将/my前置到myController类中的每个RequestMapping。

 类似资料: