You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by "Frank W. Zammetti" <fz...@omnytex.com> on 2005/08/02 16:36:48 UTC

RE: [moved] HTML labels and Struts

Moved to the dev list, I think it's more appropriate there now...

Hmm... interesting... seems like there might be more than one related
issue here.

Trying to tackle just the issue of being able to do in an Action:

mapping.findForward("myForward#myAnchor");

I can't seem to check out from SVN from my current location, so I can't
test any of this... here's what I *think* could be done to allow this
though...

(1) In ActionMapping, change the findForward() method to this:

  public ActionForward findForward(String name) {
    String anchor = "";
    int hashLoc = name.indexOf("#");
    if (name != null && hashLoc != -1) {
      anchor = name.substring(hashLoc, name.length());
      name = name.substring(0, hashLoc);
    }
    ForwardConfig config = findForwardConfig(name);
    if (config == null) {
      config = getModuleConfig().findForwardConfig(name);
    }
    if (config == null) {
      if (log.isWarnEnabled()) {
        log.warn("Unable to find '" + name + "' forward.");
      }
    }
    ActionForward af = null;
    if (config != null) {
      ActionForward af = new ActionForward((ActionForward)config);
      if (name != null}{
        af.setAnchor(anchor);
      }
    }
    return af;
  }

(2) In RequestProcessor, right before the line:

  if (forward.getRedirect()) {

... in the processForwardConfig() method, add:

  uri += ((ActionForward)forward).getAnchor();

(3) In ActionForward, add the following:

  private String anchor;
  public void setAnchor(String anchor) {
    this.anchor = anchor;
  }
  public String getAnchor() {
    return this.anchor;
  }

Quick explanation:

In ActionMapping.findForward(), we check if there is a hash mark in the
requested forward name.  If there is we grab everything to the right of
it, which is the named anchor, and we set the name to everything to the
left of it, which is the forward name.  The forward should then be found
correctly.

Then, we essentially make a clone of the ForwardConfig we found so that we
can call setAnchor() on it without affecting anything outside the current
request (ForwardConfigs are shared and generally immutable after
creation).

Lastly, in RequestProcessor, we tack the anchor on to the URI before
forwarding or redirecting.  If there was no anchor, no harm is done.

Can anyone check me on this?  Does it look reasonable?  If anyone could
even test-compile, that'd be fantastic, otherwise I'll do it when I get
home and have access.  I'll submit the patch tonight unless anyone sees an
insurmountable problem that I've missed.

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com

On Tue, August 2, 2005 8:58 am, Daniel Perry said:
> I don't think it's a bug, as anchors should never be sent to the server,
> (I
> believe the should never be sent in a redirect either).  I remember doing
> some experiments with this.
>
> If you request a page: blah.do#someLabel, #someLabel is never sent in the
> request.
>
> If you forward on the server, then it will still use the anchor from the
> original request (obvious as the browser knows nothing about the forward).
>
> If you redirect on the server, then the browser *may* loose it.
>
> I tried adding an anchor on the server in the middle of a redirect:
>
> Browser requests blah.do, server redirected to other.do#anchor.  I cant
> remember which way round it was, but I think:
> IE makes a subsequent request for other.do#anchor and obviously struts
> grumbles as this isn't a valid url.
> Firefox makes a subsequent request for other.do and uses the anchor as
> expected.
>
> Daniel.
>
>> -----Original Message-----
>> From: Frank W. Zammetti [mailto:fzlists@omnytex.com]
>> Sent: 02 August 2005 13:41
>> To: Struts Users Mailing List
>> Subject: Re: HTML labels and Struts
>>
>>
>> Not sure :)  I personally would consider it a bug :)
>>
>> I made a Wiki entry for this yesterday by the way, so at least there is
>> documentation of it now.  I agree though that it shouldn't be a big
>> change and would be nice to "fix" it (or, alter the feature, depending
>> on what it is!).
>>
>> Want to throw a patch up Laurie?  If not, I think I could squeeze in
>> time to write the 3-5 lines of code it'd likely be :)
>>
>> Frank
>>
>> Laurie Harper wrote:
>> > Ouch, is that considered a feature or a bug? :-) It probably
>> wouldn't be
>> > hard to change Struts to ignore the anchor if such a change were
>> > acceptable.
>> >
>> > L.
>> >
>> > Frank W. Zammetti wrote:
>> >
>> >> This is probably ripe for a Wiki entry :)
>> >>
>> >> As you found out, Struts can't find an Action mapping with an anchor
>> >> added
>> >> to it... it's trying to find, literally, an ActionMapping named
>> >> "someAction.do#someLabel".  You might, I suppose, be able to make
>> that
>> >> literally your mapping path, I've never tried that, but I don't think
>> >> that's what you'd want to do even if it works.
>> >>
>> >> The "typical" solution to this is a little bit of scripting on
>> your page
>> >> like so:
>> >>
>> >> <head>
>> >> <script>
>> >>   function jumpToAnchor() {
>> >>     <% if (request.getAttribute("hash") != null) { %>
>> >>       location.hash = "<%=request.getAttribute("hash")%>";
>> >>     <% } %>
>> >>   }
>> >> </script>
>> >> </head>
>> >> <body onLoad="jumpToAnchor();">
>> >>
>> >> Then, instead of adding the anchor name to the forward you are
>> >> requesting,
>> >> you add an attribute in your Action named "hash" to the request just
>> >> before you return the forward, with a value of the name of the
>> anchor you
>> >> want to jump to.
>> >>
>> >
>> >
>>
>> --
>> Frank W. Zammetti
>> Founder and Chief Software Architect
>> Omnytex Technologies
>> http://www.omnytex.com
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
>

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


