一个类想要成为javabean有以下几个要求:
- 提供一个默认的无参构造函数。
- 需要被序列化并且实现了Serializable接口。
- 可能有一系列可读写属性。
- 可能有一系列的"getter"或"setter"方法
public class User implements Serializable{
private String name;
private String password;
public User(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
javabean的引入:
<body>
<%--id:对象名
class:类
scope:该对象会存在的域
application session request page
--%>
<jsp:useBean id="user" class="com.xxjqr.JspStudy.date0901.User" scope="page">
</jsp:useBean>
</body>
原理:
javabean只是一个特殊的类,使用userBean标签引用bean其结果就是创建了一个bean对象;首先会通过对应的域对象去获取bean对象,如果bean对象为空,那么根据对应的灰创建对应的bean对象;
(打开翻译后的java代码可见)
user = (com.xxjqr.JspStudy.date0901.User) _jspx_page_context.getAttribute("user", javax.servlet.jsp.PageContext.PAGE_SCOPE);
if (user == null){
user = new com.xxjqr.JspStudy.date0901.User();
_jspx_page_context.setAttribute("user", user, javax.servlet.jsp.PageContext.PAGE_SCOPE);
out.write('\r');
out.write('\n');
out.write(' ');
}
注意:
javabean的使用:
<body>
<jsp:useBean id="user" class="com.xxjqr.JspStudy.date0901.User"></jsp:useBean>
<%--
手动设置属性
<jsp:setProperty property="name" name="user" value="丁昌江"/>
--%>
<%--
1. 从参数中设置属性
<jsp:setProperty property="name" name="user" param="name"/>
<jsp:setProperty property="password" name="user" param="password"/>
2. 从参数中设置属性(javabean类中的参数名要和提交参数名一样哦)
--%>
<jsp:setProperty property="*" name="user"/>
<%--获取属性
1. <%=user.getName() %>
2. name:${user.name } //el表达式和上面等价
--%>
name:<jsp:getProperty property="name" name="user"/><br/>
password:${user.password }
</body>
javabean练习之计算器:
Calc.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<style type="text/css">
.myBody{
margin:0px auto;
padding: 10px;
width: 300px;
height: 500px;
}
</style>
</head>
<body class="myBody">
<jsp:useBean id="calc" class="com.xxjqr.JspStudy.d0902.Calc"></jsp:useBean>
<jsp:setProperty property="*" name="calc"/>
<%
calc.calculate();
%>
<form action="${pageContext.request.contextPath}/Calc/Calc.jsp" method="get">
<div class="form-group">
<label for="exampleInputEmail1">第一个值</label>
<input type="text" name="firstParam" value="" class="form-control" id="exampleInputEmail1" placeholder="">
</div>
<div class="form-group">
<label for="exampleInputPassword1">第二个值</label>
<input type="text" name="secondParam" value="" class="form-control" id="exampleInputPassword1" placeholder="">
</div>
<div class="form-group">
操作符
<select name="operator" class="form-control">
<option value='+'>+</option>
<option value='-'>-</option>
<option value='*'>*</option>
<option value='/'>/</option>
</select>
</div>
<button type="submit" class="btn btn-default">计算</button>
</form>
<hr/>
结果:${calc.result}
</body>
</html>
Calc.java
public class Calc implements Serializable{
private String firstParam = "0";
private String secondParam = "0";
private char operator = '+';
private String result = "0";
public Calc(){}
public String getFirstParam() {
return firstParam;
}
public void setFirstParam(String firstParam) {
this.firstParam = firstParam;
}
public String getSecondParam() {
return secondParam;
}
public void setSecondParam(String secondParam) {
this.secondParam = secondParam;
}
public char getOperator() {
return operator;
}
public void setOperator(char operator) {
this.operator = operator;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String calculate(){
BigDecimal first=new BigDecimal(firstParam);
BigDecimal second=new BigDecimal(secondParam);
switch(operator){
case '+':
result = first.add(second).toString();
break;
case '-':
result = first.subtract(second).toString();
break;
case '*':
result = first.multiply(second).toString();
break;
case '/':
if (second == new BigDecimal(0))
result = "操作错误";
result = first.divide(second).toString();
break;
}
return result;
}
}