• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null value in ActionForm after javascript submit()

 
Miki Italiano
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use <html:hidden/> instead of <input type=hidden> then it should work
Olli
 
Miki Italiano
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic