Link to home
Start Free TrialLog in
Avatar of carydb
carydb

asked on

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

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" %>

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

<body>

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

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

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

<logic:present name = "allRcds">
   <p><b> ADCN PART TABLE </b></p>
                 <html:submit>
                <fmt:message key="adcnPartU.button.select"/>
              </html:submit>
   <table border="1">
   <thead>  
      <tr>
          <th>Doc_no_i</th>
          <th>Part_no_i</th>
          <th>Actn_c</th>
          <th>Cls1_c</th>
          <th>Ndx1_i</th>
          <th>Std_part_i</th>
          <th>Part_nomen_i</th>
          <th>Cage_i</th>
          <th>Part_q</th>
          <!-- th>SELECT<th -->
       </tr>
    </thead>
    <tbody>  
       <logic:iterate id="theRecords" name="allRcds">
        <tr>
          <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">
              <html:submit>
                <fmt:message key="adcnPartU.button.select"/>
              </html:submit>
            </td -->
         </tr>
        </logic:iterate>
      </tbody>  
      </table>
</logic:present>

</html:form>
</body>
</html>





////// 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 = "";
            //vec=null;
            // 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;
      //}


}
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

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.
Avatar of carydb
carydb

ASKER

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.
> May be this will help http://husted.com/struts/tips/006.html
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.
http://struts.apache.org/struts-action/userGuide/building_controller.html#map_action_form_classes

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

ASKER

Boonleng,
  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.
ASKER CERTIFIED SOLUTION
Avatar of boonleng
boonleng
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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() %>"/>