JSP tag文件的attribute指令和variable指令的使用

tag文件的attribute指令和variable指令


一、attribute指令

1、attribute指令的作用与用法

attribute指令本质就是JSP页面向tag文件传输数据的一种应用

格式:在Tag中
<%@ attribute name=“对象名字” required=“true” type=“对象类型” %>
在引用Tag的JSP中
<前缀: Tag文件名字 对象名字=“对象的引用” />

<前缀: Tag文件名字 对象名字=“对象的引用” >
标记体
</前缀: Tag文件名字 >

如:tag中定义:<%@ attribute name=“length” required=“true” %>
JSP中代码: <beijing: AddSum length=“1000” />

2、运用实例

1 example3_3.jsp

<%--
  Created by IntelliJ IDEA.
  User: ruochen
  Date: 2020/10/29
  Time: 8:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="computer"%>
<html>
<body>
<h3>以下是调用Tag文件的效果:</h3>
<computer:Triangle sideA="5" sideB="6" sideC="7"/>
</body>
</html>

2 Triangle.tag

<%@ tag  pageEncoding="utf-8" %>
<h4>这是一个Tag文件,负责计算三角形的面积。
    <%@ attribute name="sideA" required="true" %>
    <%@ attribute name="sideB" required="true" %>
    <%@ attribute name="sideC" required="true" %>
        <%!   public String getArea(double a,double b,double c) {
                if(a+b>c&&a+c>b&&c+b>a) {
                     double p=(a+b+c)/2.0;
                     double area=Math.sqrt(p*(p-a)*(p-b)*(p-c)) ;
                     return "<BR>三角形的面积:"+area;
                }
                else
                    return("<BR>"+a+","+b+","+c+"不能构成一个三角形,无法计算面积");
           }
  %>
        <%   out.println("<BR>JSP页面传递过来的三条边:"+sideA+","+sideB+","+sideC);
          double a=Double.parseDouble(sideA);
          double b=Double.parseDouble(sideB);
          double c=Double.parseDouble(sideC);
          out.println(getArea(a,b,c));
  %>


3 效果图与总结

在这里插入图片描述
通过<computer:Triangle sideA=“5” sideB=“6” sideC=“7”/>
向Triangle.tag文件传输三个数据
再通过
<%@ attribute name=“sideA” required=“true” %>
<%@ attribute name=“sideB” required=“true” %>
<%@ attribute name=“sideC” required=“true” %>
接收三个数据,从而完成tag文件从jsp文件中获取数据的需求

二、variable指令

1.variable指令的作用与用法

variable指令本质就是实现Tag向JSP返回数据。

格式:
1.在Tag中首先声明:
<%@ variable name-given=“对象名字” variable-class=“对象类型” scope=“有效范围” %>

2.然后调用jspContext内置对象的setAttribute()将对象存储到jspContext中,以便JSP调用。如:
jspContext.setAttribute(“time”, new Date());

3.那么在JSP中,可以直接通过对象名来使用这个对象。如:
int year=time.getYear()+1900;
该代码使用Date的getYear()方法,将Tag传送的time对象中记录的时间年赋给year变量。

注意
1. 返回的是一个对象。
2. JSP中不可再定义与name具有相同名字的变量,否则会出现编译错误。
3. scope定义了variable在JSP中的使用范围,可取值为AT_BEGIN、NESTED和AT_END。
AT_BEGIN: JSP页面一旦使用Tag,就可以使用variable给出的对象。
NESTED: JSP页面只可以在Tag标记的标记体中使用variable给出的对象。
AT_END: JSP页面在Tag标记结束后才可以使用variable给出的对象。

2、运用实例

1 uesone.jsp

<%--

  Created by IntelliJ IDEA.
  User: ruochen
  Date: 2020/10/29
  Time: 9:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="computer"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<computer:giveroot coefficientA="3" coefficientB="6" coefficientC="-2"/>
