Tag标记中使用attribute和variable指令(习题六) --jsp程序设计

一、作业要求:

p59 习题6,求圆和梯形面积

二、源码分析

2.1 lianxi6.jsp

<%--
  Created by IntelliJ IDEA.
  User: corrine
  Date: 2019/3/26
  Time: 9:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="RestTag" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="CircleTag" %>
<html>
<head>
    <title>练习六</title>
</head>
<body>
<h3>以下是调用Rest.tag文件的效果:</h3>
<RestTag:Rect numberA="5" numberB="6"></RestTag:Rect>
<%=RectMessage%>
<%=RectArea%>
<h3>以下是调用Circle.tag文件的效果:</h3>
<CircleTag:Circle number="5"/>
<%=CircleMessage%>
<%=CircleArea%>
</body>
</html>

2.2 Circle.tag

<%@tag pageEncoding="utf-8" %>
<%@attribute name="number" required="true" %>
<%@variable name-given="CircleArea" variable-class="java.lang.Double" scope="AT_END" %>
<%@variable name-given="CircleMessage" scope="AT_END" %>
<%!
    static double PI = 3.14;
    public double getCircleArea(double a){
        if (a!=0){
            double area =a*a*PI;
            return area;
        }else
            return -1;
    }
%>
<%  try{
    double a=Double.parseDouble(number);
    double result =0;
    result = getCircleArea(a);
    jspContext.setAttribute("CircleArea",new Double(result));
    jspContext.setAttribute("CircleMessage","半径为"+a+"的圆面积为:");
}catch (Exception e){
    jspContext.setAttribute("CircleArea",new Double(-1.0));
    jspContext.setAttribute("CircleMessage",""+e.toString());
}
%>

2.3 Rect.tag

<%@tag pageEncoding="utf-8" %>
<%@attribute name="numberA" required="true" %>
<%@attribute name="numberB" required="true" %>
<%@variable name-given="RectArea" variable-class="java.lang.Double" scope="AT_END" %>
<%@variable name-given="RectMessage" scope="AT_END" %>
<%!
    public double getRectArea(double a,double b){
        if (a!=0&&b!=0){
            double area =a*b;
            return area;
        }else
            return -1;
    }
%>
<%  try{
    double a=Double.parseDouble(numberA);
    double b=Double.parseDouble(numberB);
    double result =0;
    result = getRectArea(a, b);
    jspContext.setAttribute("RectArea",new Double(result));
    jspContext.setAttribute("RectMessage","长为"+a+",宽为"+b+"的矩形面积为:");
}catch (Exception e){
    jspContext.setAttribute("RectArea",new Double(-1.0));
    jspContext.setAttribute("RectMessage",""+e.toString());
}
%>

三、结果图:

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值