• 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

struts form HELP!

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to struts, and working on a form where a user would use checkboxes to select specific records from a list .The list object is of type 'purchaseOrder' and it can contain 1 or more products. My question is how would the struts action class know which of the product has been clicked.

following is the JSP code:

///////////////////////////////////////////////////////////////
<nested:root name="invoicePOForm">
Invoice Details for Purchase Order Number: <nested:write property="purchaseOrderNumber"/>
<nested:iterate property="purchaseOrderProduct">
<tr>
<td Width="15%"><nested:write property="productId"/> </td>
<td Width="25%"><nested:write property="productTitle"/></td>
<td Width="10%"><nested:write property="orderedQty"/> </td>
<td Width="10%"><nested:write property="receivedQty"/> </td>
<td Width="10%"><nested:write property="invoicedQty"/> </td>
<td Width="15%"><nested:write property="costPrice"/> </td>
<td Width="15%"><nested:text property="invoiceUnitPrice"/> </td>
<td Width="15%"><nested:text property="invoiceTotalPrice"/> </td>
<td Width="10%"><nested:text property="invoiceQty"/> </td>
<td Width="15%"><html:checkbox property="selectedCheckbox" value="ok"/> </td>

</nested:iterate>
</nested:root>
</table>
<html:submit> Submit </html:submit>

/////////////////////////

Follwowing is the form bean:
public PurchaseOrder m_aPurchaseOrder;
public String m_sPurchaseOrderNumber;
public String selectedCheckbox[]=new String[]{};

public String[] getSelectedCheckbox() {
return selectedCheckbox;
}


public void setSelectedCheckbox(String[] selectedCheckbox) {
this.selectedCheckbox = selectedCheckbox;
}



public String getPurchaseOrderNumber() {
return m_sPurchaseOrderNumber;
}

public void setPurchaseOrderNumber(String m_sPurchaseOrderNumber) {
this.m_sPurchaseOrderNumber = m_sPurchaseOrderNumber;}


public PurchaseOrder getPurchaseOrder() {
return m_aPurchaseOrder;
}

public void setPurchaseOrder(PurchaseOrder m_aInvoicePurchaseOrder) {
this.m_aPurchaseOrder = m_aInvoicePurchaseOrder;
}

public List<PurchaseOrderProduct> getPurchaseOrderProduct() {
return m_aPurchaseOrderProduct;
}

public void setPurchaseOrderProduct(List<PurchaseOrderProduct> m_aPurchaseOrderProduct) {
this.m_aPurchaseOrderProduct = m_aPurchaseOrderProduct;
}


public void clearAll(){
this.m_aPurchaseOrder=null;
}


public void reset(ActionMapping mapping, HttpServletRequest request){
this.selectedCheckbox=new String[]{};
}


}

///////////////////////////////////////////////////

Following is part of Action class:

List<PurchaseOrder> poList=Service.getInvoicePurchaseOrder(request.getParameter("cid"));
PurchaseOrder ipo=poList.get(0);
actionForm.clearAll();
actionForm.setPurchaseOrderNumber(ipo.getPurchaseOrderNumber());
actionForm.setPurchaseOrderProduct(poList.get(0).getProducts());



//////////////////////////////////////////////////////////////////
 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
realised the formbean has an additional attribute as well:

//////
public List<PurchaseOrderProduct> m_aPurchaseOrderProduct=new ArrayList<PurchaseOrderProduct>();

///////

Thanks.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change your selectedCheckbox property to selectedProductIds and make it of type String[]. Then change:

to

When the form is submitted, the selectedProductIds property will contain an array of the product IDs selected. I'm assuming that the product ID uniquely identifies the line item.
[ August 28, 2007: Message edited by: Merrill Higginson ]
 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the code, it worked!

I was working on a solution of adding the attribute 'isSelectedCheckBox' to the PurchaseOrderProduct object itself(not the form bean). It works but once the checkbox is checked and submit clicked and if the user is directed to the same originating screen(this wont happen in the end product) the checkbox retains it initial value(i.e. once true, itll stay true forever). dont know how to get round this.
 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another problem. The text boxes i am using displays values taken from the database/object's values when the form is loaded. If a user change those values , the action class still takes the database values. How can it take the changed values? what changes do i have to make to the form bean .

Thanks in advance.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are going to modify the values in the bean and not just display them, you will need to use indexed properties. For more information on how to do this along with a working example, see question 6 of the JavaRanch Struts FAQ.
 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice, and pointing me in the right direction. I am now getting the right values.
I have one last question , more conceptual one than any coding. I have to give the user option of adding a product to the purchaseOrder from the JSP page. Does this mean i would need an another formbean and a action class for this. How can i add a new PO line to the Purchase Order whilst retaining any changes done on the JSP page.Can you point me in the right direction.

thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic