0

我正在尝试在 spring mvc 环境下运行的 jsp 页面中应用 css 规则。webapp/css/mystyles.css:

.center{
    margin: auto; text-align: center;
}

h1{
    color: red;
}

webapp/WEB-INF/jsp/Layout.jsp:

<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title><tiles:insertAttribute name="title" ignore="true"/></title>
    <link rel="stylesheet" type="text/css" href="../../css/mystyles.css" media="screen" />
</head>
<body>
    <table border="1" cellspacing="2" align="center">
        <tr>
            <td height="30" colspan="2">
                <tiles:insertAttribute name="header"/>
            </td>
        </tr>
        <tr>
            <td height="250" width="150">
                <tiles:insertAttribute name="menu"/>
            </td>
            <td width="400">
                <tiles:insertAttribute name="body"/>
            </td>
        </tr>
        <tr>
            <td height="30" colspan="2">
                <tiles:insertAttribute name="footer"/>
            </td>
        </tr>
    </table>
</body>
</html>

这是我使用 firebug 获得的网页代码的一部分:

<head>
<title>Being Java Guys | Tiles Integration</title>
<link media="screen" href="../../css/mystyles.css" type="text/css" rel="stylesheet">
<html><head><title>Apache Tomcat/7.0.35 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The requested resource is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.35</h3></body></html>
</link>
</head>

<div class="center">
<h1>Header</h1>
</div>

不幸的是我没有得到一个红色的标题。

4

3 回答 3

0

您正在弄乱服务器端文件位置和 URL。

在服务器端/WEB-INF/pages/../../css/mycss.css将是您的 css,但不是当它是由浏览器呈现的页面时。因为在浏览器上,您的 JSP URL 是另一回事。

您应该有一个<base>标签<head>来指定相对 URL 目标,然后指定相对于该目标的 URL。

于 2013-08-03T00:13:04.167 回答
0

You have to change something so the css file gets loaded.

Tomcat can't find it at "../../css/mystyles.css" (which you show as being http://localhost/css/mystyles.css" in firebug's net tab) so the URL in the tag is either wrong or Tomcat is set up somehow wrong.

Hint: You can enter the URL for a css file in the browser address bar to try various things more easily as you change the Tomcat config or load files.

于 2013-08-02T22:30:56.930 回答
0

请在 css 文件路径之前添加应用程序的上下文路径。

<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:set var="contextPath" scope="request" value="${pageContext.request.contextPath}" />

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title><tiles:insertAttribute name="title" ignore="true"/></title>
    <link rel="stylesheet" type="text/css" href="${contextPath}/css/mystyles.css" media="screen" />
</head>
<body>
    <table border="1" cellspacing="2" align="center">
    <tr>
        <td height="30" colspan="2">
        <tiles:insertAttribute name="header"/>
        </td>
    </tr>
    <tr>
        <td height="250" width="150">
        <tiles:insertAttribute name="menu"/>
        </td>
        <td width="400">
        <tiles:insertAttribute name="body"/>
        </td>
    </tr>
    <tr>
        <td height="30" colspan="2">
        <tiles:insertAttribute name="footer"/>
        </td>
    </tr>
    </table>
</body>
</html>
于 2013-08-03T13:11:28.377 回答