<jsp:getProperty>标签:用于在JSP页面中获取一个JavaBean组件的属性
一、计算器的实体Bean
package web13.domain;
public class Calculator {
private double firstNum;
private double secondNum;
private String oper;
private double result;
public Calculator() {
super();
// TODO Auto-generated constructor stub
}
public Calculator(double firstNum, double secondNum, String oper,
double result) {
super();
this.firstNum = firstNum;
this.secondNum = secondNum;
this.oper = oper;
this.result = result;
}
public double getFirstNum() {
return firstNum;
}
public void setFirstNum(double firstNum) {
this.firstNum = firstNum;
}
public double getSecondNum() {
return secondNum;
}
public void setSecondNum(double secondNum) {
this.secondNum = secondNum;
}
public String getOper() {
return oper;
}
public void setOper(String oper) {
this.oper = oper;
}
public void calculator(){
if("+".equals(oper)){
result=firstNum+secondNum;
}else if("-".equals(oper)){
result=firstNum-secondNum;
}else if("*".equals(oper)){
result=firstNum*secondNum;
}else if("/".equals(oper)){
result=firstNum/secondNum;
}
}
public double getResult() {
calculator();
return result;
}
public void setResult(double result) {
this.result = result;
}
}
二、jsp页面(cal.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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>My JSP 'cal.jsp' starting page</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>
<div>
<jsp:useBean id="c" scope="request" class="web13.domain.Calculator"></jsp:useBean>
<jsp:setProperty property="firstNum" name="c" param="firstNum"/>
<jsp:setProperty property="secondNum" name="c" param="secondNum"/>
<jsp:setProperty property="oper" name="c" param="oper"/>
<c:if test="${c.oper!=null }">计算结果:
<jsp:getProperty property="firstNum" name="c"/>
<jsp:getProperty property="oper" name="c"/>
<jsp:getProperty property="secondNum" name="c"/>=
<jsp:getProperty property="result" name="c"/>
</c:if>
<form action="./cal.jsp" method="post">
<table>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="2">简单计算器</td>
</tr>
<tr>
<td>第一个参数</td>
<td><input type="text" name="firstNum"/></td>
</tr>
<tr>
<td>操作数</td>
<td>
<select name="oper">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
</td>
</tr>
<tr>
<td>第二个参数</td>
<td><input type="text" name="secondNum"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="计算"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>