Recipe3.16.Adding Request Parameters to a Link


Recipe 3.16. Adding Request Parameters to a Link

Problem

You want to add an arbitrary set of request parameters to a hyperlink or URL created using the Struts html:link or html:rewrite tags.

Solution

Create a java.util.HashMap as a page-context bean using the jsp:useBean tag. Populate the map using the JSTL c:set tag. Then reference the created map using the name attribute of the html:link and html:rewrite tags:

<%@ taglib prefix="c"    uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %> <!-- header stuff here --> <jsp:useBean  /> <c:set target="${params}" property="user" value="${user.username}"/> <c:set target="${params}" property="product" value="${product.productId}"/> <html:link action="/BuyProduct" name="params">Buy Product Link</html:link> <a href="javascript:window.open(          <html:rewrite action='/BuyProduct' name='params'/>)">     Buy Product Popup </a>

Discussion

The Struts html:link and html:rewrite tags allow you to add name/value parameters to the generated URL by referring to a Map property of a JavaBean. The map key is the parameter name and the map value is the parameter value.

While this approach is functional, it smells like a hack. You are forced to create Java code to represent something used only used to create an HTML link. Granted, the data comes from your business objectswhich is the Modelbut the parameter names are purely part of the View. A more practical problem with this approach is that the values for the parameters may come from different objects. Forcing developers to cobble together a class on the serverside to render a link reeks of a bad technique.

The Solution avoids this problem by utilizing the JSP, Struts, and JSTL tag libraries in concert. The jsp:useBean tag creates a java.util.HashMap as a page-scoped variable, and the JSTL c:set tag populates the map with name/value pairs. The property attribute is the map key and the value is the map value. The JSTL c:set tag provides an added benefit so you have the full power of the JSTL expression language to retrieve the value.

You can avoid the Struts tags altogether and use the JSTL c:url tag to create the URL:

<c:url="/BuyProduct.do" var="buyLink">   <c:param name="user" value="${user.username}"/>   <c:param name="product" value="${product.productId}"/> </c:url> <a href='<c:out value="${buyLink}"/>'> Buy Product Link </a> <a href="javascript:window.open('<c:out value="${buyLink}"/>')">     Buy Product Popup </a>

Nesting the c:param tags within the c:url feels more natural than the HashMap approach supported by Struts. This approach, however, has a major disadvantage compared with the Struts tags: You cannot refer to your Struts actions or global forwards defined in your struts-config.xml. The Struts tags allow you to render the links with a transaction token. You surrender these features when you don't use the Struts html:link and html:rewrite tags.

See Also

If you are unfamiliar with JSTL, refer to Recipe 3.1. It has all the details you need to get started using these powerful tags. Another important source is the Struts documentation for the html tag library, which can be found at http://struts.apache.org/userGuide/dev_html.html. The Struts transaction token handling facility is discussed in Recipe 7-9.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

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