• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null value in ActionForm after javascript submit()

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic