user6332430
user6332430

Reputation: 460

Struts2 pass url as a string

I'm using javascript function to submit form, and in the javascript function, I'd specify form.action= "Struts2 url goes here";

Here's a snippet of my code:

var form = document.forms['myForm'];
if (form != null) {
    var backURL = "ActionB!someMethodB.action?Bparam1=somevalue&Bparam2=somevalue";
    form.action="ActionA!someMethodA.action?Aparam1=somevalue&Aparam2=" + backURL;
    form.submit();
}

The problem is that in the action method someMethodA, the value for Aparam2 is always cut off by the first ampersand in backURL.

I tried to enclose backURL with quotes like this form.action="ActionA!someMethodA.action?Aparam1=somevalue&Aparam2='" + backURL + "'"; but it did not work. It kind of makes me feel like that the value of backURL is not treated as a whole but parsed as well.

I would like to know if there's a way to work around this.

Upvotes: 1

Views: 287

Answers (1)

Roman C
Roman C

Reputation: 1

If you want to use a parameter in the url which contains special characters they should be urlencoded.

var backURL = encodeURIComponent("ActionB!someMethodB.action?Bparam1=somevalue&Bparam2=somevalue");

Also, a hardcoded value for URL could be built on server with s:url tag.

var backURL = encodeURIComponent('<s:url action="ActionB" method="someMethodB"><s:param name="Bparam1" value="somevalue"/><s:param name="Bparam2" value="somevalue"/></s:url>'); 

In this case by default & is escaped to &amp; but escaped value is used normally with the browser.

Upvotes: 1

Related Questions