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

Fre*_*ser 2 css java spring spring-mvc

我提到很多帖子,但我仍然无法解决这个问题.

在此输入图像描述

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

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

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

#ItemDiv1_Table1 {
    border: 1px solid black;
}

#ItemDiv1Table2 {
    width: 20%;
    float: right;
    font-size:30px;
}
Run Code Online (Sandbox Code Playgroud)

和我的视图页面.

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

这是控制器

@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;

    }
}
Run Code Online (Sandbox Code Playgroud)

stacktrace不是错误,而是警告.

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

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

Sot*_*lis 6

你有以下几点

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

此外,在这条线上

<link href='<c:url value="/resources/css/ExpenseDetailsStyles.css" />' ... />
Run Code Online (Sandbox Code Playgroud)

使用双引号".您可能还想对此进行重构

<c:url>标签将会把值放入变量myvar,然后这将在您的<link>标签.

你还需要在jsp顶部的这一行告诉你需要jstl标签:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
Run Code Online (Sandbox Code Playgroud)