2

多くの投稿を参照しましたが、それでもこの問題を解決できませんでした。

ここに画像の説明を入力してください

この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>

これは私の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>

これは私のスタイルシートExpenseDetailsS​​tyles.cssです

#ItemDiv1_Table1 {
    border: 1px solid black;
}

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

と私のビューページ。

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

これがコントローラーです

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

    }
}

スタックトレースはエラーではなく警告です。

警告:org.springframework.web.servlet.PageNotFound- <c:url value="css/ExpenseDetailsStyles.css" />「appServlet」という名前のDispatcherServletにURI [/Calculator/]を使用したHTTPリクエストのマッピングが見つかりません

ビューページのURIに問題があると思い、さまざまな方法で変更してみました。誰かが私がURIを修正し、ビューページにスタイルを反映するのを手伝ってくれませんか?
これに関する他の情報が必要な場合はコメントしてください。

4

2 に答える 2

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>

一部のリソースをSpringサーブレットにマッピングしますが、それらを提供するためのコントローラーハンドラーメソッドがありません。上記を削除し、リソースをに、リソースをにservlet-mapping配置する必要があります。Springは、要素があるため、そこから自動的にそれらを取得することを認識します。css/resources/cssjs/resources/js<mvc:resources>

htmlファイル内のスクリプトとcssのハードコードされた場所も必ず変更してください。

また、この行で

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

二重引用符を使用します"。これにリファクタリングすることもできます

<c:url>タグは値を変数に入れ、タグmyvarで使用できるようになります<link>

また、jstlタグが必要であることを示すために、jspの上部に次の行が必要です。

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
于 2013-02-25T16:53:34.750 に答える
0

コンテキストファイルに、以下を追加します。

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

これにより、静的パス/resources/が追加されます。そこにコンテンツを配置するだけです。

于 2013-02-25T16:03:32.007 に答える