jsp的<%@ include file="jsp/common.jsp" %>報錯誤Duplicate local variable basePath

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.