简体   繁体   English

JSP文件未应用链接的CSS文件的样式

[英]jsp file not applying styles of linked css files

I am using a controller to serve my index.jsp page. 我正在使用控制器来服务我的index.jsp页面。

When I type: http://localhost:8080/Driving-Instructor-Gary/HomeController?page=home 当我输入时: http://localhost:8080/Driving-Instructor-Gary/HomeController?page=home

It loads the index.jsp file fine using this code: 使用以下代码可以很好地加载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;
        }
    }
}

The problem is that the css styling is not being applied to the webpage. 问题在于,css样式未应用于网页。

Here is the css file linking in index.jsp document: 这是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>

Here is the file structure of the project: 这是项目的文件结构:

在此处输入图片说明

Why is the styling not being applied? 为什么不应用样式?

Your css styles are located within the WEB-INF folder. 您的CSS样式位于WEB-INF文件夹中。 This folder is not accessible for the user / browser when the app has been deployed. 部署应用程序后,用户/浏览器无法访问此文件夹。 Try to pull the folder(s) (also img, js) up into WebContent. 尝试将文件夹(也包括img,js)拉入WebContent。 And change the path in your jsp file accordingly. 并相应地更改jsp文件中的路径。

Furthermore you should use standard jstl taglib. 此外,您应该使用标准的jstl taglib。 Use c:url to get the right (deployment) path if your application will not run in root context. 如果您的应用程序无法在根上下文中运行,请使用c:url获得正确的(部署)路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM