标签:
JavaBean简介
package org.it315; public class RegisterBean { private String nickName = null; private boolean male = false; public void setNickName(String nickName) { this.nickName = nickName; } public String getNickName() { return nickName; } public void setMale(boolean male) { this.male = male; } public boolean isMale() { return male; } }
2)Indexed属性
package org.it315; public class RegisterBean { private String [] specialities = new String[]{"none","none","none","none","none"}; public void setSpecialities(String [] sp) { this.specialities = sp; } public void setSpecialities(int index,String sp) { specialities[index] = sp; } public String [] getSpecialities() { return specialities; } public String getSpecialities(int index) { return specialities[index]; } }
JavaBean在JSP中的应用
//例1:testBean.jsp <jsp:useBean id="currentDate" class="java.util.Date"/> <jsp:setProperty name="currentDate" property="time" value="1234567"/> <jsp:getProperty name="currentDate" property="time"/> //例2:testBean.jsp <% java.util.Date currentDate = new java.util.Date(); currentDate.setTime(1234567); out.println(currentDate.getTime()); %>
1)<jsp:useBean>标签
<jsp:useBean id="beanInstanceName " class="package.class" scope="page|request|session|application"/>
<jsp:useBean>标签内部原理
//JSP中的语句: <jsp:useBean id="currentDate" class="java.util.Date"/> //翻译成的Servlet源码: java.util.Date currentDate = null; synchronized (_jspx_page_context) { currentDate = (java.util.Date) _jspx_page_context.getAttribute( "currentDate", PageContext.PAGE_SCOPE); if (currentDate == null){ currentDate = new java.util.Date(); _jspx_page_context.setAttribute("currentDate", currentDate, PageContext.PAGE_SCOPE); } }
<jsp:useBean>标签完整语法
//完整语法: <jsp:useBean id="beanInstanceName" scope="page | request | session | application" { class="package.class" | type="package.class" | class="package.class" type="package.class" | beanName="{package.class | <%= expression %>}" type="package.class" } />
type属性用于指定JavaBean实例对象的引用变量的类型,它必须是JavaBean对象的类名称、超类名称和所实现的接口名称中的一个,其默认值为class属性的设置值。beanName属性用于指定JavaBean的名称,这个名称不是JavaBean实例对象的引用名称,而是JavaBeans规范中定义的一种名称,它将被作为传递给java.beans.Beans类的instantiate方法的参数,例如,a.b.c。a.b.c可以代表一个类的完整名称,也可以代表a/b/c.ser这样的资源文件,java.beans.Beans类的instantiate方法可以从这个资源文件中产生出JavaBean的实例对象。beanName属性值还可以通过一个表达式来产生。
带标签体的<jsp:useBean>标签
//语法: <jsp:useBean ...> Body </jsp:useBean> //功能: //Body部分的内容只在<jsp:useBean>标签创建JavaBean的实例对象时才执行。
2)<jsp:setProperty>标签
//语法格式: <jsp:setProperty name="beanInstanceName" { property="propertyName" value="{string | <%= expression %>}" |property="propertyName" [ param="parameterName" ] | property= "*" }/>
name属性用于指定JavaBean实例对象的名称,其值应与<jsp:useBean>标签的id属性值相同。
property属性用于指定JavaBean实例对象的属性名。
<jsp:setProperty name="beanInstanceName" property="propertyName" value="{string | <%= expression %>}" /> <jsp:setProperty name="beanInstanceName" property="propertyName" param="parameterName" /> <jsp:setProperty name="beanInstanceName" property="propertyName" /> <jsp:setProperty name="beanInstanceName" property="*" /> <jsp:useBean id="beanInstanceName" …/> … <jsp:setProperty name="beanInstanceName" property="propertyName" …/> … </jsp:useBean>
未完待续……
标签:
原文地址:http://www.cnblogs.com/Vae1990Silence/p/4685192.html