在Internet Explorer和JSP基本URL(Base URL in Internet E

2019-07-17 16:05发布

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 URL nor 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>

Answer 1:

使用<c:url>从JSTL标签引用在我的JSP文件CSS / JavaScript资源。 这样,你可以肯定的是,CSS / JavaScript资源是相对于应用程序上下文 (上下文路径) 总是引用。


的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>

为了获得更大的解决方案,在这里看到我的回答:
在JSP中添加外部资源(CSS / JavaScript的/图像等) 。



文章来源: Base URL in Internet Explorer and JSP