GWT widgets and Struts
This represents one solution that I found when trying to determine how to grab text from a pop-up text field, and use it in a following Action class. Originally, I decided to use a Session variable-but the guilt of the hack was weighing me down, and this is what I found works instead.
Add a hidden input variable on the JSP page with the widget.
Add a method in the Widget code that calls a javascript method on your page. The widget method should pass in whatever value your widget has collected. For example, to pass in the value from a text area the widget should do something like:
if(ta.getText()!=null){
passComment(ta.getText());
}
In this example, the text area (ta) text is used as a parameter to the passComment() method in the widget. This method uses the text as a parameter to the javascript method like this:
public native void passComment(String text)/*-{
$wnd.submitCommentToAction(text);
}-*/;
The final method in the onSuccess is a method to submit the page. The order of the two of these methods in onSuccess is important, if the page is submitted before the variable is set, the Action won't receive it. (imagine that!)
On the JSP page, add both of the methods:
function submitCommentToAction(text){
document.forms[0].comment.value = text;
}
function submitForm (){
document.forms[0].submit();
}
Also add the hidden variable you want to set. In this example it is called "comment"-as referenced in the following line: document.forms[0].comment.value = text;
This submitCommentToAction sets the value of the hidden field using the Hidden object. http://docs.sun.com/source/816-6408-10/hidden.htm#1193138 . Orginally I considered using a document.write() statement-the problem with this approach is that it must reload the page-which causes obvious problems.
Finally, grab the request parameter in the Action, and you should be good to go.
BTW, I didn't say it was a good idea to use pre-Struts 2 with GWT, only that it can be done one way or another...if you have to jam it into a legacy app that is....apparently mixing Struts and GWT is considered bad practice-so caveat empor!
