RelativePathPrefixHandler and form "action" attributes

View: New views
5 Messages — Rating Filter:   Alert me  

RelativePathPrefixHandler and form "action" attributes

by Brad Fritz-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I am trying to rewrite an HTML form "action" attribute (for a
non-Wicket form) inside a Wicket Panel and could use some help.

While converting a large webapp to Wicket, I created a Panel to wrap a
search form.  The form processing is not handled by Wicket yet.  So in
my SearchPanel.html, I have something like this:

  <wicket:panel>
    <form method="get" action="search.do">
      [..]
    </form>
    <a href="search.do">Advanced Search</a>
  </wicket:panel>

The "Advanced Search" href is automatically prefixed with the correct
number of "../" strings by RelativePathPrefixHandler, but the form
action is not rewritten.

Is adding a new IMarkupFilter (based on RelativePathPrefixHandler) to
handle the "action" attribute rewriting a good option?  Or is there a
better way?

My first attempt was using a wicket:message for the action along with
getRelativePathPrefixToContextRoot() inside SearchPanel.java to set
the message.  That turned out to be more complicated than I
expected...but mostly because I was not able to find a simple hook
into the message lookup from the SearchPanel class to set the message
text.  (Putting the URL in SearchPanel.properties worked fine but did
not allow for dynamically prefixing the action URL based on the
request URL.)

Any ideas on how to best handle this scenario would be much
appreciated.  Thanks!

--Brad


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: RelativePathPrefixHandler and form "action" attributes

by igor.vaynberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

override oncomponenttag() of the form and call super followed by
tag.put("action", "whateverurl");

-igor

On Fri, Jul 18, 2008 at 12:56 PM, Brad Fritz
<brad-wicket@...> wrote:

>
> I am trying to rewrite an HTML form "action" attribute (for a
> non-Wicket form) inside a Wicket Panel and could use some help.
>
> While converting a large webapp to Wicket, I created a Panel to wrap a
> search form.  The form processing is not handled by Wicket yet.  So in
> my SearchPanel.html, I have something like this:
>
>  <wicket:panel>
>    <form method="get" action="search.do">
>      [..]
>    </form>
>    <a href="search.do">Advanced Search</a>
>  </wicket:panel>
>
> The "Advanced Search" href is automatically prefixed with the correct
> number of "../" strings by RelativePathPrefixHandler, but the form
> action is not rewritten.
>
> Is adding a new IMarkupFilter (based on RelativePathPrefixHandler) to
> handle the "action" attribute rewriting a good option?  Or is there a
> better way?
>
> My first attempt was using a wicket:message for the action along with
> getRelativePathPrefixToContextRoot() inside SearchPanel.java to set
> the message.  That turned out to be more complicated than I
> expected...but mostly because I was not able to find a simple hook
> into the message lookup from the SearchPanel class to set the message
> text.  (Putting the URL in SearchPanel.properties worked fine but did
> not allow for dynamically prefixing the action URL based on the
> request URL.)
>
> Any ideas on how to best handle this scenario would be much
> appreciated.  Thanks!
>
> --Brad
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: RelativePathPrefixHandler and form "action" attributes

by Brad Fritz-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Fri, Jul 18, 2008 at 01:14:08PM -0700, Igor Vaynberg wrote:

> override oncomponenttag() of the form and call super followed by
> tag.put("action", "whateverurl");

Thanks for the pointer, Igor!

For the benefit of archive searchers, the onComponentTag() solution
required that I add a wicket:id to my form--even though form
submission is not handled by Wicket.  I also needed to override
onComponentTagBody() to prevent the Form rendering from adding Wicket
hidden fields to my form.

Here is a code snippet to illustrate:

public class SearchPanel extends Panel {

    public SearchPanel(String id) {
        super(id);
        add(new Form("searchForm") {
            protected void onComponentTag(ComponentTag tag) {
                // prefix the form "action" attribute with a relative path prefix
                tag.put(
                    "action",
                    RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot()
                    + tag.getAttributes().getString("action"));
            }

            // override the default to suppress the addition of Wicket hidden form fields
            protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
                renderComponentTagBody(markupStream, openTag);
            }
        });
    }
}


--Brad


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: RelativePathPrefixHandler and form "action" attributes

by Jeremy Thomerson-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Also (just for the archives), you really could override this method on
WebMarkupContainer - it doesn't actually need to be a "form" instance since
you're not actually using any form functionality.  You're simply creating a
container that contains markup, and that you can override attributes of the
HTML tag on.

It works either way, but thought I'd throw that in for you.

--
Jeremy Thomerson
http://www.wickettraining.com

On Fri, Jul 18, 2008 at 4:16 PM, Brad Fritz <brad-wicket@...>
wrote:

>
> On Fri, Jul 18, 2008 at 01:14:08PM -0700, Igor Vaynberg wrote:
>
> > override oncomponenttag() of the form and call super followed by
> > tag.put("action", "whateverurl");
>
> Thanks for the pointer, Igor!
>
> For the benefit of archive searchers, the onComponentTag() solution
> required that I add a wicket:id to my form--even though form
> submission is not handled by Wicket.  I also needed to override
> onComponentTagBody() to prevent the Form rendering from adding Wicket
> hidden fields to my form.
>
> Here is a code snippet to illustrate:
>
> public class SearchPanel extends Panel {
>
>    public SearchPanel(String id) {
>        super(id);
>        add(new Form("searchForm") {
>            protected void onComponentTag(ComponentTag tag) {
>                // prefix the form "action" attribute with a relative path
> prefix
>                tag.put(
>                    "action",
>
>  RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot()
>                    + tag.getAttributes().getString("action"));
>            }
>
>            // override the default to suppress the addition of Wicket
> hidden form fields
>            protected void onComponentTagBody(final MarkupStream
> markupStream, final ComponentTag openTag) {
>                renderComponentTagBody(markupStream, openTag);
>            }
>        });
>     }
> }
>
>
> --Brad
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>

Re: RelativePathPrefixHandler and form "action" attributes

by Brad Fritz-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jeremy,

On Fri, Jul 18, 2008 at 04:35:21PM -0500, Jeremy Thomerson wrote:

> Also (just for the archives), you really could override this method on
> WebMarkupContainer - it doesn't actually need to be a "form" instance since
> you're not actually using any form functionality.  You're simply creating a
> container that contains markup, and that you can override attributes of the
> HTML tag on.

You're right; it's definitely simpler with WebMarkupContainer:


public class SearchPanel extends Panel {

    public SearchPanel(String id) {
        super(id);
        add(new WebMarkupContainer("searchForm") {
            @Override
            protected void onComponentTag(ComponentTag tag) {
                // prefix the form "action" attribute with a relative path prefix, e.g. "../../"
                tag.put(
                    "action",
                    RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot()
                    + tag.getAttributes().getString("action"));
            }
        });
    }
}

The onComponentTagBody() override wasn't very clean so it's nice to
eliminate it.

Thanks for the tip!

--Brad


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

LightInTheBox - Buy quality products at wholesale price