File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Null value in ActionForm after javascript submit()" Watch "Null value in ActionForm after javascript submit()" New topic

Null value in ActionForm after javascript submit()

Miki Italiano

Joined: Mar 07, 2004
Posts: 7
Good day all.
In my jsp page (using Tiles), I have the following javascript snippet:
My brain on struts <picture of the air goes here>
<script type="text/javascript">
function pageSubmit(newStartIndex) {
document.forms[0].startIndex.value = newStartIndex
In the body of the document, I have this:
<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 var="pageSubmitJS" scope="page">
javascript ageSubmit(<c ut value="${newStartIndex}"/> ;
<a href="/archives/"
oncluck="<c ut value='${pageSubmitJS}'/>">
Page <c ut value="${pageNbr}"/>
This generates the following links:
<a href="/archives/" oncluck="javascript ageSubmit(1);">
Page 1
<a href="/archives/" oncluck="javascript ageSubmit(16);">
Page 2
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?
Ranch Hand

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

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) {
return true;
<html:link ... oncluck="javascript ageSubmit(16);return false;"...
Thanks for your time.
I agree. Here's the link:
subject: Null value in ActionForm after javascript submit()
It's not a secret anymore!