我想从 home.jsp 链接到 home.css。
home.jsp 头部:
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Infinance Home Webpage">
<meta name="author" content="Infinance">
<link rel="icon" href="img/infinance-web-icon_128.png">
<title>Infinance: Inicio</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/home.css" />
<style id="style-1-cropbar-clipper">
.en-markup-crop-options {
top: 18px !important;
left: 50% !important;
margin-left: -100px !important;
width: 200px !important;
border: 2px rgba(255,255,255,.38) solid !important;
border-radius: 4px !important;
}
.en-markup-crop-options div div:first-of-type {
margin-left: 0px !important;
}
</style></head>
...
我尝试了很多在其他 StackOverFlow 帖子中找到的解决方案,但唯一有效的解决方案是:
<style type="text/css">
<%@include file="css/style.css" %>
</style>
但我想让它与 link 和 href 一起工作,因为我想做一个客户端资源引用,而不是服务器端包含。
浏览器请求css文件时发生的事情是这样的: Firefox console screenshot
我认为当浏览器请求 http://localhost:8080/infinance/css/home.css 时会发生什么,Tomcat返回home.jsp。我不知道为什么。
Home.java(servlet)代码:
@WebServlet("/")
public class Home extends HttpServlet {
private static final long serialVersionUID = 1L;
private static DatabaseManager db;
public Home() {
super();
}
public void init() {
db = new DatabaseManager();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/home.jsp");
rd.forward(request,response);
}
}
有什么想法吗?一切都有帮助。 谢谢。
最佳答案
我终于解决了。
如果你写:
@WebServlet("/")
在 servlet (Home.java) 中,当在 servlet 中未指定 URL 时,您将 home.jsp 设置为默认文件。在这种情况下,我没有 url http://localhost:8080/infinance/css/home.css在任何 Servlet 中,因此它总是重定向到 home.jsp 网页。
对我有用的解决方案正在改变:
@WebServlet("/")
改为写作:
@WebServlet("/home")
这样它只会在您编写 http://localhost:8080/infinance/home 时将您重定向到 home.jsp如果您没有指定 url,您会收到 404 错误,但它允许将 css 和 img 与 href 一起使用。
请随意编写其他也可行的解决方案。
关于java - Eclipse 动态 Web 元素使用 Tomcat 从 JSP 链接 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49772368/