Analysis of External Resource Relative Path Problem in jsp and css
- 2021-06-28 13:40:14
- OfStack
Add base to the jsp page, using relative paths:
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
And then < head > Add base to Label
< base href=" < %=basePath% > " > < /base >
When introducing external files "directly" on this page, you can do so directly
<script src="js/common/jquery-1.11.1.min.js" language="javascript"
type="text/javascript"></script>
<script src="js/common/frame.js" language="javascript"
type="text/javascript"></script>
<link href="css/common/frame.css"
rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
Similarly, the css class on this page
.top {
position: absolute;
left: 0;
top: 95px;
right: 0;
height: 120px;
background: url(images/common/title.jpg) repeat-x
}
=========================Exceptions: when introducing external css, js files and introducing image, etc. ===========================
If relative paths are also used at this time, since they are no longer in the jsp page, relative paths are relative to the directory where this css file is located:
For example: Begong New System, css file introduced in index.jsp page
< link href="css/common/frame.css" rel="stylesheet" type="text/css" / > On the jsp page, js/common/frame.js was introduced, starting from the website and directory, and that's OK.
However, in frame.js, the following css exists
.show_menu{
background-image: url(images/left_bg.gif);
background-repeat: repeat-y;
background-position:285px 51px;
}
In this case, direct url (images/left_bg.gif;Is the default from the directory where this css file is located (/css/common/frame.css) +url (images/left_bg.gif), so the image needed here needs to be configured separately.
Change to
.show_menu{
background-image: url(../../images/left_bg.gif);
background-repeat: repeat-y;
background-position:285px 51px;
}