总结
- 本来想实现from表单在单选框被选取后产生变动的功能,但是不知道怎么做,赶作业就选择先直接给tag的参数赋值。
- 使用idea过程中发现
jspContext.setAttribute()
函数爆红,原因是因为idea本身不会自动导入servlet-api.jar
和jsp-api.jar
这两个包。需要手动添加。这两个包可以在tomcat的目录apache-tomcat-8.5.35\lib
下找到。
JSP文件
lianxi6.jsp
<%@ page import="jdk.internal.util.xml.impl.Input" %><%--
Created by IntelliJ IDEA.
User: Atlantis
Date: 2019-03-24
Time: 19:04
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 prefix="CircleTag" tagdir="/WEB-INF/tags/tagsTwo" %>
<html>
<head>
<title>练习六</title>
</head>
<body>
<h3>以下是调用Rest.tag文件的效果:</h3>
<RestTag:Rest numberA="5" numberB="6"/>
<%=RectMessage%>
<%=RectArea%>
<h3>以下是调用Circle.tag文件的效果:</h3>
<CircleTag:Circle number="5"/>
<%=CircleMessage%>
<%=CircleArea%>
</body>
</html>
tag文件
Rest.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","矩形的面积");
}catch (Exception e){
jspContext.setAttribute("RectArea",new Double(-1.0));
jspContext.setAttribute("RectMessage",""+e.toString());
}
%>
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","圆的面积");
}catch (Exception e){
jspContext.setAttribute("CircleArea",new Double(-1.0));
jspContext.setAttribute("CircleMessage",""+e.toString());
}
%>
运行结果