我正在使用 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>
这是元素的文件结构:
为什么没有应用样式?
最佳答案
您的 css 样式位于 WEB-INF 文件夹中。部署应用程序后,用户/浏览器无法访问此文件夹。 尝试将文件夹(还有 img、js)拉到 WebContent 中。并相应地更改 jsp 文件中的路径。
此外,您应该使用标准的 jSTL 标签库。如果您的应用程序不会在根上下文中运行,请使用 c:url 获取正确的(部署)路径。
关于java - jsp 文件不应用链接的 css 文件的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24756191/