You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ajay Patil <ap...@vertex.co.in> on 2003/07/30 05:10:32 UTC

Example : Search & Update Rows.

Hi Faisal,

Here is an sample code for searching and updating rows for a table.
This code only shows the basic structure for implementing this 
functionality. I hope there are not any major typos that I have
done ;-) 

Trust this helps,
Ajay

struts-config.xml
------------------
<action path="/search" 
        type="xx.yy.SearchForm" 
        name="SearchForm">
  <forward name="success" path="/xx/yy/search.jsp" />
</action>

<action path="/update" 
        type="xx.yy.UpdateForm" 
        name="UpdateForm">
  <forward name="success" path="/xx/yy/search.jsp" />
</action>

Search.jsp
----------

<html:form name="SearchForm" type="xx.yy.SearchForm" action="/search">
   <html:text property="searchField1" />
   <html:text property="searchField2" />
   <html:submit><bean:message key="button.search" /></html:submit>
</form>

<logic:present name="SearchForm" property="searchResult">
<html:form action="/updateRow">
  <table>
  <logic:iterate name="SearchForm" property="searchResult" id="row" >
  <tr>
    <!-- Please note that id in logic:iterate tag should match
         the name in html:text tag -->
    <td><html:text name="row" property="fieldX" indexed="true" /></td>
    <td><html:text name="row" property="fieldY" indexed="true" /></td>
    <td><html:text name="row" property="fieldZ" indexed="true" /></td>
  </tr>
  </logic:iterate>
  </table>
  <html:submit><bean:message key="button.update" /></html:submit>
</html:form>
<logic:present name="SearchForm" property="searchResult">

SearchForm.java
---------------

public class SearchForm {

  private String searchField1;
  private String searchField2;
  private ArrayList rows;

  public String getSearchField1() {
    return this.searchField1;
  }

  public void setSearchField1(String searchField1) {
    this.searchField1 = searchField1;
  }

  public String getSearchField2() {
    return this.searchField2;
  }

  public void setSearchField2(String searchField2) {
    this.searchField2 = searchField2;
  }

  public ArrayList getSearchResult() {
    return this.rows;
  }

  public void setSearchResult(ArrayList rows) {
    this.rows = rows;
  }

  public Row getSearchResult(int index) {

   // THE WHILE LOOP IS REQUIRED IF YOUR FORM IS OF REQUEST SCOPE.
    while (this.rows.size() + 1 < index) 
      this.rows.add(new Row());

    return (Row) this.rows.get(index);
  }
}

Row.java
--------
public class Row {
  private String fieldX;
  private String fieldY;
  private String fieldZ;
  
  public String getFieldX() { 
   return this.fieldX; 
  }
  
  public void setFieldX(String fieldX) {
   this.fieldX = fieldX:
  }

  public String getFieldY() { 
   return this.fieldY; 
  }
  
  public void setFieldY(String fieldY) {
   this.fieldY = fieldY:
  }

  public String getFieldZ() { 
   return this.fieldZ; 
  }
  
  public void setFieldZ(String fieldZ) {
   this.fieldZ = fieldZ;
  }
}

SearchAction.java
-----------------

public ActionForward execute(..........) {

  SearchForm searchForm = (SearchForm) form;
  String searchField1 = searchForm.getSearchField1();
  String searchField2 = searchForm.getSearchField2();

  ArrayList rows = BusinessLogicBean.searchRows(searchField1, 
                                                searchField2);
  ... // error-handling stuff

  searchForm.setSearchResult(rows);
  return mapping.findForward(success);
}

UpdateForm.java
----------------
public class UpdateForm {

  private ArrayList rows;

  public ArrayList getRows() {
    return this.rows;
  }

  public Row getRows(int index) {

   // THE WHILE LOOP IS REQUIRED IF YOUR FORM IS OF REQUEST SCOPE.
    while (this.rows.size() + 1 < index) 
      this.rows.add(new Row());

    return (Row) this.rows.get(index);
  }
}

UpdateAction.java
-----------------

public ActionForward execute(..........) {

  UpdateForm updateForm = (UpdateForm) form;
  ArrayList rows = updateForm.getRows();
  BusinessLogicBean.updateRows(rows);
  ... // error-handling stuff
  return mapping.findForward(success);
}

Hello,

Does anyone know how to submit multiple fields in one form. Let me
explain. I have a search jsp which returns for example 3 rows. In each
of the rows there are 3 amendable columns x, y and z. which are stored
as <html:text> The user has the option of amending all the fields thus
if 3 rows were returned they could amend 9 fields. 

I have the table which holds the results enclosed within a form.
However when a user enters data into all the fields only the data
entered in the first row is passed into the form.

Does anyone know how to get all of the data entered in the fields passed
to the action class. The number of rows returned depends upon what is
in the database so is not pre defined.

Thanks in advance
faisal



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org