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 id="params" class="java.util.HashMap"/> <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 objects—which is the Model—but the parameter names are purely part of the View. A more practical problem with this approach ...