JavaBean Introduction:
JavaBean is actually a Java class that follows a specific notation, but must have the following characteristics:
1.这个java类必须具有一个公开的无参构造函数 2.属性必须私有化,类必须公开 3.私有化的属性必须通过public类型的方法暴露给其他程序,并且方法的命名也必须遵守一定的命名规范---getXxx和setXxx方法。
Defined:
把一个拥有对属性进行set和get方法的类,我们就可以称之为JavaBean。实际上JavaBean就是一个java类,在这个java类中就默认形成了一种规则——对属性进行设置和获得。而反之将说ava类就是一个JavaBean,这种说法是错误的,因为一个java类中不一定有对属性的设置和获得的方法(也就是不一定有set和get方法)。
To access the JavaBean syntax in the JSP:
1. Declare JavaBean-Must use the full class name:<jsp:usebean id="Somebean" class="package. Somebean " scope=" page "/>The above statement is equivalent to: somebean Somebean = new Somebean ();p agecontext.setattribute ("Somebean", Somebean), the optional value of scope is: page|request| Session|application need to explain is: In general, we reserve<jsp:usebean/>The body part is empty, and if it is not NULL, it is executed only when the bean is initialized, such as:<jsp:usebean id="Person" class="cn". Person ">This is the body part and will only be executed once at initialization time</Jsp:userbean>2. Access JavaBean Properties:<jsp:setproperty name= "somebean" property ="Name" value ="Tom"/><jsp:getproperty name= "somebean" property ="Name"/ >You can also access its properties and other methods directly on the page using Java code.
<jsp:useBean>
Mark:
<jsp:useBean id="name" class="classname" scope="page|request|session|application"/> id:代表jsp页面中的实例对象 通过这个对象引用类中的成员,如,id="wq", wq.成员(); class: 代表JavaBean类,如: class="com.Test",引用com包中的Test类 scope:指明了javabean的生存时间
Range of JavaBean:
The scope property of the JavaBean determines the range of the bean, which defaults to the page range.
Once a bean is declared and placed in a category, reuse within the same scope <jsp:useBean../>
is no longer created. Instead, you return an instance of the bean that you have already created. This process can be learned by looking at the source code of the container translation.
Jsp:usebean usage when demonstrating that the property name is a basic data type: User.java:
PackageCn.hncu.regServletPojo.domain; Public class User { PrivateString name;PrivateInteger age; PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicIntegerGetage() {returnAge } Public void Setage(Integer Age) { This. Age = Age; }@Override PublicStringtoString() {return "User [name=]+ name +", age="+ Age +"]"; }}
JAVABEAN.JSP:
PackageCn.hncu.regServletPojo.domain; Public class User { PrivateString name;PrivateInteger age; PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicIntegerGetage() {returnAge } Public void Setage(Integer Age) { This. Age = Age; }@Override PublicStringtoString() {return "User [name=]+ name +", age="+ Age +"]"; }}
DOBEANFORM.JSP:
<%@ page language="java" import="java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" ><html> <head> </head> <body> <% request. setcharacterencoding ("Utf-8"); %> <jsp:usebean id= "user3" class=" Cn.hncu.regServletPojo.domain.User "></Jsp:usebean> <!--The following sentence is equivalent to User3.setname (Request.getparameter ("name")); <jsp:setproperty property ="Name" name="User3" param= "name"/> <jsp:setproperty property =' age ' name=' User3 ' param="Age"/>${USER3}<br/><br/> <jsp:usebean id="User4" class="Cn.hncu.regServletPojo.domain.User "></Jsp:usebean> <!--property with the "*" number, is to let the system automatically according to the parameters submitted to find the corresponding property assignment- - <jsp:setproperty property ="*" name="User4"/> ${USER4}<br/><br/> </body></html>
Jsp:usebean usage when demonstrating a special property name versus the Setter-getter method: Person.java:
PackageCn.hncu.bean;ImportJava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Date;/** * @author Chen Haoxiang * * 2016-7-30 * * Public class person { PrivateString name;PrivateInteger age;//Preferably with an integer object instead of an int PrivateDate birth;//Special attributes (the type of this variable is not a basic data type when the,<jsp:setproperty> tag cannot be used directly!) ) //Solution: Do not write its own default Setter-getter--setbirth (Date) //Rather write a pair of setter-getter---setbirthday (String) method that overrides the attribute In addition, when using <jsp:setProperty> on a page, the property name should be reversed with the Setter-getter method-the---birthday //START I want to use overloading to do, in fact, overloading is also possible to achieve this same function, but note oh, the return type can not be overloaded! So the Get method is certainly not, synthesis, or the above method is good. Public Person() {Super(); } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicIntegerGetage() {returnAge } Public void Setage(Integer Age) { This. Age = Age; }/*//all we have to do is change this. 2 Methods public Date Getbirth () {return birth; } public void Setbirth (Date birth) {This.birth = birth; } */ //Injection date formatSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd"); PublicStringGetbirthday(){returnSdf.format (birth); } Public void Setbirthday(String strdate) {Try{Date birth = sdf.parse (strdate); This. Birth=birth; }Catch(ParseException e) {Throw NewRuntimeException ("date format error", e); } }@Override PublicStringtoString() {return "person [name="+ name +", age="+ Age +", birth="+ Birth +"]"; }}
JAVABEAN.JSP: Add code
<br/><br/> <!--The following is a jsp:userbean usage when the property name is compared to the Setter-getter method. <jsp:usebean id="Person" class="Cn.hncu.bean.Person"> </Jsp:usebean> <jsp:setproperty property =' name ' name=' person ' value ="Tom"/> <jsp:setproperty property =' age ' name=' person ' Value="/>" <!--Note that the value of the following sentence is: Birthday!!! - <jsp:setproperty property =' birthday ' name=' person ' value ="2016-7-30"/> <jsp:getproperty property =' name ' name=' person '/> <jsp:getproperty property =' age ' name=' person '/ > <jsp:getproperty property =' birthday ' name=' person '/>
Demo Result:
JSP---javabean use-jsp:usebean tag-related