Search from Struts2 Library

Monday, September 22, 2008

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.

No comments: