Netbeans WEB应用程序项目无法读取CSS文件的相对路径 [英] Netbeans WEB application project not able to read the relative path for CSS file

查看:45
本文介绍了Netbeans WEB应用程序项目无法读取CSS文件的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一本名为"Murach的Java Servlet和JSP"的书中尝试示例代码.出于某种原因,我无法使CSS正常工作. Netbeans 似乎没有使用相对href 路径找到 main.css 文件.

index.html

 < html>< head>< title> Murcah的Java Servlet和JSP</title>< meta charset ="UTF-8">< meta name ="viewport" content ="width = device-width,initial-scale = 1.0">< link rel ="stylesheet" href ="resources/css/main.css" type ="text/css"/></head><身体>< h1>加入我们的电子邮件列表</h1>< p>要加入我们的电子邮件列表,请在下面输入您的姓名和电子邮件地址.< form action ="emailList" method ="post"><输入type ="hidden" name ="action" value ="add"/>< label>电子邮件:</label><输入type ="email" name ="email"必填/>< br/>< label>名:</label><输入type ="text" name ="firstName"是必需的/>< br/><标签>姓氏:</label><输入type ="text" name ="lastName"必填/>< br/>< label>& nbsp;</label>< input type ="submit" value =立即加入" id ="submit"/></form></body></html> 

main.css

  body {字体家族:Arial,Helvetica,sans-serif;字体大小:11pt;左边距:2em;右边距:2em;背景颜色:深红色;背景:黄色;}h1 {颜色:青绿色;}标签{向左飘浮;宽度:6em;底边距:0.5em;}输入[type ="text"],输入[type ="email"] {宽度:15em;左边距:0.5em;底边距:0.5em;}br {清除:两者;}#提交{左边距:0.5em;} 

context.xml

 <?xml version ="1.0" encoding ="UTF-8"?><上下文路径="/ch02email"/> 

这是将资源组织到子文件夹中的方式.

main.css 路径,我相信是 ch02email/resources/css/main.css ,但是由于我将contextPath设置为

 <?xml version ="1.0" encoding ="UTF-8"?><上下文路径="/ch02email"/> 

index.html 上,您可以看到我的 href < link rel ="stylesheet" href ="resources/css/main.css"type ="text/css"/>

由于某些原因,css不适用.另外,我尝试查看源代码来打开css,但是得到了ff.

查看源代码

查看源代码,我收到一条 404 消息.

(查看源代码)

我在 Firefox Google Chrome 上都尝试过,但是得到了相同的结果.找不到CSS 在此处输入代码文件.

我在这里想念什么?我什至尝试放入 WEB-INF/resources/css/main.css ,我认为这是不正确的,因为我理解在引用时不包含 WEB-INF

最后,我尝试使用< base href ="$ {pageContext.request.contextPath}/"/> 失败.

请帮助.

谢谢.

解决方案

我刚刚发现它无法访问main.css文件的原因.我找到了

resources 文件夹现在包含在 Web Pages 文件夹中.

我知道这是一个简单的问题,但是我不知道的非常简单的细节引起了头痛.

我希望这对其他会找到此帖子的人有所帮助.:)

I'm trying out example codes from a book called "Murach's Java Servlet and JSP" For some reason, I can't get the CSS to work. Netbeans doesn't seem to find the main.css file using a relative href path.

index.html

<html>
    <head>
        <title>Murcah's Java Servlet and JSP</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="resources/css/main.css" type="text/css" />
    </head>
    <body>
        <h1>Join our email list</h1>
        <p>To join our email list, enter your name and email address below.</p>

        <form action="emailList" method="post">

            <input type="hidden" name="action" value="add" />

            <label>Email: </label>
            <input type="email" name="email" required /> <br />
            <label>First Name:</label>
            <input type="text" name="firstName" required /> <br/>
            <label>Last Name:</label>
            <input type="text" name="lastName" required /> <br />

            <label>&nbsp;</label>
            <input type="submit" value="Join Now" id="submit" />

        </form>
    </body>
</html>

main.css

body{
    font-family: Arial,Helvetica,sans-serif;
    font-size: 11pt;
    margin-left: 2em;
    margin-right: 2em;
    background-color: darkred;
    background: yellow;
}

h1{
    color:teal;
}

label{
    float:left;
    width:6em;
    margin-bottom: 0.5em;
}

input[type="text"], input[type="email"]{
    width: 15em;
    margin-left: 0.5em;
    margin-bottom: 0.5em;
}

br{
    clear:both;
}

#submit{
    margin-left: 0.5em;
}

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/ch02email"/>

Here's how the resources are organized into sub-folders.

And main.css path I believe to be ch02email/resources/css/main.css But since I have set the contextPath to

<?xml version="1.0" encoding="UTF-8"?>
    <Context path="/ch02email"/>

on index.html you can see that my href is <link rel="stylesheet" href="resources/css/main.css" type="text/css" />

For some reason the css doesn't apply. Also, I tried to view the source to open the css but I get the ff.

view source

view sourceI get a 404 message.

(view source)

I tried both on Firefox and Google Chrome but I get the same result. CSSenter code here file can't be found.

What am I missing here? I even tried to put WEB-INF/resources/css/main.css which I don't think to be right as I understand not to include WEB-INF when referencing.

Lastly, I tried to use <base href="${pageContext.request.contextPath}/"/> with no success.

Please help.

Thank you.

解决方案

I just found out the reason why it's not able to access the main.css file. I found the information here. I found that files contained in WEB-INF are not accessible publicly. So what I did was move the resources folder like what's on the screenshot.

resources folder is now contained in Web Pages folder.

I know this is a simple question but caused headache for that very simple detail I didn't know.

I hope this helps others who will find this post. :)

这篇关于Netbeans WEB应用程序项目无法读取CSS文件的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