jsp的<%@ include file="jsp/common.jsp" %>报错误Duplicate local variable basePath

将公共引入的文件放到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-08-07 20:52:07

jsp的<%@ include file="jsp/common.jsp" %>报错误Duplicate local variable basePath的相关文章

在用 &lt;%@include file=&quot;date.jsp&quot; %&gt; &quot;date.jsp&quot;老提示出错,错误为: Multiple annotations found at this line: - Duplicate local variable path - Duplicate local variable basePath 该怎么解决呢?

重复变量 date.jsp文件内部不应该再出现重复的变量定义 也就是<%@include%>是先把文件源代码一模一样的拷贝过来,然后才开始编译 所以如果有相同的变量肯定报错 用了动态的<jsp: include file="top.jsp" />的就正确了 因为<%@include%>引进的是代码,把代码包含进来,而新进JSP时,会默认生成 <% String path = request.getContextPath(); String ba

JSP 使用&lt;%@include%&gt;报Duplicate local variable path 错误 解决方法

错误提示:Multiple annotations found at this line:- Duplicate local variable path- Duplicate local variable basePath 重复变量,因为<%@include%>引进的是代码,把代码包含进来,而新进JSP时,会默认生成<%String path = request.getContextPath();String basePath = request.getScheme()+":/

&lt;jsp:include page=&quot;index.jsp&quot;&gt;和&lt;%@include file=&quot;index.jsp&quot; %&gt;的区别

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd&quo

HTTP Status 500 - Unable to compile class for JSP:Duplicate local variable xxx

jsp页面中使用了:<% String title = com.xunge.base.constant.PlatInfo.title; %> ,由于一个页面中引用的多个jsp页面中都包含该语句,那么就会出现以下错误: 详细错误信息如下图: 解决该问题的方法: 方法一: 保证引用的页面中只有一个页面存在类似语句: 方法二: 参考:jsp页面中JSTL/EL标签引用java后台静态static字段的方法总结

jsp include file(变量) 动态加载文件

include file(变量) 动态加载文件 <%@include file="/includes/<%=id %>/abc.html" %>  这样写系统会报错,提示找不到文件,主要原因是<%=id %>并没有被解析为你想要的id. 好,我们换一种方式,使用<jsp:include page="/includes/<%=id %>/adc.html"></jsp:include>是不是可以呢

jsp的静态包含与动态包含:&lt;%@ include file=&quot;&quot; %&gt;和&lt;jsp:include page=&quot;&quot;&gt;&lt;/jsp:include&gt;区别与分析

<%@ include file="" %>是将文件原封不动的copy进现有的文件中,像是拼接好后,再编译成为servlet运行. <jsp:include page=""></jsp:include>是编译后的servlet运行到该句时,跳转到指定的jsp编译的那个servlet继续运行,然后将运行结果,copy到现在的jsp中,故包含与被包含文件都是单独运行的. 在开发过程中,我们需要正确选择使用.举个例子: 比如在工程项目中

&lt;jsp:include page=&quot;&quot;&gt;和&lt;%@ include file=&quot;&quot;%&gt;区别总结

<jsp:include page="">和<%@ include file=""%>区别总结 1:<jsp:include page="top.jsp">:先将top.jsp中的java脚本和jsp指令都执行完毕以后再将top.jsp页面加入到引用页面中. 2:<%@ include file="top.jsp"%>静态读取:则是将top.jsp的整个页面不加解析(无论是脚本还

&lt;%@ include file=""%&gt;与&lt;jsp:include page=""/&gt;区别

我们都知道在jsp中include有两种形式,分别是Include指令:<%@ include file=""%>和include动作:<jsp:include page="" flush="true"/>.前者是指令元素.后者是行为元素.具体它们将在何处用?如何用及它们有什么区别?这应该是很多人看到它都会想到的问题.下面一起来看看吧. 通常当应用程序中所有的页面的某些部分(例如标题.页脚和导航栏)都相同的时候,我们就可以

&lt;%@ include file=&quot;&quot;%&gt;与&lt;jsp:include page=&quot;&quot;/&gt;区别(转)

http://www.iteye.com/topic/312500/ 我们都知道在jsp中include有两种形式,分别是Include指令:<%@ include file=""%>和include动作:<jsp:include page="" flush="true"/>     前者是指令元素.后者是行为元素.具体它们将在何处用?如何用及它们有什么区别?这应该是很多人看到它都会想到的问题.下面一起来看看吧.