• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

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;
      //}


}
0
carydb
Asked:
carydb
  • 4
  • 2
1 Solution
 
Peter KwanCommented:
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.
0
 
carydbAuthor Commented:
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.
0
 
boonlengCommented:
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
boonlengCommented:
> 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.
0
 
carydbAuthor Commented:
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.
0
 
boonlengCommented:
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}"/>
      </c:forEach>


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.


Regards,
Boon Leng
0
 
boonlengCommented:
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() %>"/>
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now