zoukankan      html  css  js  c++  java
  • JSP基本语法--包含指令<%@include file="路径"%> <jsp:include page>

    包含指令,真正改变的地方只有具体内容处:

    方法1: 在每个jsp页面(HTML)都包含工具栏,头部信息,尾部信息,具体内容

    方法2: 将工具栏,头部信息,尾部信息都分成各个独立的文件,使用的时候直接导入进来

    方法2比1更好,如果采用第一种做法的话,很多代码会出现重复的问题。第二种形式的话,修改的时候很方便,因为是导入进来的。

    包含分为静态包含和动态包含。

    • 静态包含

    方式: <%@include file="路径"%>,很类似于<%@page=%>命令

    info.html

    <h2><font color="red">info.htm</font></h2>

    info.jsp:

    <h2><font color="green"><%="info.jsp"%></font></h2>

    info.inc:

    <h2><font color="blue">info.inc</font></h2>

    include_demo01.jsp:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
        <h1>静态包含操作</h1>
        <%@ include file="info.htm"%>
        <%@ include file="info.jsp"%>
        <%@ include file="info.inc"%>
    </body>
    </html>

    结果如下:

    3个文件都被包含进来,就是将内容直接替换,好比程序中的变量一样。

    • 动态包含:

    方式:

    <jsp:include page=“{要完结的路径|<%=表达式%>}” flush="true/false">

      <jsp:param name="参数名称" value="参数内容"/>

    </jsp:include>

    只要代码以<>出现的jsp代码,都称为标签指令,所有的标签都要完结:

    例子:include_demo02.jsp:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
        <h1>动态包含操作</h1>
        <jsp:include page="info.htm"/>
        <jsp:include page="info.jsp"/>
        <jsp:include page="info.inc"/>
    </body>
    </html>

    如果使用参数的话先定义一个接收参数的文件:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <h1>参数一:<%=request.getParameter("name")%></h1>
    <h1>参数二:<%=request.getParameter("info")%></h1>

    再定义一个传递参数的文件:

    注意,要传递变量通过表达式完成,而不用out.println()

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
        <h1>动态包含操作</h1>
        <%
            String username = "LiXingHua" ;
        %>
        <jsp:include page="receive_param.jsp">
            <jsp:param name="name" value="<%=username%>"/>
            <jsp:param name="info" value="www.mldnjava.cn"/>
        </jsp:include>
    </body>
    </html>

    现在,有静态包含和动态包含,使用哪种?

    看下面的例子:

    include.jsp;

    <%
        int x = 10 ;
    %>
    <h1>include.jsp -- <%=x%></h1>

    下面通过静态包含语句,包含以上文件,并定义一个与之前一样同名的变量:

    include_demo04:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
        <%
            int x = 100 ;    // 变量重复
        %>
        <h1>include_demo04.jsp -- <%=x%></h1>
        <%@include file="include.jsp"%>
    </body>
    </html>

    程序运行结果,出现500错误,因为x变量重复了,原因:静态包含的处理原则:

    静态包含属于先包含后处理:

    动态包含include_demo05.jsp:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
        <%
            int x = 100 ;    // 变量重复
        %>
        <h1>include_demo04.jsp -- <%=x%></h1>
        <jsp:include page="include.jsp"/>
    </body>
    </html>

    结果如下:

    并没有发生任何错误,因为动态包含的时候,如果包含动态页面,原则是先处理后包含的方式,包含的是处理的结果。

    所以,动态包含操作更容易使用,因为开发中容易发生重复定义变量的问题,所以建议多使用动态包含。

  • 相关阅读:
    Silverlight 2 开发环境
    Silverlight: 通过LINQ 和Isolated Storage构建客户端数据库
    奥巴马就职委员会选择微软Silverlight技术
    Silverlight 和WPF的Composite Guidance(Prism V2)发布了
    分布式计算平台:Dryad
    Ironclad
    WCF安全指南
    WPF/Silverlight中的Command
    Silverlight 2 控件 SDK 源代码
    Mono 2.2 发布了
  • 原文地址:https://www.cnblogs.com/wujixing/p/4949038.html
Copyright © 2011-2022 走看看