将公共引入的文件放到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>
时间: 2024-10-26 10:01:07