bean:define tag in Struts with Example .

Consider the following example. Suppose a JSP has one field named “email”. The email field should be editable based on the userid. If the userid is admin, then the text box should be editable otherwise the text box should be read only. This can be done in many ways. One way is to define a scripting variable using <bean:define> based on the value of bean property userid.

<bean:define> is used to define a scripting variable based on  the value of the specified bean property (i.e. used to create a attribute in the type of bean property (Primitive type will be wrapped to corresponding Wrapper class i.e int is wrapped by java.lang.Integer). The name of the attribute or scripting variable is named by the id attribute. The scope of the attribute can be defined by toScope attribute. The value of the scripting variable can be assigned by specifying name or  property attribute or both where name will be  the Form name or any other bean  and property will be the java bean property. The value can also  be assigned by specifying a value attribute which will have string. The sample code for the above example is given below

Example statement for assigning a value to a script variable by specifying value attribute in the scope “request” is

<bean:define id=”uid” value=”admin” type=”java.lang.String” toScope=”request” />

to write/print the  value of uid

<bean:write name=”uid” />





You may also like

Leave a Reply

Be the First to Comment!