wood burning stoves 2.0*
The moose likes Struts and the fly likes pass value from struts2 form to javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "pass value from struts2 form to javascript" Watch "pass value from struts2 form to javascript" New topic
Author

pass value from struts2 form to javascript

Armin Vetek
Greenhorn

Joined: Aug 21, 2010
Posts: 29
Hi,

I try to implement UI block functionality.

I have a s:form and I call a struts2 action when submit button is clicked. I use javascript to call a struts2 action as ajax. I unblock UI when the action is finished. Blow is a simple example.

I would like to know:
1.how can i pass the field values from form to javascript call of struts2 action
2.is there another way to implement UI block functionality. I would prefer a working example.

Best regards,
Javanus

<sj:head jqueryui="true"/>
<script type="text/javascript" src="jquery.blockUI.js"></script>

<script type="text/javascript">
function funcBlockUI()
{

$.blockUI('<h1><img src="ajax-loader.gif" />Longaction...</h1>');
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
$.unblockUI();
document.write(xmlhttp.responseText);
}
}
//???how to pass value from username textfield to action???
xmlhttp.open("POST","doLongAction",true);
xmlhttp.send();
}
</script>

<s:form action="doLongAction">
<s:textfield label="User" name="username" maxlength="10" />
<s:submit onclick="funcBlockUI()"></s:submit>
</s:form>


http://www.javanus.com/blogs
Johannes Geppert
Ranch Hand

Joined: Jan 12, 2010
Posts: 67
Why do you include the jQuery Head but make AJAX with plain javascript?

Use jQuery for ajax or the <sj:submit ../> button.

when using the <sj:submit ../> button you can use an onBeforeTopics to block the ui
and an onCompleteTopics to unblock it.

http://code.google.com/p/struts2-jquery/wiki/SubmitTag#AJAX_from_submit_with_Highlight_Effects_and_Events

When using the the sj:submit tag the params of your form was submitted like normal
request params. when using plain javascript you must deserialize the form and attach it to you request.



My Java and Webprogramming Blog
Armin Vetek
Greenhorn

Joined: Aug 21, 2010
Posts: 29
Hi,

First I would like to thank you for your help.

I have decided to use AJAX with plain javascript because I almost give up with block UI implementation and I try to find out all possible solutions.

I am using a firefox as a default browser and unfortunately the code below does not work with firefox. Today I try to launch the code with IE and I was suprised to find it worked with IE.

The application starts to work with firefox and chrome if I comment out the <script type="text/javascript" src="jquery.blockUI.js"></script>
It looks like using jquery.blockUI.js breaks down the struts-jquery-plugin using with firefox and chrome. I found out that System.out.println from longAction works also with firefox but the redirection to done.jsp does not work with firefox.

Best regards,
http://www.javanus.com/blogs

My code snippets consist from four parts:
1.index jsp

2.done.jsp is used to display success page


3.LongAction


4.struts.xml

Armin Vetek
Greenhorn

Joined: Aug 21, 2010
Posts: 29
Hi,

I found out that I have used an old jquery.blockUI version.

I have downloaded the latest jquery.blockUI version now but now the automatic redirection to done.jsp after completed action does not work anymore. The action called from index.jsp is finished successfully but the user is note redirected to done.jsp.

The redirection to done.jsp page works if I comment <script type="text/javascript" src="jquery.blockUI.js"></script> but then I loose the blockUI functionallity.

Does anyone know how to solve this problem? Below is my code.

Best regards,
Javanus

1.index.jsp


2.done.jsp


3.struts.xml


4.LongAction.java
Johannes Geppert
Ranch Hand

Joined: Jan 12, 2010
Posts: 67
you have specified a target result in you submit tag.
but a html element with id result is not abailable in your jsp.

change your index.jsp to this and it should work.

Armin Vetek
Greenhorn

Joined: Aug 21, 2010
Posts: 29
Thank you.



It is almost perfect after spending more then 10*n hours (n???)

Done.jsp page is presented but I would not like to include Done.jsp page inside index.jsp.

Test jQuery Block UI
User: Javanus_____
[SUBMIT]

The successful action produces the following layout. Text "Test jQuery Block UI" is from index.jsp and should not be presented. I would like to include blockUI functionality when the user click save button on the entry form. Entry form display only one record and it is used to insert/edit a record. The list of all records should be displayed after the action is successfully completed. The list of all records is displayed in another jsp page.

Test jQuery Block UI
Action Finished
Hello Javanus!

If I do not use blockUI then I got the following content. Done.jsp is not included into index.jsp but it is own page.
Action Finished
Hello javanus!

Best regards,
Javanus
Johannes Geppert
Ranch Hand

Joined: Jan 12, 2010
Posts: 67
if you not want to use ajax then remove the targets attribute from your submit tag.

Johannes
Armin Vetek
Greenhorn

Joined: Aug 21, 2010
Posts: 29


I have modified the code like below. And....It works!!!

<sj:submit onClickTopics="before" value="Submit" />

Danke schön!!!

Best regards,
Javanus

poornimaa ananth
Greenhorn

Joined: Mar 23, 2012
Posts: 3
Hi,

I Am working on struts 2 to be deployed on IBM portal server. Need to use ajax. when a textfield is populated an action has to be invoked. based on the result a dropdown has to be enabled or disabled. I have tried the other samples provided in the thread but with no result. Need help asap. Thanks in advance.
deepika bhardwaj
Greenhorn

Joined: Apr 27, 2012
Posts: 1
I want to add javascript on the file tag in struts 2
when the user upload the image the javascript call
in javascript I want to calculate the %of red color in the image if it is greater then 50 then it will be uploaded
please reply soon thanks in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass value from struts2 form to javascript
 
Similar Threads
unable to do validation for select in struts2
Struts2 & validation [SOLVED]
jQuery UI tabs not loading in the same tab
iterate problem in struts2
problem with blockUI implementation and struts2-jquery-plugin