JavaBean is actually a java class that follows a specific writing method, but it must have the following characteristics:
1. This java class must have a public no-argument constructor
2. Attributes must be private and classes must be public
3. Private attributes must be exposed to other programs through public type methods, and the naming of methods must also comply with certain naming conventions---getXxx and setXxx methods.
To put it simply, write a Bean (that is, a special Java class) first and compile it. Then when you need to use this class in a jsp page, use the action
to use the previously written Bean. So this Bean can also be called a component in java.
The syntax for accessing JavaBean in JSP:
1、声明JavaBean对像-必须使用完整类名: <jsp:useBean id=“someBean” class=“package.SomeBean” scope=“page”/> 以上声明相当于: SomeBean someBean = new SomeBean(); pageContext.setAttribute(“someBean”,someBean); scope的可选值为:page|request|session|application 需要说明的是:一般情况下,我们保留<jsp:useBean/>的body部分为空,如果不为空,则只有初始化此Bean时执行,如: <jsp:useBean id=“person” class=“cn.Person”> 这是body部分,只会在初始化时执行一次 </jsp:userBean> 2、访问JavaBean的属性: <jsp:setProperty name=“someBean” property=“name” value=“Tom”/> <jsp:getProperty name=“someBean” property=“name”/> 也可以在页面上使用Java代码直接访问它的属性和其他方法。
<jsp:useBean id="mdb" class="ckstudio.db.faq" scope="page"/>
In this sentence, the class attribute indicates which class (i.e. Bean) to use, here It is ckstudio.db.faq, and the id attribute indicates the name of the generated instance.
Here is mdb, and the scope attribute indicates the lifetime of the instance.
Here is page, that is, in the same Valid during the session.
Using javaBean can separate the java processing code from the jsp page to a certain extent.
Example:
<jsp:usebean id="cart" scope="session" class="session.carts" /> <jsp:setproperty name="cart" property="*" /> <jsp:usebean id="checking" scope="session" class="bank.checking" > <jsp:setproperty name="checking" property="balance" value="0.0" /> </jsp:usebean>
Related learning recommendations: java basic tutorial
The above is the detailed content of What does jspuserBean do?. For more information, please follow other related articles on the PHP Chinese website!