【问题标题】:jsp file not applying styles of linked css filesjsp文件不应用链接css文件的样式
【发布时间】:2014-09-05 12:43:02
【问题描述】:

我正在使用控制器来提供我的 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>

这是项目的文件结构:

为什么没有应用样式?

【问题讨论】:

    标签: java html css jsp model-view-controller


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-02-04
      • 2014-12-31
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多