Re: [moved] HTML labels and Struts

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
The situation is actually even a little more complicated...

I would have sworn before that you could include a query string when you 
called ActionMapping.findForward(), but you cannot... after the fact I 
remembered that you have to play some games with adding it to the path 
on the returned forward.

So, the code needs to be altered to even allow that in the first place. 
    I haven't yet verified whether you can have an anchor and a query 
string, although I suspect you can.  I'm toying with this stuff right now...

Frank

Frank W. Zammetti wrote:
> Laurie Harper wrote:
> 
>> The only thing that occurs to me is: are anchors guaranteed to be at 
>> the end of the URL? I.e. is this valid: 
>> http://.../myAction.do#anchor?query=...
> 
> 
> That's a good point, I don't know the answer off the top of my head... 
> have to do some looking around...
> 
>> Come to think of it, it may not make any difference; is the 'name' 
>> argument to findForward pre-processed to strip request parameters?
> 
> 
> Nope, wouldn't appear to be... findForward() in ActionMapping calls 
> findForwardConfig() in ActionConfig, and that simply does a get() on the 
> forwards collection (a HashMap).  So, at least that part of it isn't a 
> problem.
> 
> The first question is interesting though... I'd be willing to bet you 
> can have parameters after an anchor (or maybe the other way around, not 
> sure)... lemme see what I can dig up, unless anyone reading this knows 
> for sure?
> 
> Wouldn't be a big hassle to deal with it if it is possible.
> 
> Frank
> 
>> L.
>>
>> Frank W. Zammetti wrote:
>>
>>> Moved to the dev list, I think it's more appropriate there now...
>>>
>>> Hmm... interesting... seems like there might be more than one related
>>> issue here.
>>>
>>> Trying to tackle just the issue of being able to do in an Action:
>>>
>>> mapping.findForward("myForward#myAnchor");
>>>
>>> I can't seem to check out from SVN from my current location, so I can't
>>> test any of this... here's what I *think* could be done to allow this
>>> though...
>>>
>>> (1) In ActionMapping, change the findForward() method to this:
>>>
>>>   public ActionForward findForward(String name) {
>>>     String anchor = "";
>>>     int hashLoc = name.indexOf("#");
>>>     if (name != null && hashLoc != -1) {
>>>       anchor = name.substring(hashLoc, name.length());
>>>       name = name.substring(0, hashLoc);
>>>     }
>>>     ForwardConfig config = findForwardConfig(name);
>>>     if (config == null) {
>>>       config = getModuleConfig().findForwardConfig(name);
>>>     }
>>>     if (config == null) {
>>>       if (log.isWarnEnabled()) {
>>>         log.warn("Unable to find '" + name + "' forward.");
>>>       }
>>>     }
>>>     ActionForward af = null;
>>>     if (config != null) {
>>>       ActionForward af = new ActionForward((ActionForward)config);
>>>       if (name != null}{
>>>         af.setAnchor(anchor);
>>>       }
>>>     }
>>>     return af;
>>>   }
>>>
>>> (2) In RequestProcessor, right before the line:
>>>
>>>   if (forward.getRedirect()) {
>>>
>>> ... in the processForwardConfig() method, add:
>>>
>>>   uri += ((ActionForward)forward).getAnchor();
>>>
>>> (3) In ActionForward, add the following:
>>>
>>>   private String anchor;
>>>   public void setAnchor(String anchor) {
>>>     this.anchor = anchor;
>>>   }
>>>   public String getAnchor() {
>>>     return this.anchor;
>>>   }
>>>
>>> Quick explanation:
>>>
>>> In ActionMapping.findForward(), we check if there is a hash mark in the
>>> requested forward name.  If there is we grab everything to the right of
>>> it, which is the named anchor, and we set the name to everything to the
>>> left of it, which is the forward name.  The forward should then be found
>>> correctly.
>>>
>>> Then, we essentially make a clone of the ForwardConfig we found so 
>>> that we
>>> can call setAnchor() on it without affecting anything outside the 
>>> current
>>> request (ForwardConfigs are shared and generally immutable after
>>> creation).
>>>
>>> Lastly, in RequestProcessor, we tack the anchor on to the URI before
>>> forwarding or redirecting.  If there was no anchor, no harm is done.
>>>
>>> Can anyone check me on this?  Does it look reasonable?  If anyone could
>>> even test-compile, that'd be fantastic, otherwise I'll do it when I get
>>> home and have access.  I'll submit the patch tonight unless anyone 
>>> sees an
>>> insurmountable problem that I've missed.
>>>
>>
>>
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


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


Re: [moved] HTML labels and Struts

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Laurie Harper wrote:
> The only thing that occurs to me is: are anchors guaranteed to be at the 
> end of the URL? I.e. is this valid: http://.../myAction.do#anchor?query=...

That's a good point, I don't know the answer off the top of my head... 
have to do some looking around...

> Come to think of it, it may not make any difference; is the 'name' 
> argument to findForward pre-processed to strip request parameters?

Nope, wouldn't appear to be... findForward() in ActionMapping calls 
findForwardConfig() in ActionConfig, and that simply does a get() on the 
forwards collection (a HashMap).  So, at least that part of it isn't a 
problem.

The first question is interesting though... I'd be willing to bet you 
can have parameters after an anchor (or maybe the other way around, not 
sure)... lemme see what I can dig up, unless anyone reading this knows 
for sure?

Wouldn't be a big hassle to deal with it if it is possible.

Frank

> L.
> 
> Frank W. Zammetti wrote:
> 
>> Moved to the dev list, I think it's more appropriate there now...
>>
>> Hmm... interesting... seems like there might be more than one related
>> issue here.
>>
>> Trying to tackle just the issue of being able to do in an Action:
>>
>> mapping.findForward("myForward#myAnchor");
>>
>> I can't seem to check out from SVN from my current location, so I can't
>> test any of this... here's what I *think* could be done to allow this
>> though...
>>
>> (1) In ActionMapping, change the findForward() method to this:
>>
>>   public ActionForward findForward(String name) {
>>     String anchor = "";
>>     int hashLoc = name.indexOf("#");
>>     if (name != null && hashLoc != -1) {
>>       anchor = name.substring(hashLoc, name.length());
>>       name = name.substring(0, hashLoc);
>>     }
>>     ForwardConfig config = findForwardConfig(name);
>>     if (config == null) {
>>       config = getModuleConfig().findForwardConfig(name);
>>     }
>>     if (config == null) {
>>       if (log.isWarnEnabled()) {
>>         log.warn("Unable to find '" + name + "' forward.");
>>       }
>>     }
>>     ActionForward af = null;
>>     if (config != null) {
>>       ActionForward af = new ActionForward((ActionForward)config);
>>       if (name != null}{
>>         af.setAnchor(anchor);
>>       }
>>     }
>>     return af;
>>   }
>>
>> (2) In RequestProcessor, right before the line:
>>
>>   if (forward.getRedirect()) {
>>
>> ... in the processForwardConfig() method, add:
>>
>>   uri += ((ActionForward)forward).getAnchor();
>>
>> (3) In ActionForward, add the following:
>>
>>   private String anchor;
>>   public void setAnchor(String anchor) {
>>     this.anchor = anchor;
>>   }
>>   public String getAnchor() {
>>     return this.anchor;
>>   }
>>
>> Quick explanation:
>>
>> In ActionMapping.findForward(), we check if there is a hash mark in the
>> requested forward name.  If there is we grab everything to the right of
>> it, which is the named anchor, and we set the name to everything to the
>> left of it, which is the forward name.  The forward should then be found
>> correctly.
>>
>> Then, we essentially make a clone of the ForwardConfig we found so 
>> that we
>> can call setAnchor() on it without affecting anything outside the current
>> request (ForwardConfigs are shared and generally immutable after
>> creation).
>>
>> Lastly, in RequestProcessor, we tack the anchor on to the URI before
>> forwarding or redirecting.  If there was no anchor, no harm is done.
>>
>> Can anyone check me on this?  Does it look reasonable?  If anyone could
>> even test-compile, that'd be fantastic, otherwise I'll do it when I get
>> home and have access.  I'll submit the patch tonight unless anyone 
>> sees an
>> insurmountable problem that I've missed.
>>
> 
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


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


Re: [moved] HTML labels and Struts

Posted by Laurie Harper <la...@holoweb.net>.
The only thing that occurs to me is: are anchors guaranteed to be at the 
end of the URL? I.e. is this valid: http://.../myAction.do#anchor?query=...

Come to think of it, it may not make any difference; is the 'name' argument 
to findForward pre-processed to strip request parameters?

L.

Frank W. Zammetti wrote:

> Moved to the dev list, I think it's more appropriate there now...
> 
> Hmm... interesting... seems like there might be more than one related
> issue here.
> 
> Trying to tackle just the issue of being able to do in an Action:
> 
> mapping.findForward("myForward#myAnchor");
> 
> I can't seem to check out from SVN from my current location, so I can't
> test any of this... here's what I *think* could be done to allow this
> though...
> 
> (1) In ActionMapping, change the findForward() method to this:
> 
>   public ActionForward findForward(String name) {
>     String anchor = "";
>     int hashLoc = name.indexOf("#");
>     if (name != null && hashLoc != -1) {
>       anchor = name.substring(hashLoc, name.length());
>       name = name.substring(0, hashLoc);
>     }
>     ForwardConfig config = findForwardConfig(name);
>     if (config == null) {
>       config = getModuleConfig().findForwardConfig(name);
>     }
>     if (config == null) {
>       if (log.isWarnEnabled()) {
>         log.warn("Unable to find '" + name + "' forward.");
>       }
>     }
>     ActionForward af = null;
>     if (config != null) {
>       ActionForward af = new ActionForward((ActionForward)config);
>       if (name != null}{
>         af.setAnchor(anchor);
>       }
>     }
>     return af;
>   }
> 
> (2) In RequestProcessor, right before the line:
> 
>   if (forward.getRedirect()) {
> 
> ... in the processForwardConfig() method, add:
> 
>   uri += ((ActionForward)forward).getAnchor();
> 
> (3) In ActionForward, add the following:
> 
>   private String anchor;
>   public void setAnchor(String anchor) {
>     this.anchor = anchor;
>   }
>   public String getAnchor() {
>     return this.anchor;
>   }
> 
> Quick explanation:
> 
> In ActionMapping.findForward(), we check if there is a hash mark in the
> requested forward name.  If there is we grab everything to the right of
> it, which is the named anchor, and we set the name to everything to the
> left of it, which is the forward name.  The forward should then be found
> correctly.
> 
> Then, we essentially make a clone of the ForwardConfig we found so that we
> can call setAnchor() on it without affecting anything outside the current
> request (ForwardConfigs are shared and generally immutable after
> creation).
> 
> Lastly, in RequestProcessor, we tack the anchor on to the URI before
> forwarding or redirecting.  If there was no anchor, no harm is done.
> 
> Can anyone check me on this?  Does it look reasonable?  If anyone could
> even test-compile, that'd be fantastic, otherwise I'll do it when I get
> home and have access.  I'll submit the patch tonight unless anyone sees an
> insurmountable problem that I've missed.
> 


-- 
Laurie, Open Source advocate, Java geek and novice blogger:
http://www.holoweb.net/laurie


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