java org.springframework.web.servlet.PageNotFound - 没有找到带有 URI 的 HTTP 请求的映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15070857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:23:22  来源:igfitidea点击:

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI

javacssspringspring-mvc

提问by Freakyuser

I referred to many posts but still I was unable to solve this problem.

我参考了很多帖子,但仍然无法解决这个问题。

enter image description here

在此处输入图片说明

I have tried putting this css folder in WEB-INF and within views folder as well.
But still the styles are not reflected in the view page.

我曾尝试将此 css 文件夹放在 WEB-INF 和 views 文件夹中。
但是样式仍然没有反映在视图页面中。

Here is my servlet-context.xml

这是我的 servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <resources mapping="/resources/**" location="/resources/" />

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="ctc.event.control" />

</beans:beans>

This is my web.xml

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

This is my style sheet ExpenseDetailsStyles.css

这是我的样式表 ExpenseDetailsS​​tyles.css

#ItemDiv1_Table1 {
    border: 1px solid black;
}

#ItemDiv1Table2 {
    width: 20%;
    float: right;
    font-size:30px;
}

and my view page.

和我的查看页面。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ include file="TagIncludes.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Expense Details</title>
<link href='<c:url value="/resources/css/ExpenseDetailsStyles.css" />' rel="stylesheet" type="text/css" media="screen" />
<script type='text/javascript' src="../js/jquery-1.9.0.js"></script>
<script type="text/javascript"></script>
</head>
<body>
    <form:form name="ExpenseDetails" method="Post"
        action="ExpenseDetailsForm.html">
        <div id="ItemDiv1">
            <table>
                <tr>
                    <td>
                        <table id="ItemDiv1_Table1">
                            <tr>
                                <td>Item</td>
                                <td><input type="text" name="Item1" id="Item1" required /></td>
                            </tr>
                            <tr>
                                <td>Expense in Rs</td>
                                <td><input type="text" name="ItemValue1" id="ItemValue1" ></td>
                            </tr>
                        </table>
                    </td>
                    <td>
                        <table id="ItemDiv1_Table2">
                            <tr>
                                <td>Edit</td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
    </form:form>
</body>
</html>

Here is the controller

这是控制器

@Controller
public class ExpenseFormController {

    @RequestMapping(value = "/ExpenseDetailsForm.html", method = RequestMethod.GET)
    public String openForm() {
        return "ExpenseDetailsForm";

    }

    @RequestMapping(value = "/ExpenseDetailsForm.html", method = RequestMethod.POST)
    public String submitForm() {
        return null;

    }
}

stacktrace is not an error but a warning.

stacktrace 不是错误而是警告。

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/Calculator/<c:url value="css/ExpenseDetailsStyles.css" />] in DispatcherServlet with name 'appServlet'

警告:org.springframework.web.servlet.PageNotFound -<c:url value="css/ExpenseDetailsStyles.css" />在名为“appServlet”的 DispatcherServlet 中找不到带有 URI [/Calculator/ ] 的HTTP 请求的映射

I think there is some problem with my uri in the view page and I tried changing it in many different ways. Can anyone please help me correct the uri and reflect the styles on the view page?
Please comment if any other information pertaining this, is needed.

我认为视图页面中的 uri 存在一些问题,我尝试以多种不同方式对其进行更改。任何人都可以帮我更正 uri 并在视图页面上反映样式吗?
如果需要与此相关的任何其他信息,请发表评论。

回答by Sotirios Delimanolis

You have the following

你有以下

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

mapping some resources to the Spring servlet but you don't have any controller handler methods to serve them up. You should remove the above servlet-mappings and put your cssresources in /resources/cssand jsresources in /resources/js. Spring will know to get them automatically from there because of the <mvc:resources>element.

将一些资源映射到 Spring servlet,但您没有任何控制器处理程序方法来为它们提供服务。您应该删除上述servlet-mappings 并将您的css资源/resources/cssjs资源放入/resources/js. 由于该<mvc:resources>元素,Spring 将知道从那里自动获取它们。

Make sure to also change the hard-coded location of your scripts and css in html files.

确保还更改了 html 文件中脚本和 css 的硬编码位置。

Also, on this line

另外,在这条线上

<link href='<c:url value="/resources/css/ExpenseDetailsStyles.css" />' ... />

use double quotes ". You might also want to refactor to this

使用双引号"。您可能还想重构为此

The <c:url>tag will put the value into the variable myvarwhich will then be available in your <link>tag.

<c:url>标签会将值放入变量中myvar,然后该变量将在您的<link>标签中可用。

Also you need this line at the top of your jsp to tell it you need jstl tags:

您还需要在 jsp 顶部使用这一行来告诉它您需要 jstl 标签:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

回答by CodeChimp

In you context file, add the following:

在您的上下文文件中,添加以下内容:

<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

This adds a static path, /resources/. Simply place your content there.

这会添加一个静态路径 /resources/。只需将您的内容放在那里。