spatcherServlet中找不到名称为URI的HTTP请求

spatcherServlet中找不到名称为URI的HTTP请求

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

问题描述

我已经检查了关于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>

myController:

myController:

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>

因此,当我启动该appication时,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批注,然后请尝试以下网址/<webappname>/my/hello.html.这是因为org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping/my 附加到myController类中的每个RequestMapping.

You could try and add an @Controller annotation on top of your myController Class andtry 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.

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

08-05 19:22