File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes document.form.action problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "document.form.action problem" Watch "document.form.action problem" New topic
Author

document.form.action problem

Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Hi,

I have a text box. I want to call an action class once i enter some value inside the text box and move my mouse out of the text box.

When i try to run the code, i get an error message saying /challan.do is not available.

Entry in struts-config:

<action path="/challan" type="ActionClass.ViewChallanAction" parameter="method" name = "ViewChallanForm" >
<forward name="success" path="/pages/viewchallan.jsp"></forward>
<forward name="next" path="/pages/challanentryB2B.jsp"></forward>
</action>


i tried the following, but failed:

<td >
<html:text name = "Products" property="nos_BL" size="7" onmouseout="calcBBSeries();" ></html:text>
</td>

function calcBBSeries()
{
document.forms[0].action = "/challan.do?method=calcBBSeries";
document.forms[0].submit();
}

Am i doing anything wrong?
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Not a 100% on this but when you say document.forms[0].action = "/challan.do?method=calcBBSeries";
the web browser consider the link to be relative to the root of the domain name.
Hence if your link is something like, http://MyIP/Application/currentPage.do
then instead of hitting the link http://MyIP/Application/challan.do it hits http://MyIP/challan.do which might result in a Page not found.

Can you post the URL of the page that shows up in the browser when the page submits?

Alternately you can try removing the "/" from document.forms[0].action = "/challan.do?method=calcBBSeries"; and see if that helps!


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:Not a 100% on this but when you say document.forms[0].action = "/challan.do?method=calcBBSeries";
the web browser consider the link to be relative to the root of the domain name.
Hence if your link is something like, http://MyIP/Application/currentPage.do
then instead of hitting the link http://MyIP/Application/challan.do it hits http://MyIP/challan.do which might result in a Page not found.

Can you post the URL of the page that shows up in the browser when the page submits?

Alternately you can try removing the "/" from document.forms[0].action = "/challan.do?method=calcBBSeries"; and see if that helps!


Hey, I tried removing '/' from action ,but didnt work.

I also tried the first option, i.e. i gave the entire url :http://localhost:8080/struts-blank/challan.do?method=calcBBSeries and it worked.

But can i get the url through some method, since if in future i change my application folder, i will have to change the url mechanically.

Thanks for your timely help. I was going mad searching for a solution
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Ok Let me take a point blank guess here. Are you displaying a JSP/HTML page directly?

What I mean is, is your URL of the page from where you are calling the document.forms[0].submit(); something like this --> http://localhost:8080/struts-blank/jsp/index.jsp or something like this http://localhost:8080/struts-blank/home.do

Posting the URL of the page from which you are making the call would help.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:Ok Let me take a point blank guess here. Are you displaying a JSP/HTML page directly?

What I mean is, is your URL of the page from where you are calling the document.forms[0].submit(); something like this --> http://localhost:8080/struts-blank/jsp/index.jsp or something like this http://localhost:8080/struts-blank/home.do

Posting the URL of the page from which you are making the call would help.


Hi,
I am not directly calling the jsp page. I am calling an action class.

URL : document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries";

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You should probably create the link using the Struts link tag, giving it the action you're submitting to.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
David Newton wrote:You should probably create the link using the Struts link tag, giving it the action you're submitting to.


Hi,
I am calling that action from onchange of my <html:text>. I dont want it to appear as a link. Hence have not used <html:link>.


Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

What is the link of the page where this form is displayed.

I know that the link of the page that you want to redirect to is http://localhost:8080/struts-blank/challan.do?method=calcBBSeries? My question is what is the link of the page from which this action is happening?
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:What is the link of the page where this form is displayed.

I know that the link of the page that you want to redirect to is http://localhost:8080/struts-blank/challan.do?method=calcBBSeries? My question is what is the link of the page from which this action is happening?


Hi,
Sorry i couldn't get your question earlier.
Well the link from where i am calling the action is:

http://localhost:8080/struts-blank/challan.do?method=getProducts

Thanks
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

The construct

document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries"
or
document.forms[0].action = "challan.do?method=calcBBSeries" (without the front slash)

are the same from the way your application is structured. So both should work in the same manner.


The problem here is that you cannot pass a parameter using a query string when you do a document.forms[0].action which basically means that the parameter you pass 'method' would be lost on both cases. You will need to use hidden text to pass the parameter method to your 'challan.do' page.

I am surprised that the construct "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries worked for you. It should work exactly in a similar manner to what happens when you do "challan.do?method=calcBBSeries". You could tell what is happening by changing the form[0] method to a 'Get' and looking at the URL in the web browser when the onchange event happens.

@Bear - This is a Java Script problem - I think it was in the correct forum initially - Just me thoughts.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:
. You could tell what is happening by changing the form[0] method to a 'Get' and looking at the URL in the web browser when the onchange event happens.

.


Hey,
I tried changing the form method to 'GET'. But i get an error saying:
The request sent by the client was syntactically incorrect (Request[/challan] does not contain handler parameter named method).

URL: http://localhost:8080/struts-blank/challan.do?nos_BL=12&BL=Select
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Deeps Mistry wrote:
Hey,
I tried changing the form method to 'GET'. But i get an error saying:
The request sent by the client was syntactically incorrect (Request[/challan] does not contain handler parameter named method).

URL: http://localhost:8080/struts-blank/challan.do?nos_BL=12&BL=Select


Exactly as you can see from the URL being send there is no parameter called 'method'. There are just two parameters being passed namely nos_BL and BL.

To pass the parameter method as part of the form you will need to use a hidden variable <input type='hidden' name='method'> in the form[0] & set its value to 'calcBBSeries' using JavaScript just before the step where you do document.forms[0].action = "/challan.do?method=calcBBSeries";

Hope this makes sense ...

P.S : Is the code working when you give the full URL i.e. http://localhost:8080/struts-blank/challan.do?method=calcBBSeries and method is POST?
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:

To pass the parameter method as part of the form you will need to use a hidden variable <input type='hidden' name='method'> in the form[0] & set its value to 'calcBBSeries' using JavaScript just before the step where you do document.forms[0].action = "/challan.do?method=calcBBSeries";



P.S : Is the code working when you give the full URL i.e. http://localhost:8080/struts-blank/challan.do?method=calcBBSeries and method is POST?


Hi,
Yes the code is working fine with full URL and method = "post".

I tried what you said:
<html:hidden name="ViewChallanForm" property="method" value=""/>

function calcBBSeries(product)
{

document.forms[0].method.value = calcBBSeries;
document.forms[0].action = "/challan.do?method=calcBBSeries";
//document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries";

document.forms[0].submit();
}

But its giving me the previous error: The requested resource (/challan.do) is not available.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

The code is correct except for the URL which should be,
document.forms[0].action = "challan.do"; i.e the URL without the '/'

In case it does not work, you could repeat the previous step to change the 'Post' to 'Get' and see the URL thats being called. The URL should be http://localhost:8080/struts-blank/challan.do?nos_BL=12&BL=Select&method=calcBBSeries. If its not post the URL that you are getting.

Pretty long thread eh
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:The code is correct except for the URL which should be,
document.forms[0].action = "challan.do"; i.e the URL without the '/'

In case it does not work, you could repeat the previous step to change the 'Post' to 'Get' and see the URL thats being called. The URL should be http://localhost:8080/struts-blank/challan.do?nos_BL=12&BL=Select&method=calcBBSeries. If its not post the URL that you are getting.

Pretty long thread eh


URL when method = post : http://localhost:8080/struts-blank/pages/challan.do?method=calcBBSeries
when method = get : http://localhost:8080/struts-blank/pages/challan.do?method=function+calcBBSeries%28product%29%0D%0A%7B%0D%0A%09%2F%2Fvar+product+%3D+document.forms%5B0%5D.products%3B%0D%0A%09alert%28product%29%3B%0D%0A%09alert%28document.forms%5B0%5D.method.value%29%3B%0D%0A%09document.forms%5B0%5D.method.value+%3D+calcBBSeries%3B%0D%0A%09alert%28document.forms%5B0%5D.method.value%29%3B%0D%0A%09document.forms%5B0%5D.action+%3D+%22challan.do%3Fmethod%3DcalcBBSeries%22%3B%0D%0A%09%2F%2Fdocument.forms%5B0%5D.action+%3D+%22http%3A%2F%2Flocalhost%3A8080%2Fstruts-blank%2Fchallan.do%3Fmethod%3DcalcBBSeries%22%3B%0D%0A%09%2F%2Fdocument.getElementById%28%27form_1%27%29.action%3D%22%2Fchallan.do%3Fmethod%3DcalcBBSeries%22%3B%0D%0A%09document.forms%5B0%5D.submit%28%29%3B%0D%0A%7D&nos_BL=23&BL=Select

pretty long url javascript:emoticon('');
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

document.forms[0].method.value = calcBBSeries;

You need to put calcBBSeries in double quotes, it should read
document.forms[0].method.value = "calcBBSeries";

What you are currently doing is assigning document.forms[0].method the value of the variable calcBBSeries which incidentally is the name function ... and that's what you get in your url...Once you put "calcBBSeries" in double quotes you'll see all your problems solved! (Hopefully )

Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:document.forms[0].method.value = calcBBSeries;

You need to put calcBBSeries in double quotes, it should read
document.forms[0].method.value = "calcBBSeries";

What you are currently doing is assigning document.forms[0].method the value of the variable calcBBSeries which incidentally is the name function ... and that's what you get in your url...Once you put "calcBBSeries" in double quotes you'll see all your problems solved! (Hopefully )



No not yet solved
By doing document.forms[0].method.value = "calcBBSeries"; what are we achieving? We are not setting it in the action.
What we are doing is :document.forms[0].action = "challan.do?method=calcBBSeries";

Totally confused
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Ok let me explain.

document.forms[0].action = "challan.do?method=calcBBSeries statement is actually incorrect. A form submit action should not contain a parameter to be passed to the server, basically it should not contain a query string.

That's the reason when you use a Get the parameters are overwritten and you don't see it getting passed as part of the query string.
When you use a post the form in incorrectly getting submitted with query string parameters. A Post request should not have any query string parameters. This is the reason why it is working with a POST and not a GET. However this is still incorrect and may break on a different browser.

By setting a form variable like document.forms[0].method.value = "calcBBSeries"; what happens is when the form is submitted all the form variable are either submitted to the server as
1. Post - Inside the body of the request
2. Get - As a query string.
Hence the value of 'method' becomes available to the server in the form Bean.

As always the only ways we can know whats going wrong is if you post more code or give the Get and Post URL as in the previous posts...

Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:Ok let me explain.

document.forms[0].action = "challan.do?method=calcBBSeries statement is actually incorrect. A form submit action should not contain a parameter to be passed to the server, basically it should not contain a query string.

That's the reason when you use a Get the parameters are overwritten and you don't see it getting passed as part of the query string.
When you use a post the form in incorrectly getting submitted with query string parameters. A Post request should not have any query string parameters. This is the reason why it is working with a POST and not a GET. However this is still incorrect and may break on a different browser.

By setting a form variable like document.forms[0].method.value = "calcBBSeries"; what happens is when the form is submitted all the form variable are either submitted to the server as
1. Post - Inside the body of the request
2. Get - As a query string.
Hence the value of 'method' becomes available to the server in the form Bean.

As always the only ways we can know whats going wrong is if you post more code or give the Get and Post URL as in the previous posts...



I am getting the same URL which i posted earlier.

Sam, cant we take the path from request(may be from some properties file) and append it to the action. So changes done at one place would be reflected at all other places. wat say?


Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Its pretty strange that when you put quotes around "calcBBSeries" you still get the old URL ->
http://localhost:8080/struts-blank/pages/challan.do?method=function+calcBB...D&nos_BL=23&BL=Select
That's pretty weird!

Yes you could put the URL in a properties file and get it to be read from there. And yes that might solve your problem.

But it would still not be the correct way of doing it - A request having both Get and Post parameters is really strange! But I'm really out of answers at the moment, maybe somebody could spot what I am missing out on



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I meant use the rewrite tag (I didn't mean the actual *link* tag) to generate the URL so you just specify the action.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
David Newton wrote:I meant use the rewrite tag (I didn't mean the actual *link* tag) to generate the URL so you just specify the action.


Could you please explain it in simple words or with an example?

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: document.form.action problem
 
Similar Threads
Struts Validation Empty alert Box
Action class being called only once in ajax
pass <bean:write> to a function
pass a variable in the URl
SEVERE: No action instance for path /xxxx ould be created java.lang.ClassCastExcepti