---------------------- android培训、java培训、期待与您交流! ----------------------
JSP中关于javaBean组件的标签:
<jsp:useBean>:用于在JSP页面中查找或实例化一个JavaBean组件;
<jsp:setProperty>:用于在JSP页面中设置一个JavaBean组件的属性;
<jsp:getProperty>:用于在JSP页面中获取一个JavaBean组件的属性;
<body>
<jsp:useBean id="person" class="cn.itcast.domain.Person"scope="page">
</jsp:useBean>
<%=person.getName() %>
</body>
</head>
<body>
<jsp:useBean id="person" class="cn.itcast.domain.Person"/>
<jsp:setProperty name="person" property="name" value="XXX"/>
<%=person.getName() %>
<jsp:setProperty name="person" property="name" param="name"/>
<%=person.getName() %>
</body>
</html>
案例:jsp和javaBean做简易计算器!
javaBean部分:
package cn.itcast.domain;
import java.math.BigDecimal;
publicclass CalculatorBean {
private String firstNum="0";
privatecharoperator='+';
private String secondNum="0";
private String result="0";
public String getFirstNum() {
returnfirstNum;
}
publicvoid setFirstNum(String firstNum) {
this.firstNum = firstNum;
}
publicchar getOperator() {
returnoperator;
}
publicvoid setOperator(char operator) {
this.operator = operator;
}
public String getSecondNum() {
returnsecondNum;
}
publicvoid setSecondNum(String secondNum) {
this.secondNum = secondNum;
}
public String getResult() {
returnresult;
}
publicvoid setResult(String result) {
this.result = result;
}
publicvoid calculate(){
BigDecimal first=new BigDecimal(this.firstNum);
BigDecimal second=new BigDecimal(this.secondNum);;
switch(this.operator){
case'+':{
this.result=first.add(second).toString();
break;
}
case'-':{
this.result=first.subtract(second).toString();
break;
}
case'*':{
this.result=first.multiply(second).toString();
break;
}
case'/':{
if(second.doubleValue()==0){
thrownew RuntimeException("被除数不能为零");
}
this.result=first.divide(second,20,BigDecimal.ROUND_HALF_UP).toString();
break;
}
default:
thrownew RuntimeException("运算符只能是+-*、");
}
}
}
Jsp部分:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>计算器</title>
</head>
<body style="text-align: center;">
<jsp:useBean id="calculatorBean" class="cn.itcast.domain.CalculatorBean"/>
<jsp:setProperty name="calculatorBean" property="*"/>
<%
calculatorBean.calculate();
%>
<br/>-------------------------------<br>
计算结果是:
<jsp:getProperty name="calculatorBean" property="firstNum"/>
<jsp:getProperty name="calculatorBean" property="operator"/>
<jsp:getProperty name="calculatorBean" property="secondNum"/>
=
<jsp:getProperty name="calculatorBean" property="result"/>
<br/>-------------------------------<br>
<br/>
<form action="/day09/Calculator.jsp" method="post">
<table>
<tr >
<td colspan="2">简单的计算器</td>
</tr>
<tr>
<td>第一个参数</td>
<td>
<input type="text" name="firstNum">
</td>
</tr>
<tr>
<td>操作符</td>
<td>
<select name="operator">
<option value="+">+</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 cosspan="2">
<input type="submit" value="计算">
</td>
</tr>
</table>
</form>
</body>
</html>
MVC设计模式:model(javaBean)、view(JSP)、controller(servlet)
EL表达式和JSTL快速入门:
JSP页面中:${标识符}àpageContext.findAttribute(“data”)àpage、request、session、application
注:用EL表达式在取数据时,通常用“.”号,取不出来时,用[ ];
${pageContext.request.contextPath}à获取当前web应用名称
JSTL标签库:JSTL是SUN公司开发的一套标签库,使用JSTL可以在页面中实现一些简单的逻辑,从而替换页面中的脚本代码;
在页面中使用JSTL标签需要完成以下2个步骤:
1、 导入jstl.jar和standerd.jar这两个JSTL的jar文件
2、 在JSP页面中使用<%@tablib uri=” ” prifix=” ”%>元素导入标签库
JSTL标签库中常用标签:<c:foreach var=” ” items=” ”><c:if test=” ”>
uri―>standard.jaràMETA-INFàc.tIdàuri
---------------------- android培训、java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima