zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    【高端黑】软件工程师去理发店
    [SQL]用于提取组内最新数据,左连接,内连接,not exist三种方案中,到底谁最快?
    Oracle数据库访问客户端 sqldeveloper-19.2.1.247.2212-x64 下载
    《木兰辞》中最精彩的六句
    SqlComparison
    别让情绪扰乱心绪
    50年内神秘消失的恒星
    java命名总结
    针对nginx,来具体聊聊正向代理与反向代理 (转载)
    Nginx可以做什么?(转载)
  • 原文地址:https://www.cnblogs.com/brolanda/p/4143905.html
Copyright © 2011-2022 走看看