Using the param tag has already been established in this chapter. In particular, our coverage of the bean tag showed a couple of use cases for the param tag, including as a means for passing parameters into your own custom utility objects. As long as you have the general idea, it’s just a matter of perusing the APIs to see which tags can take parameters. Toward this end, it’s always a good idea to consult the online documentation of the Struts 2 tags to see if a given tag can take a parameter of some sort.
Search from Struts2 Library
Showing posts with label Miscellaneous. Show all posts
Showing posts with label Miscellaneous. Show all posts
Monday, September 22, 2008
Param tag
The last tag we’ll discuss has already been used throughout this chapter. The param tag does nothing by itself, but at the same time it’s one of the more important tags. It not only serves an important role in using many of the tags covered in this chapter, it’ll also play a role in many of the UI component tags, Table below lists the attributes you’re now familiar with.

Using the param tag has already been established in this chapter. In particular, our coverage of the bean tag showed a couple of use cases for the param tag, including as a means for passing parameters into your own custom utility objects. As long as you have the general idea, it’s just a matter of perusing the APIs to see which tags can take parameters. Toward this end, it’s always a good idea to consult the online documentation of the Struts 2 tags to see if a given tag can take a parameter of some sort.
Using the param tag has already been established in this chapter. In particular, our coverage of the bean tag showed a couple of use cases for the param tag, including as a means for passing parameters into your own custom utility objects. As long as you have the general idea, it’s just a matter of perusing the APIs to see which tags can take parameters. Toward this end, it’s always a good idea to consult the online documentation of the Struts 2 tags to see if a given tag can take a parameter of some sort.
Labels:
Data tag,
java,
Miscellaneous,
Param,
struts2
The i18n and text tags
Many applications need to work in multiple languages. The process of making this happen is called internationalization, or i18n for short. (There are 18 letters between the I and the N in the word internationalization.) Chapter 11 discusses Struts 2’s internationalization support in detail, but we’d like to take a moment to detail the two tags that are central to this functionality: the i18n tag and the text tag.
The text tag is used to display language-specific text, such as English or Spanish, based on a key lookup into a set of text resources. This tag retrieves a message value from the ResourceBundles exposed through the framework’s own internationalization mechanisms. For now, we’ll just note the usage of the tag; it takes a name attribute that specifies the key under which the message retrieval should occur. The framework’s default Locale determination will determine the Locale under which the key will be resolved.

You can also name an ad hoc ResourceBundle for resolving your text tags. If you want to manually specify the ResourceBundle that should be used, you can use the i18n tag. Table below lists the attributes of the i18n tag.

Here’s a quick example that shows how to set the ResourceBundle with the i18n tag and then extract a message text from it with the text tag:
<s:i18n name="manning.chapterSix.myResourceBundle_tr">
In <s:text name="language"/>,
<s:text name="girl" var="foreignWord"/>
</s:i18n>
"<s:property value="#foreignWord"/>" means girl.

The i18n tag simply specifies a resource bundle to use. The bundle is only used during the body of the tag. However, as our example demonstrates, you can “persist” a message from the bundle using the text tag’s var attribute to set the message to the ActionContext as a named reference. This usage of the var attribute should be more familiar by now. The first text tag writes the message associated with the key “language” directly to the output. The second text tag stores the message associated with the key “girl” under the reference name “foreignWord”.
These tags are simple, but seem out of context without full knowledge of the uiltininternationalization features of Struts 2. In particular, you’ll need to know how the framework locates, loads, and uses properties file ResourceBundles.
The text tag is used to display language-specific text, such as English or Spanish, based on a key lookup into a set of text resources. This tag retrieves a message value from the ResourceBundles exposed through the framework’s own internationalization mechanisms. For now, we’ll just note the usage of the tag; it takes a name attribute that specifies the key under which the message retrieval should occur. The framework’s default Locale determination will determine the Locale under which the key will be resolved.
You can also name an ad hoc ResourceBundle for resolving your text tags. If you want to manually specify the ResourceBundle that should be used, you can use the i18n tag. Table below lists the attributes of the i18n tag.

Here’s a quick example that shows how to set the ResourceBundle with the i18n tag and then extract a message text from it with the text tag:
<s:i18n name="manning.chapterSix.myResourceBundle_tr">
In <s:text name="language"/>,
<s:text name="girl" var="foreignWord"/>
</s:i18n>
"<s:property value="#foreignWord"/>" means girl.

The i18n tag simply specifies a resource bundle to use. The bundle is only used during the body of the tag. However, as our example demonstrates, you can “persist” a message from the bundle using the text tag’s var attribute to set the message to the ActionContext as a named reference. This usage of the var attribute should be more familiar by now. The first text tag writes the message associated with the key “language” directly to the output. The second text tag stores the message associated with the key “girl” under the reference name “foreignWord”.
These tags are simple, but seem out of context without full knowledge of the uiltininternationalization features of Struts 2. In particular, you’ll need to know how the framework locates, loads, and uses properties file ResourceBundles.
URL Tag
When you’re building web applications, URL management is a central task. Struts 2 provides a URL tag to help you do this. The tag supports everything you might want to do with a URL, from controlling parameters to automatically persisting sessions in the absence of cookies. Table below lists its attributes.

Here are a couple of examples. First we look at a simple case:
URL = <s:url value="IteratorTag.action"/> <a href='<s:url value="IteratorTag.action" />'> Click Me </a>
And here’s the output markup:
URL = IteratorTag.action
The URL tag just outputs the generated URL as a string. First we display it for reference. Then we use the same markup to generate the href attribute of a standard anchor tag. Note that we set the target of the URL with the value attribute. This means we must include the .action extension ourselves. If we want to target an action, we should probably use the action attribute, as seen in the next example:
URL = <s:url action="IteratorTag" var="myUrl"> <s:param name="id" value="2"/> </s:url> <a href='<s:property value="#myUrl" />'> Click Me </a>
Now, let’s see the markup generated by these tags:
URL =
<a href='/manningHelloWorld/chapterSix/IteratorTag.action?id=2'> Click Me </a>
As you can see, the URL tag didn’t generate any output in this example. This happened because we used the var attribute to assign the generated URL string to a reference in the ActionContext. This helps improve the readability of the code. In this example, our URL tag, with its param tags, has become unwieldy to embed directly in the anchor tag. Now we can just pull the URL from the ActionContext with a property tag and some OGNL. This is also useful when we need to put the URL in more than one place on the page.
The param tag used in this example specifies querystring parameters to be added to the generated URL. You can see generated querystring in the output. Note that you can use the includeParams attribute to specify whether parameters from the current request are carried over into the new URL. By default this attribute is set to get, which means only querystring params are carried over. You can also set it to post, which causes the posted form parameters to also be carried over. Or you can specify none.
<a href='IteratorTag.action'> Click Me </a>
Here are a couple of examples. First we look at a simple case:
URL = <s:url value="IteratorTag.action"/> <a href='<s:url value="IteratorTag.action" />'> Click Me </a>
And here’s the output markup:
URL = IteratorTag.action
The URL tag just outputs the generated URL as a string. First we display it for reference. Then we use the same markup to generate the href attribute of a standard anchor tag. Note that we set the target of the URL with the value attribute. This means we must include the .action extension ourselves. If we want to target an action, we should probably use the action attribute, as seen in the next example:
URL = <s:url action="IteratorTag" var="myUrl"> <s:param name="id" value="2"/> </s:url> <a href='<s:property value="#myUrl" />'> Click Me </a>
Now, let’s see the markup generated by these tags:
URL =
<a href='/manningHelloWorld/chapterSix/IteratorTag.action?id=2'> Click Me </a>
As you can see, the URL tag didn’t generate any output in this example. This happened because we used the var attribute to assign the generated URL string to a reference in the ActionContext. This helps improve the readability of the code. In this example, our URL tag, with its param tags, has become unwieldy to embed directly in the anchor tag. Now we can just pull the URL from the ActionContext with a property tag and some OGNL. This is also useful when we need to put the URL in more than one place on the page.
The param tag used in this example specifies querystring parameters to be added to the generated URL. You can see generated querystring in the output. Note that you can use the includeParams attribute to specify whether parameters from the current request are carried over into the new URL. By default this attribute is set to get, which means only querystring params are carried over. You can also set it to post, which causes the posted form parameters to also be carried over. Or you can specify none.
<a href='IteratorTag.action'> Click Me </a>
Labels:
java,
Miscellaneous,
struts2,
tags,
URL
Include Tag
Whereas JSP has its own include tag, <jsp:include>, Struts 2 provides a version that integrates with Struts 2 better and provides more advanced features. In short, this tag allows you to execute a Servlet API–style include. This means you can include the output of another web resource in the currently rendering page. One good thing about the Struts 2 include tag is that it allows you to pass along request parameters to the included resource.
This differs from the previously seen action tag, in that the include tag can reference any servlet resource, while the action tag can include only another Struts 2 action within the same Struts 2 application. This inclusion of an action stays completely within the Struts 2 architecture. The include tag can go outside of the Struts 2 architecture to retrieve any resource available to the web application in which the Struts 2 application is deployed. This generally means grabbing other servlets or JSPs. The include tag may not make a lot of sense unless you’re pretty familiar with the Servlet API. Again, the Servlet Specification is recommended reading: http://java.sun.com/products/servlet/download.html.
Table below lists the sole attribute for the include tag.

We won’t show a specific example of the include tag, as its use is straightforward. When using the include tag, you should keep in mind that you’re including a JSP, servlet, or other web resource directly. The semantics of including another web resource come from the Servlet API. The include tag behaves similarly to the JSP include tag. However, it’s more useful when you’re developing with Struts 2, for two reasons: it integrates better with the framework, and it provides native access to the ValueStack and a more extensible parameter model. What does all of this mean?
Let’s start with the framework integration. For example, your tag can dynamically define the resource to be included by pulling a value from the ValueStack using the %{ ... } notation. (You have to force OGNL evaluation here, as the value attribute is of type String and would normally be interpreted as a string literal.) Similarly, you can pass in querystring parameters to the included page with the <s:param> tag (discussed in a moment). This tag can also pull values from the ValueStack. This tight integration with the framework makes the Struts 2 include tag a powerful choice.
Another advantage to choosing the Struts 2 include tag over the native JSP version is plain-old user-friendliness. For example, it’ll automatically rewrite relative URLs for you. If you want to include the URL ../index.jsp, you’re free to do so even though some application servers don’t support that type of URL when using the JSP include tag. The Struts 2 include tag will rewrite ../index.jsp as an absolute URL based on the current URL where the JSP is located.
This differs from the previously seen action tag, in that the include tag can reference any servlet resource, while the action tag can include only another Struts 2 action within the same Struts 2 application. This inclusion of an action stays completely within the Struts 2 architecture. The include tag can go outside of the Struts 2 architecture to retrieve any resource available to the web application in which the Struts 2 application is deployed. This generally means grabbing other servlets or JSPs. The include tag may not make a lot of sense unless you’re pretty familiar with the Servlet API. Again, the Servlet Specification is recommended reading: http://java.sun.com/products/servlet/download.html.
Table below lists the sole attribute for the include tag.

We won’t show a specific example of the include tag, as its use is straightforward. When using the include tag, you should keep in mind that you’re including a JSP, servlet, or other web resource directly. The semantics of including another web resource come from the Servlet API. The include tag behaves similarly to the JSP include tag. However, it’s more useful when you’re developing with Struts 2, for two reasons: it integrates better with the framework, and it provides native access to the ValueStack and a more extensible parameter model. What does all of this mean?
Let’s start with the framework integration. For example, your tag can dynamically define the resource to be included by pulling a value from the ValueStack using the %{ ... } notation. (You have to force OGNL evaluation here, as the value attribute is of type String and would normally be interpreted as a string literal.) Similarly, you can pass in querystring parameters to the included page with the <s:param> tag (discussed in a moment). This tag can also pull values from the ValueStack. This tight integration with the framework makes the Struts 2 include tag a powerful choice.
Another advantage to choosing the Struts 2 include tag over the native JSP version is plain-old user-friendliness. For example, it’ll automatically rewrite relative URLs for you. If you want to include the URL ../index.jsp, you’re free to do so even though some application servers don’t support that type of URL when using the JSP include tag. The Struts 2 include tag will rewrite ../index.jsp as an absolute URL based on the current URL where the JSP is located.
Labels:
Include,
java,
Miscellaneous,
struts2,
tags
Subscribe to:
Posts (Atom)