You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Ruth, Brice" <br...@fiskars.com> on 2004/08/02 20:13:17 UTC

Re: taglibs: how can i rewrite an action uri without rendering a html link tag?

hallelujah! Now I just need to migrate ... ugh

Erik Weber wrote:

> If I'm not mistaken, the "action" attribute to html:rewrite is 
> supported in 1.2.
>
> Erik
>
>
>
> Ruth, Brice wrote:
>
>> alu, artifex wrote:
>>
>>> i need to rewrite an action uri for use in a javascript function, 
>>> something like this:
>>> ----------
>>> function foo()
>>> {
>>>    document.location.href=<html:link action="/foo/bar" 
>>> paramId="blah" paramName="blah"/>;
>>> }
>>> ----------
>>> however the <html:link> renders html code which i can't use in my 
>>> javascript function.
>>> the <html:rewrite> tag doesnt provide support for the "action" 
>>> attribute (which, imho, renders
>>> the "transaction" attribute useless). do i have to define an 
>>> actionforward for the action (seems like
>>> rewrite can render an actionforward) or is there a better approach? 
>>> my application depends
>>> heavily on javascript operations like this so i have to define an 
>>> actionforward for nearly every
>>> single action.
>>>
>>> thanks in advance,
>>> art
>>>
>> I'm not entirely sure why the Struts team left out the 'action' 
>> attribute for the html:rewrite tag, but what we've done in the 
>> www.fiskars.com site is simply use the page attribute, appending the 
>> '.do' manually. Certainly not as clean, and it breaks best practice, 
>> but it works ... that's how we've gotten URLs into javascript.
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

-- 
Brice Ruth, Sr. IT Analyst
Fiskars Brands Inc
http://www.fiskarsbrands.com/


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: taglibs: how can i rewrite an action uri without rendering a html link tag?

Posted by Kris Schneider <kr...@dotech.com>.
Or, add it yourself:

package com.dotech.servlet.taglibs;

import java.net.MalformedURLException;

import java.util.Map;

import javax.servlet.jsp.JspException;

import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

public class RewriteTag extends org.apache.struts.taglib.html.RewriteTag {

    public int doStartTag() throws JspException {

        // Generate the hyperlink URL
        Map params = RequestUtils.computeParameters(pageContext,
                                                    paramId,
                                                    paramName,
                                                    paramProperty,
                                                    paramScope,
                                                    name,
                                                    property,
                                                    scope,
                                                    transaction);
             
        String url = null;
        try {
            // Note that we're encoding the & character to &amp; in XHTML mode
only, 
            // otherwise the & is written as is to work in javascripts. 
            url = RequestUtils.computeURL(pageContext,
                                          forward,
                                          href,
                                          page,
                                          action,  // The Struts 1.1 version
explicitly passes null for action
                                          params,
                                          anchor,
                                          false,
                                          isXhtml());
                    
        } catch (MalformedURLException e) {
            RequestUtils.saveException(pageContext, e);
            throw new JspException(messages.getMessage("rewrite.url",
e.toString()));
        }

        ResponseUtils.write(pageContext, url);

        return (SKIP_BODY);
    }
}

Then, a TLD dumped somewhere under WEB-INF in your app:

<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>dot-html</short-name>
    <uri>http://dotech.com/taglibs/html</uri>

    <tag>
        <name>rewrite</name>
        <tag-class>com.dotech.servlet.taglibs.RewriteTag</tag-class>
    </tag>
</taglib>

And finally use in your JSPs:

<%@ taglib prefix="dot-html" uri="http://dotech.com/taglibs/html" %>
..
<dot-html:rewrite ...>
..

Quoting "Ruth, Brice" <br...@fiskars.com>:

> hallelujah! Now I just need to migrate ... ugh
> 
> Erik Weber wrote:
> 
> > If I'm not mistaken, the "action" attribute to html:rewrite is 
> > supported in 1.2.
> >
> > Erik

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org