【问题标题】:Build File Path in Java Web Project在 Java Web 项目中构建文件路径
【发布时间】:2013-03-02 22:37:11
【问题描述】:

我正在使用以下代码将 css 文件动态添加到我的 Web 项目中。但是,当我写出 css 文件的绝对路径时,它会返回我的 Web 项目之外的内容。例如,为了测试,我将 css/style.css 添加到 cssAssets ArrayList 中。当我检查f.isFile() 的条件时,它会写出home/*username*/css/styles.css 而不是从index.jsp 文件到css 目录的相对路径。它应该去workspace/projectName/WebContent/css/styles.css。任何帮助表示赞赏。

public String buildHead()
{
    htmlHead = new StringBuilder();
    htmlHead.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
    htmlHead.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
    htmlHead.append("<meta http-equiv=\"cache-control\" content=\"max-age=0\" />");
    htmlHead.append("<meta http-equiv=\"cache-control\" content=\"no-cache\" />");
    htmlHead.append("<meta http-equiv=\"expires\" content=\"Tue, 01 Jan 1980 1:00:00 GMT\" />");
    htmlHead.append("<meta http-equiv=\"pragma\" content=\"no-cache\" />");
    if (this.title.length() > 0)
        htmlHead.append("<title>" + this.title + "</title>");
    else
        htmlHead.append("<title>MMJ Ceo </title>");

    for (String css : this.cssAssets)
    {
        File f = new File(css);

        if (f.isFile())
        {
            htmlHead.append("<link rel='stylesheet' href=\"" + css + "\"/>");
        }
        else
            return f.getAbsolutePath();
    }
    for(String js : this.jsAssets)
    {
        File f = new File(js);
        if(f.isFile())
            htmlHead.append("<script type='text/javascript' src='" + js + "'></script>");
    }
    htmlHead.append("</head>");
    return htmlHead.toString();
}

更新

这段代码sn-p来自我位于projectName.src.com.projectName.view的Page类,css文件位于projectName/WebContent/css/style.css,它是从projectName/WebContent调用的/index.jsp

【问题讨论】:

    标签: java css jsp


    【解决方案1】:

    问:您使用的是什么 IDE?这听起来像 Eclipse,在这种情况下,“是的:您应该将 css/ 目录放在“WebContent/”下(就像您的“index.jsp”也在“WebContent/”下一样)。

    ... 但是 ....

    正在运行的 servlet 的根路径不同于您的 Web 服务器上的实际物理路径。看这里:

    【讨论】:

    • 我正在使用 Eclipse。感谢您为我指明正确的方向。我将根据您的建议进行快速测试。
    猜你喜欢
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多