aspose file tools*
The moose likes Struts and the fly likes Cant Access contents of form in forms Action class... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Cant Access contents of form in forms Action class..." Watch "Cant Access contents of form in forms Action class..." New topic
Author

Cant Access contents of form in forms Action class...

Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186
Hi all,

This is some part my Part of Struts-config.xml
============================
<form-bean name="viewcartForm" type="com.pal.struts.form.ViewcartForm" />

<action
attribute="viewcartForm"
input="/jsp/ViewCart.jsp"
name="viewcartForm"
path="/viewcart"
scope="request"
type="com.pal.struts.action.ViewcartAction">
<forward name="viewc" path="/jsp/ViewCart.jsp"></forward>
</action>


<action
attribute="viewcartForm" name="viewcartForm" path="/updatecart"
type="com.pal.struts.action.UpdatecartAction">
<forward name="success" path="/viewcart.do" />
</action>

==========================

Now in my web application there is page "viewcart.jsp" which displays all parts in cart. as follows:-
=================================
<html:form action="updatecart" method="post">
<table border="0" cellpadding="0" width="100%" bgcolor="#71B8FF"
bordercolor="#000000" cellspacing="1">
<tbody>
<tr>
<td class=aheader align=center>Part No.</td>
<td class=aheader align=center>Description</td>
<td class=aheader align=center>Qty</td>
<td class=aheader align=right>Unit-Price($)</td>
<td class=aheader align=right>Sub-Total($)</td>
<td class=aheader align=center> <html:image src="../images/RFQ.gif" value="Move To Enquiry Cart" property="rfqbtn"

alt='Move To RFQ Cart'/></td>
<td class=aheader align=center> <html:image src="../images/Del_Parts.gif" value="Delete from Cart" property="delbtn"

alt='Delete from Cart'/></td></tr>
<logic:iterate name="viewcartForm" property="cartdata"id="partsincart">
<bean efine id="units" name="partsincart" property="qty" type="java.lang.Double" />
<bean efine id="unitprice" name="partsincart" property="unit_price" type="java.lang.Double" />

<%Double sub_ttl;%>
<tr>
<td class=cartevenrow><bean:writename="partsincart" property="partid" /></td>
<td class=cartevenrow><bean:writename="partsincart" property="partdesc" /></td>
<td class=cartevenrow><bean:writename="partsincart" property="qty" /></td>
<td class=cartevenrow><bean:writename="partsincart" property="rfqpart" /></td>
<td class=cartevenrow><%=((Double) pageContext.getAttribute("units")).doubleValue() * ((Double)

pageContext.getAttribute("unitprice")).doubleValue() %></td>
<td class=cartevenrow><logic:equal name="partsincart" property="rfqpart" value="true">
<html:checkbox name="partsincart" property="checkedforrfq" /></logic:equal></td>
<td class=cartevenrow><html:checkbox name="partsincart" property="checkedfordel" /></td>

</tr></logic:iterate>
</table>
</html:form>
================================
& My ViewCartForm.java (formbean) is as follows:-

=================
public class ViewcartForm extends ActionForm {

private CartSessionData[] cartdata=new CartSessionData[0];
//CartSessionData is my ValueObject Class
private CartSessionData cartdata1=new CartSessionData();

private String rfqbtn;

private String delbtn;


public CartSessionData[] getCartdata() {

return cartdata;

}


public void setCartdata(CartSessionData[] cartdata) {

this.cartdata = cartdata;

}

public String getDelbtn() {

return delbtn;

}

public void setDelbtn(String delbtn) {

this.delbtn = delbtn;

}

public String getRfqbtn() {

return rfqbtn;

}

public void setRfqbtn(String rfqbtn) {

this.rfqbtn = rfqbtn;

}


}
===============
& on Clicking on Submit Buttons I am calling action "updatecart.
Now in UpdatecartAction.java I am not able to aceess contents of my ViewcartForm to which I want to process in

UpdatecartAction.java.
Some code of my UpdatecartAction.java is as follows:-
====================
public ActionForward execute(

ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {


HttpSession session=request.getSession();

ViewcartForm viewcartForm = (ViewcartForm) form;

System.out.println("Size of Carts Items========="+ viewcartForm.getCartdata().length);


userid=session.getAttribute("userid").toString();

System.out.println("UserID is:--"+userid);
=================

Now in above code of UpdateCartAction.java on the line no. 5 ie.,
System.out.println("Size of Carts Items========="+ viewcartForm.getCartdata().length);


It is giving me length of CartData as '0' though at present there are 3 items in my ViewCartForm that I can View in my

ViewCart.jsp Page.
I think here the values in the formbean are not getting transferredf from ViewCart.jsp to UpdateCartAction.
Where I am wrong? As I acnt access to any content of my formbean on updatecartaction I am not able to manipulate it at all in

my updatecartaction.
Please tell me what shoul I do so that I can Access the contents of "ViewCartForm" in UpdatecartAction ?

Thanx in advance.
Prash


********Deserve Before You Desire********
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Prashant,
In the struts config file, you have the form scope set to action. This means the values will be wiped out between page submissions. If you want the view and update pages to share a form bean, set the scope to session.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186
Hi,
Thanx for ur reply,
But in my application I am submitting my viewcartForm to "updatecart" action. So all the values of form fields should be available in "updatecartAction". Also If I am using ur suggestion in my application (setting scope of form to session) then I am not able to view checkbox status in my action form. For all the checkboxes those I have checked in my viewcartForm in "viewcart.jsp" corresonding to each part I am getting its value as "false". I think this happening because "updatecartaction" trying to access the original form bean for which the scope is set to session. & it is not referring my form in Viewcart.jsp after I am checking some checkboxes. So what should I do here to get values oin my action(updatecartaction)??

Pls reply.
Thanx.
Prash
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
With session request your form goes away when the first page is displayed. When the page is submitted Struts will create a brand new, empty instance of your form. That new instance will be populated with values that were submitted with the page. In your case, I do not see many form fields that will be submitted. The checkbox values will, but stuff shown on the page from bean:write will not.

If you want to completely recreate the shopping cart, you need to store all its values on the jsp in hidden fields. This seems like a messy solution. Another option is to use session scope. Another option might be to store the shopping cart in a database table and retrieve the values from the database.

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cant Access contents of form in forms Action class...