This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Struts and the fly likes struts form HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts form HELP!" Watch "struts form HELP!" New topic
Author

struts form HELP!

ahsan mir
Ranch Hand

Joined: Jul 17, 2006
Posts: 42
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

Joined: Jul 17, 2006
Posts: 42
realised the formbean has an additional attribute as well:

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

///////

Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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 ]

Merrill
Consultant, Sima Solutions
ahsan mir
Ranch Hand

Joined: Jul 17, 2006
Posts: 42
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

Joined: Jul 17, 2006
Posts: 42
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Jul 17, 2006
Posts: 42
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts form HELP!
 
Similar Threads
Problem with ActionForm + dynamically generated fields..
validation of indexed properties
Problem with forward in the struts-config.xml-File
Struts: No getter method for property name problem
form bean is not getting updated upon submit in jsp