標籤:style blog io ar color 使用 sp java strong
將公用引入的檔案放到common.jsp中,其他頁面引入該jsp即可使用
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 %> 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html>10 <head>11 <base href="<%=basePath%>">12 <%@ include file="jsp/common.jsp" %>13 <title>My JSP ‘index.jsp‘ starting page</title>14 </head>
此時報:Duplicate local variable basePath
因為<%@ include file="jsp/common.jsp" %>是講file指定的頁面代碼完全放入到你的頁面中,這樣,相當於聲明了兩次
<base href="<%=basePath%>">,所以報了重複的錯誤。
分析:<%@ include file="" %>和<jsp:include page=""></jsp:include>區別與分析
<%@ include file="" %>是將檔案原封不動的copy進現有的檔案中,像是拼接好後,再編譯成為servlet運行。
<jsp:include page=""></jsp:include>是編譯後的servlet運行到該句時,跳轉到指定的jsp編譯的那個servlet繼續運行,然後將運行結果,copy到現在的jsp中,故包含與被包含檔案都是單獨啟動並執行。
解決辦法:為了達到目的,我們可以在一個jsp檔案中,只聲明要使用的檔案的引入,而不需要指定base等,如下:
<script type="text/javascript" src="js/alert.js" charset="UTF-8"></script><script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.easyui.min.js" charset="UTF-8"></script><script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.min.js" charset="UTF-8"></script><script type="text/javascript" src="js/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js" charset="UTF-8"></script><link rel="stylesheet" href="js/jquery-easyui-1.4.1/themes/icon.css" type="text/css" charset="UTF-8"></link><link rel="stylesheet" href="js/jquery-easyui-1.4.1/themes/color.css" type="text/css" charset="UTF-8"></link><link rel="stylesheet" href="js/jquery-easyui-1.4.1/themes/default/easyui.css" type="text/css" charset="UTF-8"></link>
其中js和css檔案均以webapp為根指定相應的引用地址。
此時在在index.jsp中可以使用<%@ include file="jsp/common.jsp" %>即可解決問題
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><%@ include file="jsp/common.jsp" %><title>My JSP ‘index.jsp‘ starting page</title></head>
jsp的<%@ include file="jsp/common.jsp" %>報錯誤Duplicate local variable basePath