Skip to Main Content
Jakarta Struts Cookbook
book

Jakarta Struts Cookbook

by Bill Siggelkow
February 2005
Intermediate to advanced content levelIntermediate to advanced
528 pages
12h 53m
English
O'Reilly Media, Inc.
Content preview from Jakarta Struts Cookbook

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Jakarta Struts

Programming Jakarta Struts

Chuck Cavaness
Beginning Spring Framework 2

Beginning Spring Framework 2

Bruce Snyder, Sing Li, Anne Horton, Thomas Van de Velde, Naveen Balani, Christian Dupuis
Java Cookbook

Java Cookbook

Ian F. Darwin
Struts 2 in Action

Struts 2 in Action

J. Scott Stanlick, Chad Michael Davis, Donald J. Brown

Publisher Resources

ISBN: 059600771XSupplemental ContentErrata Page