• 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>
  • 相关阅读:
    理解OO 思想 架构好一个程序之基石!~
    WP7 ZIP 压缩与解压缩
    Gis LBS 应用 剧本 (自己乱想的)
    PHP 入门 环境搭建
    Android 入门必须知道的 英文缩写
    Android 开发 数据结构理解 队列和栈 分析及实现
    c函数中形参为引用的情况;C++中*a和*&a的区别
    如何在.NET中使用尾递归
    [ProjectEuler.net] 25 找到第一个fib数,数位为1000位
    NFA_DFA
  • 原文地址:https://www.cnblogs.com/brolanda/p/4143905.html
Copyright © 2020-2023  润新知