引入頁面時候
${pageContext.request.contextPath}為頁面上下文路徑;
也可以用js來實現;
<script type="text/javascript" src="${pageContext.request.contextPath}/js/student_evaluation/studentInfo.js"></script> 相當於: <script type="text/javascript" id="studentInfoJS"></script> <script type="text/javascript"> document.getElementById('studentInfoJS').src = basePath + 'js/student_evaluation/studentInfo.js'; </script>
基礎頁面可以寫成如下的代碼;
var basePath = "<%= basePath%>"; 實現全局路徑變量,不過${pageContext.request.contextPath}完全可以代替了;
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html>
<html>
<script type="text/javascript">
var basePath = "<%= basePath%>";
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>學生成績信息</title>
<link rel="stylesheet" type="text/css"
href="<%=basePath%>jquery/jquery.bsgrid-1.37/builds/merged/bsgrid.all.min.css">
<script type="text/javascript"
src="<%=basePath%>jquery/jquery-1.12.3.min.js"></script>
<script type="text/javascript"
src="<%=basePath%>jquery/jquery.bsgrid-1.37/builds/js/lang/grid.zh-CN.min.js"></script>
<script type="text/javascript"
src="<%=basePath%>jquery/jquery.bsgrid-1.37/builds/merged/bsgrid.all.min.js"></script>
<script type="text/javascript" src="<%=basePath%>jquery/jquery.bsgrid-1.37/builds/js/util.min.js"></script>
</head>
<body>
</body>
</html>
通過以下語句引入:
<jsp:include page="/view/base.jsp" flush="true" />
關鍵時刻還是要用這樣直觀一點:
<script type="text/javascript"> var pp = "${pageContext.request.contextPath}"; alert(pp); </script>
但是引入的js文件裡面這樣是取不到值的,所以繼承還是有必要的。
好了,先寫到這裡吧,有啥疑問在溝通啊!