Original Works are allowed to be reprinted. During reprinting, please mark the article in hyperlink form
Original source, author information, and this statement. Otherwise, legal liability will be held. Http://zhangjunhd.blog.51cto.com/111083/18210jspuse JavaBean
This article describes how to use JavaBean in JSP.
1. javajan component standards① Implement java. Io. serializable interface ② provide a construction method without parameters ③ provide getter () and setter () methods to access its attributes2. Use Javabean in JSPThe JSP Specification defines <JSP: usebean>, <JSP: setproperty>, and <JSP: useproperty> to reference JavaBean and read/set the attribute values of JavaBean. 2.1 <JSP: usebean>
<jsp:useBean id=”name” class=”classname” scope={“page/request/session/application”}/>Execution Process: ① search for the JavaBean instance named name in the specified scope. ② If it is found, a local variable named name and classname is created, and its reference points to the JavaBean. If it is not found, create a JavaBean named name and classname in the scope, and create a local variable pointing to it. 2.2 <JSP: setproperty>
<jsp:setProperty name=”beanName” propertyDetails/>
Among them, ① name = "beanname" is a required attribute. The value is the bean name. Before that, use the ID introduced by JSP: usebean. Example:
<jsp:useBean id=”usersession” scope=”session” class=”com.user.UserSession”><jsp:setProperty name=”usersession” property=”name” value=”Tom”/></jsp:useBean>
② Propertydetails can be used to specify the attribute information in four different ways. [1] property = "*" is a shortcut for setting bean properties. In bean, the attribute name and type must match the parameter name in the request object. If the property value of the request object has a null value, no value is set for the corresponding bean property. Similarly, if an attribute in the bean does not have the corresponding request parameter value, this attribute is not set. With property = "*", bean attributes do not need to be sorted in the order in the HTML form. [2] property = "propertyname" uses a parameter value in the request to specify a property value in the bean. Here, Property specifies the bean property name, and the bean property and request parameter name should be the same. If there is a null value in the request object parameter value, the corresponding bean property will not be set to any value. [3] property = "propertyname" Param = "parametername" this method can be used in different bean attribute names and request parameter names. Param specifies the parameter name in the request. If there is a null value in the request object parameter value, the corresponding bean property will not be set with any value [4] property = "propertyname" value = "propertyvalue" value is an optional attribute, it uses the specified value to set bean attributes. If the parameter value is null, the corresponding property value is not set. You cannot use PARAM and value in a <JSP: setproperty> file. 2.3 <JSP: getproperty> is a supplement to the <JSP: setproperty> operation, which is used to access a bean property.
<jsp:getProperty name=”beanName” propertry=”propertyName”/>
① Name = "beanname" is a required attribute. The value is the bean name. Before that, use the ID introduced by JSP: usebean. Example:
<jsp:useBean id=”usersession” scope=”session” class=”com.user.UserSession”><jsp:getProperty name=”usersession” property=”name” /></jsp:useBean>
② Property = "propertyname" is a required property. The value is the attribute name of the specified bean.
3. Register instance 1 and use property = "*"Userbean. Java
package com.zj.sample;import java.io.Serializable;/** * Create a JavaBean */public class UserBean implements Serializable { private static final long serialVersionUID = 1L; public String userName; public String password; public int age; public UserBean() {} public void setUserName(String name) { this.userName = name; } public void setPassword(String password) { this.password = password; } public void setAge(int age) { this.age = age; } public String getUserName() { return this.userName; } public String getPassword() { return this.password; } public int getAge() { return this.age; }}Reg.html
<HTML> Reg. jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = gb18030" pageencoding = "gb18030" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <JSP: usebean id = "user" Scope = "page" class = "com. ZJ. sample. userbean "/> <JSP: setproperty name = "user" property = "*"/> // use property = "*" <HTML>
4. Register instance 2 and use Param = "parametername"Userbean.java is the same as reg2.html in the previous example.
Reg2.jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = gb18030" pageencoding = "gb18030" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <JSP: usebean id = "user" Scope = "page" class = "com. ZJ. sample. userbean "/> // property =" username "corresponds to the userbean attribute, And Param =" username "corresponds to the form attribute <JSP: setproperty name = "user" property = "username" Param = "username"/> // property = "password" corresponds to the userbean attribute, And Param = "password" corresponds to the form attribute <JSP: setproperty name = "user" property = "password" Param = "password"/> // property = "Age" corresponds to userb EAN attribute. Param = "Age" corresponds to the form attribute. <JSP: setproperty name = "user" property = "Age" Param = "Age"/> <HTML>
This article is from the "sub-blog", please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/18210