struts - sending "all" rows from jsp back to action form.

Posted on 2006-05-05
Last Modified: 2013-11-24
I have a jsp in my struts app that displays data from a table. I can edit any field in each record, but when I submit, none of the records are captured in the FormBean to be processed by the ActionBean.  The data elements are nulls.  How can I make all records from a jsp forward to the FormBean so it will validate and my action can update the database? I'd even settle for only record from the list being selected and forwarded for update action. I'm attaching my jsp and my Form code.   Thanks in advance for any help on this.

////// THE JSP //////
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%-- JSTL tag libs --%>
<%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %>

<%-- Struts provided Taglibs --%>
<%@ taglib prefix="html" uri="/WEB-INF/struts-html.tld" %>

<%@ taglib prefix="logic" uri="/WEB-INF/struts-logic.tld" %>
<%@ taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>

      <fmt:setBundle basename="ApplicationResources" />
      <title><fmt:message key="adcnPartU.title"/></title>


<html:errors property="adcnPartU"/>
<html:form action="">

<logic:notPresent name = "allRcds">
   <h2>Data source not in scope!</h2>
             <fmt:message key="adcnPartU.button.load"/>

<logic:present name = "allRcds">
   <logic:empty name = "allRcds">
       <h2>Data source in scope, but no data found!</h2>

<logic:present name = "allRcds">
   <p><b> ADCN PART TABLE </b></p>
                <fmt:message key=""/>
   <table border="1">
          <!-- th>SELECT<th -->
       <logic:iterate id="theRecords" name="allRcds">
          <td><input type="int" indexed="true" name="Doc<bean:write name='theRecords' property='record_no' />"  property="doc_no_i" value="<bean:write name='theRecords' property='doc_no_i' />" /></td>
          <td><input type="int" indexed="true" name="Part<bean:write name='theRecords' property='record_no' />"   property="part_no_i" value="<bean:write name='theRecords' property='part_no_i' />" /></td>
          <td><input type="text" indexed="true" name="Act<bean:write name='theRecords' property='record_no' />"  property="actn_c" value="<bean:write name='theRecords' property='actn_c'/>" size="2" maxlength="2" /></td>
          <td><input type="text" indexed="true" name="Cls<bean:write name='theRecords' property='record_no' />"  property="cls1_c" value="<bean:write name='theRecords' property='cls1_c'/>" size="2" maxlength="2" /></td>
          <td><input type="text" indexed="true" name="Ndx<bean:write name='theRecords' property='record_no' />"  property="ndx1_i" value="<bean:write name='theRecords' property='ndx1_i'/>" size="5" maxlength="5" /></td>
          <td><input type="text" indexed="true" name="Std<bean:write name='theRecords' property='record_no' />"  property="std_part_i" value="<bean:write name='theRecords' property='std_part_i'/>" size="15" maxlength="15" /></td>
          <td><input type="text" indexed="true" name="Pnom<bean:write name='theRecords' property='record_no' />"  property="part_nomen_e" value="<bean:write name='theRecords' property='part_nomen_e'/>" size="15" maxlength="15" /></td>
          <td><input type="text" indexed="true" name="cag<bean:write name='theRecords' property='record_no' />"  property="cage_i" value="<bean:write name='theRecords' property='cage_i'/>" size="15" maxlength="15" /></td>
          <td><input type="int"  indexed="true" name="pq<bean:write name='theRecords' property='record_no' />"  property="part_q" value="<bean:write name='theRecords' property='part_q'/>" /></td>
          <!-- td colspan="1" align="center">
                <fmt:message key=""/>
            </td -->


////// THE FORM /////
package adcn;

import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

 * @author Cary Buchanan
public class AdcnPartForm extends ActionForm

      private static final long serialVersionUID = 1L;
      //private Vector vec=null;
      private Integer record_no;
      private Integer doc_no_i;
      private Integer part_no_i;
      private String actn_c;
      private String cls1_c;
      private String ndx1_i;
      private String std_part_i;
      private String part_nomen_e;
      private String cage_i;
      private Integer part_q;
       * Resets data fields to initial values on loginform
       * @param mapping
       * @param request
      public void reset(ActionMapping mapping, HttpServletRequest request)
            // Gotta be some rules.  Like Can't reset DOC_NO_I and other fields?
            actn_c = "";
            std_part_i = "";
            // MAke this function go back to record number one from the bean.
       * Performs validation of data on loginform
       * @param mapping
       * @param request
       * @return ActionErrors
      //Probably not needed here, since this is just a view.
      public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
            // Put
            System.out.println("Validating field data!");
            ActionErrors errors = new ActionErrors();
            // put in some code to check questionable data / nulls, etc.
            HttpSession session = request.getSession();
            Vector sData = (Vector)session.getAttribute("allRcds");
            if (sData != null) {
            System.out.println("Session vector captured!");

              if( doc_no_i == null  || (doc_no_i.intValue() == 0)){
                    System.out.println("doc_no_i is null!");
                    errors.add("doc_no_i", new ActionError("error.doc_no_i.required"));
              return errors;
            return errors;
       * @return String - Attribute Accessors

      public String getActn_c() {
            return actn_c;

      public void setActn_c(String actn_c) {
            this.actn_c = actn_c;

      public String getCage_i() {
            return cage_i;

      public void setCage_i(String cage_i) {
            this.cage_i = cage_i;

      public String getCls1_c() {
            return cls1_c;

      public void setCls1_c(String cls1_c) {
            this.cls1_c = cls1_c;

      public Integer getDoc_no_i() {
            return doc_no_i;

      public void setDoc_no_i(Integer doc_no_i) {
            this.doc_no_i = doc_no_i;

      public String getNdx1_i() {
            return ndx1_i;

      public void setNdx1_i(String ndx1_i) {
            this.ndx1_i = ndx1_i;

      public Integer getPart_no_i() {
            return part_no_i;

      public void setPart_no_i(Integer part_no_i) {
            this.part_no_i = part_no_i;

      public String getPart_nomen_e() {
            return part_nomen_e;

      public void setPart_nomen_e(String part_nomen_e) {
            this.part_nomen_e = part_nomen_e;

      public Integer getPart_q() {
            return part_q;

      public void setPart_q(Integer part_q) {
            this.part_q = part_q;

      public String getStd_part_i() {
            return std_part_i;

      public void setStd_part_i(String std_part_i) {
            this.std_part_i = std_part_i;

      public Integer getRecord_no() {
            return record_no;

      public void setRecord_no(Integer record_no) {
            this.record_no = record_no;
      public String toString() {
        return doc_no_i.toString() + part_no_i.toString() + actn_c + cls1_c + ndx1_i +      std_part_i + part_nomen_e+ cage_i+part_q.toString();

      //public Vector getVec() {
      //      return vec;

      //public void setVec(Vector vec) {
      //      this.vec = vec;

Question by:carydb
    LVL 16

    Expert Comment

    by:Peter Kwan
    This is because you are using standard HTML <input> tag rather than <html:text> tag.

    Please try using <html:text> tag to replace <input> tag.

    Author Comment

    I did, but now my data does not load into the form and I have no box in which to change the data.  

    Still, how would that help me get all of my rows in to the Action Form when I hit submit?  That's my goal.
    LVL 14

    Expert Comment

    LVL 14

    Expert Comment

    > May be this will help
    Hmm... The above tutorial probably not good enought to solve the problem.

    Should use a map-backed form, then you can store the record Id as the map key.

    Im currently dont have my workspace with me, will try to get back to you tommorow.

    Author Comment

      Thanks.   I'm really having a difficult time with this. Mostly because I am new to jsp/servlet web programming in general, let alone struts. Will look again tomorrow.  Thanks.
    LVL 14

    Accepted Solution

    Sorry for the late reply, I dont have internet access at work, so will be a bit slow in replying message.
    The following is th sample code, on the form bean, use Map as the property instead of String.

    public class AdcnPartForm extends ActionForm {

        private Map cages = new HashMap();

        public Map getCages() {
            return cages;

        public String getCage(String key) {
            return cages.get(key);

        public void setCages(Map cages) {
            this.cages = cages;

        public void setCage(String key, String value) {
            this.cages.put(key, value);

    Then on the jsp, create the input field with the name "<property name>(<map key>)". E.g.:

          <c:forEach var="row" items="${allRecords}">
                <html:text property="cage(${row.record_no})" value="${row.cage_i}"/>

    Once the form submited, in the action class, the map key for the property will be the record no and the map value will be the input value. E.g.:

        public ActionForward execute(ActionMapping mapping,
            ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

            AdcnPartForm thisForm = (AdcnPartForm) form;

            if (thisForm.getCages() != null) {
                for (String key : thisForm.getRecords().keySet()) {
                    System.out.println(key + "=" + thisForm.getRecord(key));
            return mapping.findForward("success");

    In such way, you'll be able to identity each value that tie to the record no when submitting all rows.

    Boon Leng
    LVL 14

    Expert Comment

    Im using JDK1.5 and JSTL, so you might need to change for loop in action class, and jstl tag to "pageContext" to retrive value from <logi:iterate>, E.g:

    <% SomeBean row = (SomeBean) pageContext.getAttribute("row"); %>
    <html:text property="<%="cage(" + row.getRecord_no() + ")" %>" value="<%=row.getCage_i() %>"/>

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    sum28 challenge 31 82
    powerN  challenge 3 31
    array6 challenfge 6 48
    count11 challenge 6 30
    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now