java - jsp 文件不应用链接的 css 文件的样式

标签 java html css jsp model-view-controller

我正在使用 Controller 来为我的 index.jsp 页面提供服务。

当我键入:http://localhost:8080/Driving-Instructor-Gary/HomeController?page=home

它使用这段代码很好地加载了 index.jsp 文件:

package uk.co.morleys;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HomeController
 */
@WebServlet("/HomeController")
public class HomeController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public HomeController() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = request.getParameter("page");
        String views = "/WEB-INF/views/";
        switch (page){
        case "home":
            RequestDispatcher rd = getServletContext().getRequestDispatcher(views + "index.jsp");
            rd.forward(request, response);
            break;
        }
    }
}

问题是 css 样式没有应用到网页。

这是在 index.jsp 文件中链接的 css 文件:

<head>
    <meta charset="utf-8">
    <title>Morley's Motoring Mentoring</title>
    <meta name="description" content="">
    <meta name="author" content="Chris Mepham">
    <meta name="keywords" content="">
    <link rel="stylesheet" href="../css/main.css" type="text/css">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="../css/bootstrap.min.css">

    <!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>

这是元素的文件结构:

enter image description here

为什么没有应用样式?

最佳答案

您的 css 样式位于 WEB-INF 文件夹中。部署应用程序后,用户/浏览器无法访问此文件夹。 尝试将文件夹(还有 img、js)拉到 WebContent 中。并相应地更改 jsp 文件中的路径。

此外,您应该使用标准的 jSTL 标签库。如果您的应用程序不会在根上下文中运行,请使用 c:url 获取正确的(部署)路径。

关于java - jsp 文件不应用链接的 css 文件的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24756191/

相关文章:

java - 如何使用java检查Webdriver中的链接是否启用?

java - 接口(interface)和实现的名称,哪个更好?

css - 在小型媒体查询菜单中未设置在中心它在中型或超小型中的工作

Java - 检查 JTextField 是否为空

java - 点运算符内的字符串

javascript - iframe url 在浏览器中打开

javascript - 是否有类似于 sessionStorage 但具有多维键的东西?

javascript - 如何使用 JavaScript 对从 JSON 数据生成的 HTML Div 使用搜索/过滤器?

jquery - Portlet 仅在 IE7 中正确关闭

html - Bootstrap 模式 : class ="modal fade" causing problems