Struts Bean Tags


Struts Bean tag library contains tags to access JavaBeans and resource bundles among others. Two frequently used tags are MessageTag (bean:message) and WriteTag (bean:write).

Message Tag and Multiple Resource Bundles

You have already used the message Tag for accessing externalized messages in resource bundles using locale independent keys. In this section, we will go further and investigate the applicability of multiple resource bundles. When the application is small, a single resource bundle suffices. When the application gets larger, the single properties file gets cluttered much like a single struts-config.xml getting cluttered.

It is advisable to have multiple resource bundles based on the message category from the outset. This saves the pain involved in splitting the single bundle into pieces and updating all the resources accessing it.

The first step in using multiple resource bundles is to declare them in the struts-config.xml first. The semantic for declaring multiple resource bundle is as follows :

 <message-resources parameter="mybank.example.DefaultMsgResource"                    null="false"/> <message-resources parameter="mybank.example.AltMsgResource"                    null="false" key="  bundle.alt  " /> <message-resources parameter="mybank.example.ErrorMsgResource"                    null="false" key="  bundle.error  " /> 

The above snippet declares three resource bundles identified by a key. The default resource bundle does not have a key. As the key suggests, the AltMsgResource contains alternate messages and the ErrorMsgResource contains error messages. The message tag accesses the default resource bundle as follows:

 <bean:message key=msg.key /> 

The key specified in the < bean:message > tag is the key used in the properties file to identify the message.

The non-default resource bundles are accessed by specifying the bundle key as declared in struts-config.xml ( key= ‚½bundle.alt ‚½ , key= ‚½bundle.error ‚½ etc.). For instance, a message tag accesses a message in AltMsgResource as follows:

 <bean:message key="msg.key"  bundle="bundle.alt"  /> 

Similarly a errors tag access the messages in the non-default bundle by using the bundle attribute as follows:

 <html:errors  bundle="bundle.error"  /> 

You can also specify alternate bundles to the following tags in HTML Tag library ‚ messages , image , img and option .

Write Tag

Write Tag is another frequently used tag. The usage of this tag is as follows:

 <bean:write name=customer property=firstName /> 

It accesses the bean named customer in the page, request, session and application scopes in that order (unless a scope attribute is specified) and then retrieves the property named firstName and renders it to the current JspWriter. If format attribute is specified, the value is formatted accordingly . The format can be externalized to the resource bundle by using the formatKey attribute instead. Alternate resource bundle can also be specified. This is handy when the display format is locale specific.

Going further, < c:out > and other JSTL formatting tags are preferred over write tag for formatting and output.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net