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


这是项目的文件结构:



为什么不应用样式?

最佳答案

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

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

10-05 20:47
查看更多