📑前言
本文主要是【Java】——JavaBean简单使用的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:阿里云主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
JavaBean基本使用
以圆的面积周长及四则运算为例
圆的面积周长
java类Circle:
package javabean;
public class Circle {
private double radius;
private double area;
private double length;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
this.area = this.radius*this.radius*3.14;
return area;
}
public void setArea(double area) {
this.area = area;
}
public double getLength() {
this.length = 2*3.14*this.radius;
return length;
}
public void setLength(double length) {
this.length = length;
}
}
jsp :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="c" scope="request" class="javabean.Circle"/>
//注入Circle为bean对象 id为c
<jsp:setProperty name="c" property="*"/>
//设置属性的name都为c
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Circle</title>
</head>
<body align="center">
<form action="" method="post" >
<h1>输入圆的半径,求圆的面积和周长</h1>
半径:<input type=text name="radius" value="<jsp:getProperty property="radius" name="c"/>" >
<input type=submit value="提交">
</form>
圆的半径是:
<jsp:getProperty name="c" property="radius"/><br>
面积是:
<jsp:getProperty name="c" property="area"/><br>
周长是:
<jsp:getProperty name="c" property="length"/>
</body>
</html>
运行结果:
四则运算
计算类Calculator:
package test2;
public class Calculator {
double a;
double b;
String op="";
String ans="";
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public String getOp() {
return op;
}
public void setOp(String op) {
this.op = op;
}
public String getAns() {
if("+".equals(this.op)) {
this.ans = String.valueOf(a+b);
}else if("-".equals(this.op)) {
this.ans = String.valueOf(a-b);
}else if("*".equals(this.op)) {
this.ans = String.valueOf(a*b);
}else if("/".equals(this.op)) {
if(b==0) {
this.ans = "除数不能为0";
}else {
this.ans = String.valueOf(a/b);
}
}
return ans;
}
public void setAns(String ans) {
this.ans = ans;
}
}
Calculator.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="c" scope="request" class="test2.Calculator"/>
//注入Circle为bean对象 id为c,注意类在test2包下
<jsp:setProperty name="c" property="*"/>
//设置属性的name都为c
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Circle</title>
</head>
<body align="center">
<form action="" method="post" >
<h1>输入数字a,数字b及运算符</h1>
a:<input type=text name="a" value="<jsp:getProperty property="a" name="c"/>" >
<select name="op">
<option value="<jsp:getProperty property="op" name="c"/>" selected="selected">
<jsp:getProperty property="op" name="c"/>
</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
b:<input type=text name="b" value="<jsp:getProperty property="b" name="c"/>" >
=<jsp:getProperty property="ans" name="c"/>
<input type=submit value="提交">
</form>
</body>
</html>
运行结果: