Reusing JavaBeans in JSP Table> center> Message: i > H1> body> html> SimpleBean.java beantest page is used in SimpleBean.
SimpleBean code is as follows: package hall; public class SimpleBean {private String message = "No message specified"; public String getMessage () {return (message);} public void setMessage (String message) {this.message = message;}} 13.3 About JSP: Further Description of UseBean Use the easiest way to use Bean to load bean: then by JSP: SetProperty and JSP: GetProperty Modify and extract the properties of the bean. However, there must be two points. First, we can also instantiate bean: body > body jsp: usebean>, only when the first instantiation bean is performed, if It is not to execute the Body section using the existing Bean instance. As will be introduced below, JSP: UseBean does not always mean create a new bean instance. Second, in addition to ID and CLASS, JSP: USEBean has three other properties, namely: scope, type, beanname. The following table briefly explains the usage of these attributes. Attribute Usage ID Names the variable that references the bean. If you can find the identical bean instance of ID and Scope, JSP: UseBean action will use an existing bean instance instead of creating a new instance. Class Specifies the full package name of the bean. Scope Specifies which of which is available in the context, one of the four values below: Page, Request, Session, and Application. The default value is Page, indicating that the bean is only available within the current page (saved within the current page). Request means that the bean is valid within the current customer request (saved within the servletRequest object). Session indicates that the bean is valid for all pages in the current HTTPSession. Finally, if application is valued, the bean is valid for all pages with the same servletContext. Scope is important because JSP: UseBean instantizes new objects only when there is no object with the same ID and Scope; if the ID and Scope are the same object, use existing objects directly, this Anything between the JSP: USEBean starts tag and end tags will be ignored. TYPE specifies the type of variable that references the object, it must be the name of the bean class, a super class name, one of the interface names implemented by this class. Remember that the name of the variable is specified by the id attribute. BeanName Specifies the name of the bean. If the Type property and the beanName property are provided, the Class property is allowed to omit. 13.4 JSP: SETPROPERTY Action JSP: SetProperty is used to set the properties of the instantiated Bean object, there are two usage.
First, you can use JSP: setProperty outside the JSP: UseBean element, as shown below: ... At this time, regardless of the JSP: UseBean finds an existing bean or a new BEAN instance, JSP: setProperty will execute. The second usage is to put JSP: setProperty into the JSP: UseBean element, as shown below: ... jsp: usebean> This JSP: SetProperty will only be executed when a new bean instance is created, and JSP: setProperty is not performed if you use an existing instance. JSP: setProperty action has the following four properties: Property Description Name Name property is required. It means which bean is to set the property. The Property property is required. It indicates which property to set. There is a special usage: if the value of the Property is "*", the request parameters that match all the names and bean property names will be passed to the corresponding attribute set method. The Value Value property is optional. This property is used to specify the value of the bean property. String data automatically converts into numbers through standard Valueof methods in the target class, Boolean, Boolean, Byte, Byte, Char, Character. For example, the property value of the Boolean and Boolean types (such as "True") converted through the attribute value of the Boolean.ValueOf conversion, the INT, and Integer types (such as "42") via Integer.Valueof. Value and Param cannot be used at the same time, but can be used any one. Param Param is optional. It specifies which request parameter as the value of the bean property. If there is no parameters of the current request, what is not done, the system does not pass NULL to the set method of the bean property. Therefore, you can let the bean provide the default attribute value, and only modify the default attribute value when the request parameter is clearly specified. For example, the following code segment indicates that if there is a NumItems request parameter, set the value of the NumberOfItems property to the value of the request parameter NumItems; otherwise, nothing. If Value and Param are omitted, its effect is equivalent to providing a param and its value is equal to the value of Property. Further, this kind of thought of automatic assignment of automatic assignment by using the request parameter and attribute name, you can also specify "*" in the Property (name of the bean property), then omit value and param. At this point, the server will view all bean properties and request parameters, if the names are the same, automatically assign.
Below is an example of using JavaBean calculations. If there is a Numdigits parameter in the request, the value is passed to the Numdigits property of the bean; Numprimes is similar. JSPprimes.jsp Using JavaBean Title> head> head> < Center> Using JAVABean Table> center> Some Digit Primes: body> html> Note: NumberedPrimes code is slightly. 13.5 JSP: GetProperty action JSP: getProperty action extracts the value of the specified bean property, converts into a string, and then output. JSP: getProperty has two essential properties, namely: name, represents the name of the bean; Property, indicates which attribute to extract. Here is an example, more examples can be found in the forebel. ... | |