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.15. Generating URLs

Problem

You want to generate a hyperlink URL for use in a JavaScript function, using the same mechanisms available to the Struts html:link tag.

Solution

Use the Struts html:rewrite tag to generate the URL in the function as needed:

<script language="JavaScript">
  function popupHelp(  ) {
    window.open('<html:rewrite forward="showHelp"/>');
  }
</script>

Discussion

The Struts html:link tag can be used to generate hyperlinks that are based on a Struts action (Struts 1.2), a global forward, the name of a JSP page, or any other URL. Sometimes, however, you need to generate a URL for uses other than as the value of the href attribute of an HTML anchor (a) element. The Struts html:rewrite tag addresses this issue.

A common example of this usage occurs when a URL is needed within JavaScript. In the Solution, the html:rewrite tag generates the URL for the JavaScript window.open() function. This function opens a new browser window using the URL—the first argument of the function—as the browser location. Without the html:rewrite tag, you would have to hardcode the real path for the URL. Furthermore, if you needed to use URL rewriting to maintain the session ID, you would have to hardcode this as well. The html:rewrite tag properly handles URL rewriting just like the html:link tag. Like the Struts html:link tag, the html:rewrite tag can refer to a global forward, an action, a context-relative page, or an absolute URL. Exactly one of the following attributes must be specified: ...

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