Thursday, May 1, 2008

Using Struts tags - <logic:iterate>,<logic:equal>,<bean:write>

I was wondering what should i write about now.And suddenly i got this to do.
I have a small change in the jsp which is made using struts framework.In struts, there are many tags which are easy to use and generate output fast.easy to write.

In struts, we have ActionServlet,Action classes and ActionForms.

ActionServlet will transfer the request to ActionServlet with ActionForms containing all the HTML form data.so you can access any data of the of ActionForm from Servlet request.

See the below code.then we will talk regarding it.

<select name="sel1" id="selTestStruts" style="width:90px;">
<option value="0" selected="selected">Please Select
<logic:iterate id="myBean" name="someForm" property="someFormProp1">
<logic:equal value="test" property="someStrPropOfMyBean" name="myBean">
<option value='<bean:write name="myBean" property="someStrPropOfMyBean"/>'><bean:write name="myBean" property="someNamePropOfMyBean"/></option>
</logic:equal>
</logic:iterate>
</select>


here we are trying to fill HTML select box using struts' <logic:iterate>, <logic:equal><bean:write> tags.

<logic:iterate> will iterate on 'someFormProp1' of 'someForm' which is in the http request.it will make 'myBean' from property 'someFormProp1' which is a List of some data.

You can simply write any property from that 'myBean' we made above.But i also shown here how you can put logic to seperate things on some property value so you don't need to put logic in the SP or generate another property for it.

<logic:equal> checks the value of property named 'someStrPropOfMyBean' with the string given in 'value="test"' attribute (means with 'test' here). If the value matches then only it will make new <option> for <select> box.

same thing you can do with not including some specific value match using <logic:notEqual>

<bean:write> tag will simply write property 'someStrPropOfMyBean' of 'myBean' which is a object of some Value Object class from the List property 'someFormProp1' of 'someForm'.

Though this is very simple thing but i write because may be this can be helpful to some newbies.

0 Comments: