Box.java:
package com.cuit.xywei.box;
public class Box {
long length=01;
long width=01;
long height=01;
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public long getWidth() {
return width;
}
public void setWidth(long width) {
this.width = width;
}
public long getHeight() {
return height;
}
public void setHeight(long height) {
this.height = height;
}
public long getArea(){
return (length*width+length*height+width*height)*2;
}
public long getVolumn(){
return (length*width*height);
}
}
useBox.jsp:
<%@page import="com.cuit.xywei.box.Box"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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%>">
<title>用EL使用JavaBean</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//生成一个javabean
com.cuit.xywei.box.Box box = new com.cuit.xywei.box.Box();
box.setLength(10);
box.setWidth(20);
box.setHeight(30);
request.setAttribute("box", box);
%>
盒子的表面积为:${box.area }
<br>
<c:set var="volumn" value="volumn" />
盒子的容积为:${box[volumn]}
</body>
</html>
这里有个地方不明白,为什么在这里:<c:set var="volumn" value="volumn" />
还有这里:盒子的容积为:${box[volumn]},var的变量的值不是value里面的volumn吗,为什么会变成真正的体积?两句如果改为:${box.volumn}的话也可以正确输出体积的。