找不到HTTP请求的映射(Spring MVC)

Jul*_*ius 8 spring spring-mvc

这可能是关于这个Spring MVC错误的第100个问题,但是我无法让它继续工作.

我正在尝试将一个简单的控制器方法映射到/ account,稍后我想添加/ account/{id},但我甚至无法使用/ account工作.

这是我的web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>My Spring MVC web application</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>
</web-app> 
Run Code Online (Sandbox Code Playgroud)

application-context.xml的内容:

<mvc:annotation-driven />
<context:component-scan base-package="org.example" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

AccountController.java:

@Controller
public class AccountController  {

    @RequestMapping(value="/account", method = RequestMethod.GET)
    public ModelAndView showAccount() throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("account");
        mav.addObject("someText", "Hello World!");
        return mav;
    }   
}
Run Code Online (Sandbox Code Playgroud)

的src/main/webapps /目录视图/ account.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<h1>${someText}</h1>
Run Code Online (Sandbox Code Playgroud)

当我在Tomcat中启动应用程序时,我看到日志中出现以下行:

[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/account], methods=[GET], params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.example.springmvc.controller.AccountController.showAccount() throws java.lang.Exception
Run Code Online (Sandbox Code Playgroud)

对我来说,这表明url localhost:8080/account已正确映射,至少应该提供一些输出.但是,当我访问localhost:8080 /帐户时,我收到404错误,日志显示:

No mapping found for HTTP request with URI [/views/account.jsp] in DispatcherServlet with name 'springDispatcherServlet'
No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'springDispatcherServlet'
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助.

Bij*_*men 4

您的 Spring 配置没有任何问题,看起来/account您的 Controller 正在正确处理 URI,并且它正在正确返回视图名称account,该名称正在由您的 InternalViewResolver 解析为/views/account.jsp> 的路径。

现在,由于某种原因,这个调度出了问题(因为/*Spring DispatcherServlet 的映射,假设 Spring 也可以处理这个 /views,这可能就是您看到这个特定错误的原因)。您可以执行此操作,而不是将视图放在/views文件夹中,而是将其移动到/WEB-INF/views文件夹并将视图解析器更改为:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

  • 我让它工作了!呼呼!最后。我将 web.xml 中的 &lt;url-pattern&gt; 从 /* 更改为 / 。我不太明白为什么它现在有效,至少我是快乐的程序员。 (4认同)