This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Null value in ActionForm after javascript submit() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Null value in ActionForm after javascript submit()" Watch "Null value in ActionForm after javascript submit()" New topic
Author

Null value in ActionForm after javascript submit()

Miki Italiano
Greenhorn

Joined: Mar 07, 2004
Posts: 7
Good day all.
In my jsp page (using Tiles), I have the following javascript snippet:
<head>
<title>
My brain on struts <picture of the air goes here>
</title>
<script type="text/javascript">
<!--
function pageSubmit(newStartIndex) {
document.forms[0].startIndex.value = newStartIndex
alert(document.forms[0].startIndex.value);
document.forms[0].submit();
}
//-->
</script>
</head>
In the body of the document, I have this:
<html:form
action="/Search"
name="searchForm"
type="com.funwithstruts.archives.formbean.SearchForm">
...
<input type="hidden" name="startIndex"
value="<c ut value='${startIndex}'/>"/>
<c:forEach var="pageNbr" begin="${1}"
end="${(total+subsetTotal)/subsetTotal}" step="${1}">
<c:set var="newStartIndex" scope="page">
<c ut value="${(subsetTotal*(pageNbr-1)) + 1}"/>
</c:set>
<c:set var="pageSubmitJS" scope="page">
javascript ageSubmit(<c ut value="${newStartIndex}"/> ;
</c:set>
<a href="/archives/Search.do"
oncluck="<c ut value='${pageSubmitJS}'/>">
Page <c ut value="${pageNbr}"/>
</a>
<br/>
</c:forEach>
This generates the following links:
<a href="/archives/Search.do" oncluck="javascript ageSubmit(1);">
Page 1
</a>
<br/>
<a href="/archives/Search.do" oncluck="javascript ageSubmit(16);">
Page 2
</a>
Here's the problem. When I click on the link, I see the alert box with the expected value (see javascript above). When I System.out.println() the value of startIndex after retrieving it from the action form in the action class, I see the word "null" instead of what I saw in the alert box. For example, the alert box shows 16, and the action class prints to STDOUT the value "null". (i typed oncluck on purpose so javaranch would post this).
Heres the code in the action class:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
System.out.println("entering SearchAction.execute()");
// retrieve form object
SearchForm theForm = (SearchForm)form;
// get form attributes
String startIndexString = theForm.getStartIndex();
System.out.println("startIndexString=" + startIndexString);
// just printed "null" here to STDOUT
Any ideas?
Thanks
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
use <html:hidden/> instead of <input type=hidden> then it should work
Olli
Miki Italiano
Greenhorn

Joined: Mar 07, 2004
Posts: 7
Using html:hidden didn't do the trick. Struts will auto-pop into the ActionForm any/all form input tags <input.../> (seems to do this regardless of whether or not the input tags were created with Struts custom tags or typed in directly). I put in more debug statements both in the Action and ActionForm classes and discovered that the html form was being submitted twice. The second submit was "resetting" the form attributes. I figured out by trial and error and going though the struts mailing list that by adding the following "return true" and "return false" lines in the javascript, the form gets submitted only once and the form bean state is where it should be in the Action class processing stage.
<script language="text/javascript">
function pageSubmit(newStartIndex) {
document.form[0].submit();
return true;
}
</script>
<html:form...
<html:link ... oncluck="javascript ageSubmit(16);return false;"...
</html:form>
Thanks for your time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null value in ActionForm after javascript submit()
 
Similar Threads
Dynamic population of second list box based on selection of first
javascript error: object expected
JSTL and c:if problem
Retrieving radio selection & checkbox selection from a DynaForm
How to do database pagination using JSTL tags