问题描述
Internet Explorer不支援HTML 使用< c: url> 来自JSTL的标记,以引用JSP文件中的CSS / JavaScript资源。通过这样做,您可以确保CSS / JavaScript资源始终相对于应用程序上下文(上下文路径)被引用。
示例
index.jsp:
<%@ page language =javacontentType =text / html; charset = UTF-8pageEncoding =UTF-8%>
<!DOCTYPE html>
< html>
< head>
< title> Some Title< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< link type =text / css =stylesheethref =< c:url value =/ css / main.css/> />
< script type =text / javascriptsrc =< c:url value =/ js / utils.js/>>< / script>
< script type =text / javascriptsrc =< c:url value =/ js / jquery-1.8.3.js/>>< / script>
< / head>
< body>
...
< / body>
< / html>
有关更多解决方案,请参阅我的答案:
。
Internet Explorer doesn't support HTML <base> tag and even other browsers do, there are some problems when redirect takes place inservletsto some.jsppages for examplerequest dispatching.`
It's feasible to add ${pageContext.request.contextPath} with each URLnor request.getServletPath()
JSP relative links for CSS and images with servlets forwarding may change things a lot. This link : Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
Is there a better approach with JSP / servlets or it's just an IE issue?
Link : HTML <base> TAG and local folder path with Internet Explorer
And if it is an IE issue:
1. how to fix the IE issue as the above post is unable to give a valid answer?
2. how to solve it with JSP / servlets?
My website is now showing CSS and images.
E.g. HTML output is:
<base href="http://localhost:8080/Alpinema/" /> is not working for <link media="all" rel="stylesheet" type="text/css" href="css/all.css">
It works in other browsers like Firefox and Chrome.
My JSP code portion:
<head> <base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" /> <meta charset="utf-8"> <title>Alpinema.com</title> <link media="all" rel="stylesheet" type="text/css" href="css/all.css"> /css?family=Merriweather|PT+Sans:700|Nobile:400italic' rel='stylesheet' type='text/css'> </head>
Use <c:url> tag from JSTL to reference CSS/JavaScript resources inside my JSP files. By doing so you can be sure that the CSS/JavaScript resources are referenced always relative to the application context (context path).
Example
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <title>Some Title</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href="<c:url value="/css/main.css" />" /> <script type="text/javascript" src="<c:url value="/js/utils.js" />"></script> <script type="text/javascript" src="<c:url value="/js/jquery-1.8.3.js" />"></script> </head> <body> ... </body> </html>
For even more solutions see my answer here:
Adding external resources (CSS/JavaScript/images etc) in JSP.
这篇关于Internet Explorer和JSP中的基本URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!