<h4> 方程的根以及计算两个根的和:
        <%
           if(rootOne!=null&&rootTwo!=null){
              double r1=rootOne.doubleValue();   //rootOne是giveroot.tag文件返回的Double型对象
              double r2=rootTwo.doubleValue();  //rootTwo是giveroot.tag文件返回的Double型对象
              out.println("<br>根1:"+r1);
              out.println("<br>根2:"+r2);
              double sum=r1+r2;
              out.println("<br>根1与根2之和:"+sum);
          }
          else{
               out.println("<br>方程没有实根");
          }
  %>
</body>
</html>


2 giveroot.tag

<%@ tag  pageEncoding="UTF-8" %>
<%@ attribute name="coefficientA" required="true" %>
<%@ attribute name="coefficientB" required="true" %>
<%@ attribute name="coefficientC" required="true" %>
<%@ variable name-given="rootOne" variable-class="java.lang.Double" scope="AT_END" %>
<%@ variable name-given="rootTwo" variable-class="java.lang.Double" scope="AT_END" %>
<%   double disk,root1,root2;
    double a=Double.parseDouble(coefficientA);
    double b=Double.parseDouble(coefficientB);
    double c=Double.parseDouble(coefficientC);
    disk=b*b-4*a*c;
    if(disk>=0&&a!=0){
        root1=(-b+Math.sqrt(disk))/(2*a);
        root2=(-b-Math.sqrt(disk))/(2*a);
        jspContext.setAttribute("rootOne",root1);     //为JSP页面返回对象rootOne
        jspContext.setAttribute("rootTwo",root2);    //为JSP页面返回对象rootTwo
    }
    else{
        jspContext.setAttribute("rootOne",null);
        jspContext.setAttribute("rootTwo",null);
    }
%>

3 效果图与总结

在这里插入图片描述

通过<computer:giveroot coefficientA=“3” coefficientB=“6” coefficientC="-2"/>
向giveroot.tag文件传输三个数据
再通过
<%@ attribute name=“sideA” required=“true” %>
<%@ attribute name=“sideB” required=“true” %>
<%@ attribute name=“sideC” required=“true” %>
接收数据
通过variable指令向jsp页面传输数据

<%@ variable name-given=“rootOne” variable-class=“java.lang.Double” scope=“AT_END” %>
<%@ variable name-given=“rootTwo” variable-class=“java.lang.Double” scope=“AT_END” %>
调用jspContext内置对象的setAttribute()将对象存储到jspContext中,以便JSP调用。
jspContext.setAttribute(“rootOne”,root1);

总结

提示:这里对文章进行总结:
attribute指令是jsp页面向tag文件传输数据
variable指令是从tag文件向jsp页面传输数据

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是 GetArea.tag 文件的代码: ```html <%@ tag language="java" pageEncoding="UTF-8" %> <%@ attribute name="side1" required="true" %> <%@ attribute name="side2" required="true" %> <%@ attribute name="side3" required="true" %> <%@ variable %> <% double a = Double.parseDouble((String)pageContext.getAttribute("side1")); double b = Double.parseDouble((String)pageContext.getAttribute("side2")); double c = Double.parseDouble((String)pageContext.getAttribute("side3")); double p = (a + b + c) / 2; double area = Math.sqrt(p * (p - a) * (p - b) * (p - c)); pageContext.setAttribute("area", String.format("%.3f", area)); %> ``` 在 JSP 页面中,可以使用以下代码调用 GetArea.tag 文件: ```html <%@ taglib prefix="mytag" tagdir="/WEB-INF/tags" %> <mytag:GetArea side1="3" side2="4" side3="5" /> ``` 其中,side1、side2 和 side3 分别表示三角形的三边长度。在 GetArea.tag 文件中,我们使用variable 指令将三角形的面积保存在 pageContext 中,可以在 JSP 页面中使用 EL 表达式 ${area} 获取三角形的面积。 对于 one.jsp,可以使用以下代码显示三角形的面积: ```html <mytag:GetArea side1="3" side2="4" side3="5" /> 三角形的面积为:${area} ``` 对于 two.jsp,可以使用以下代码显示三角形的面积: ```html <mytag:GetArea side1="3" side2="4" side3="5" /> 三角形的面积为:${area} ``` 注意,我们在 GetArea.tag 文件使用了 String.format 方法格式化三角形的面积,保留了最多 3 位或 6 位小数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值