本帖最后由 hacket 于 2013-4-1 21:20 编辑
- JSP代碼:
- <div class="blockcode"><blockquote><%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
- <%@ page import="cn.zengfansheng.web.calculator.Calculator" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- </head>
- <%--
- JSP计算器
- --%>
- <body>
- <%-- C --%>
- <%-- 1.将取得的值存入JavaBean --%>
- <jsp:useBean id="calculator" class="cn.zengfansheng.web.calculator.Calculator" scope="page" />
- <%-- 将地址栏中的参数存入JavaBean --%>
- <jsp:setProperty property="*" name="calculator" />
-
- <!-- <jsp:setProperty property="firstNum" name="calculator" param="firstNum"/>
- <jsp:setProperty property="operator" name="calculator" param="operator" />
- <jsp:setProperty property="secondNum" name="calculator" param="secondNum" /> -->
-
- <%--2.处理计算结果--%>
-
- <%
- calculator.operate();
- %>
- <!-- 3.显示计算结果 -->
- 计算结果是:
- <jsp:getProperty property="firstNum" name="calculator"/>
- <jsp:getProperty property="operator" name="calculator"/>
- <jsp:getProperty property="secondNum" name="calculator"/>=
- <jsp:getProperty property="result" name="calculator" />
-
- <%-- V --%>
- <hr />
- <form action="/myday10/calculator/calculator.jsp" method="get">
- <table border="1px" align="center">
- <tr>
- <td>第一个参数:</td>
- <td><input type="text" name="firstNum" /></td>
- </tr>
- <tr>
- <td> 运算符:</td>
- <td>
- <select name="operator">
- <option value="+" selected>+</option>
- <option value="-">-</option>
- <option value="*">*</option>
- <option value="/">/</option>
- </select>
- </td>
- </tr>
- <tr>
- <td>第二个参数:</td>
- <td><input type="text" name="secondNum" /></td>
- </tr>
- <tr >
- <td colspan=2 align="center">
- <input type="submit" value="计算" />
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
复制代码 JavaBean代碼:- package cn.zengfansheng.web.calculator;
- import java.math.BigDecimal;
- // M
- public class Calculator {
- double firstNum ;
- double secondNum ;
- char operator;
- double result;
-
- public Calculator(){
- System.out.println("构造函数开始了");
- }
- public double getFirstNum() {
- System.out.println("getFirstNum success");
- return firstNum;
-
- }
- public void setFirstNum(double firstNum) {
- this.firstNum = firstNum;
- System.out.println("setFirstNum success");
- }
-
-
- public double getSecondNum() {
- System.out.println("getSecondNum success");
- return secondNum;
- }
- public void setSecondNum(double secondNum) {
- this.secondNum = secondNum;
- System.out.println("setSecondNum success");
- }
-
-
- public char getOperator() {
- System.out.println("getOperator success");
- return operator;
- }
- public void setOperator(char operator) {
- this.operator = operator;
- System.out.println("setOperator success");
- }
-
-
- //结果保留小数点后一位
- public double getResult() {
- System.out.println("getResult success");
- BigDecimal bd = new BigDecimal(this.result).setScale(2,BigDecimal.ROUND_HALF_UP);
- return bd.doubleValue();
- }
- public void setResult(double result) {
- this.result = result;
- System.out.println("setResult success");
- }
-
-
- public void operate(){
-
- switch(this.operator){
- case '+': this.result = this.firstNum+this.secondNum;break;
- case '-': this.result = this.firstNum-this.secondNum;break;
- case '*': this.result = this.firstNum*this.secondNum;break;
- case '/': this.result = this.firstNum/this.secondNum;break;
- }
- System.out.println("operate success");
- }
- }
复制代码 結果為:- 构造函数开始了
- setOperator success
- setFirstNum success
- setSecondNum success
- setFirstNum success
- setOperator success
- setSecondNum success
- operate success
- getFirstNum success
- getOperator success
- getSecondNum success
- getResult success
复制代码 我想問一下,
存到JavaBean使用<jsp:setProperty property="*" name="calculator" />時,爲什麽
setFirstNum()
setOperator()
setSecondNum()這個幾個方法會運行兩邊?
|