URL construction using DSP 2

Posted by wsargent Tue, 01 Mar 2005 06:23:00 GMT

I thought that string construction would be easier in JSP for some reason. Apparently I was wrong.

I need to do string construction for URLs. Specifically URLs that I pass to formhandlers. Don’t ask me if there’s a better way to do it that doesn’t involve stitching URLs together… it’s The Standard, and so many form handlers use it it’s more trouble than it’s worth to do something else.

Constructing URLs is not so bad if it’s just a single exit point. But this gets interesting when I have to juggle URLs for a page that has several entrance and exit points. And it becomes downright annoying when I have to maintain a context root, or pass parameters to the page to maintain state. Most of the time I can pass a referrer param through to the page so I can do a callback in JSP:

It’s possible to use DSP tags inside attributes, like this:

<input type="hidden" name='index<dsp:valueof param="index"/>' value='<dsp:valueof param="element"'/>

This is useful, as it means I don’t have to do hamfisted scriptlets to jam parameters together. However, it only works in HTML attributes. Trying the following:

<dsp:input type="hidden" bean="MyFormHandler.cancelURL" value='<dsp:valueof param="referrer"/>?foo=<dsp:valueof param="foo"/>'/>

will fail. Badly. And since it’s a bean thingy, it’s hard to write the raw tags unless you want to get into DARGS.

I usually do the following:

<% String url = request.getParameter("referrer"); %>
<dsp:input type="hidden" bean="MyFormHandler.cancelURL" value='<%= url + "?foo=" + request.getParameter("foo") %>'/>

But I never liked it. I found a less kludgy way to do this today:

<dsp:droplet name="/atg/dynamo/droplet/Format">
  <dsp:param name="format" value="{referrer}?foo={foo}&bar={bar}"/>
   <dsp:param name="foo" param="foo"/>
   <dsp:param name="bar" param="bar"/>
   <dsp:param name="referrer" param="referrer"/>
   <dsp:oparam name="output">

 &lt;dsp:input type="hidden" bean="MyFormHandler.cancelURL" paramvalue="message"/&gt;

</dsp:oparam> </dsp:droplet>

But that’s still not ideal. The ideal solution would be to use JSTL and do the following:

<dspel:input type="hidden" bean="MyFormHandler.cancelURL" value="${param.referrer + '?foo=' + param.foo}"/>

or even better (using c:url as described here):

<c:url var="cancelURL" value="${param.referrer}">
   <c:param name="foo" value="${param.foo}"/>
   <c:param name="bar" value="${param.bar}"/>
</c:url>
<dspel:input type="hidden" bean="MyFormHandler.cancelURL" value="${cancelURL}"/>

The nice thing about using c:url is that it maintains the context path for me. If not, I have to make the referrer include the context path when I’m generating it, or insert

<dsp:param name="contextpath" bean="/OriginatingRequest.contextPath"/>

into the Format droplet.

But JSTL is only good when using straight string parameters. If I start going into beans or anything involving many.levels.of.indirection, then I have to use <dsp:getvalueof> for each variable, and that’s actually more annoying than just using the Format droplet.

The bottom line is that I don’t think there’s a perfect way unless JSTL can support DynamicBeans. Until that point, I’ll use c:url for the simple stuff, and the Format droplet for the complex.

EDIT: Ah-ha. There’s a undocumented attribute on dspel:tomap.

<dspel:tomap recursive="true" bean="Profile"/>
should make JSTL work across multiple levels of indirection. Thanks to Charles Morehead.

Comments

Leave a comment

  1. Sebastiano Pilla about 14 hours later:

    That’s a clever use of the Format droplet, thank you for posting this (it will sure be useful to me sooner or later). Come to think of it, one could write a specialized subclass of atg.droplet.Format to keep the context path in generated URLs.

  2. Dan Brandt 7 days later:

    And if using dsp tag library instead of dspel you can get that attribute like so:

    <dsp:input type=”hidden” bean=”MyFormHandler.cancelURL” value=’<%= pageContext.getAttribute(“cancelURL”) %>’/>

Comments