Econify
asked on
Struts Question - Can't access contents of form into form's action class after submiting form...
Hi experts,
This is some part my Part of Struts-config.xml
========================== ==
<form-bean name="viewcartForm" type="com.pal.struts.form. ViewcartFo rm" />
<action
attribute="viewcartForm"
input="/jsp/ViewCart.jsp"
name="viewcartForm"
path="/viewcart"
scope="request"
type="com.pal.struts.actio n.Viewcart Action">
<forward name="viewc" path="/jsp/ViewCart.jsp">< /forward>
</action>
<action
attribute="viewcartForm" name="viewcartForm" path="/updatecart"
type="com.pal.struts.actio n.Updateca rtAction">
<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.g if" value="Delete from Cart" property="delbtn"
alt='Delete from Cart'/></td></tr>
<logic:iterate name="viewcartForm" property="cartdata" id="partsincart">
<bean:define id="units" name="partsincart" property="qty" type="java.lang.Double" />
<bean:define id="unitprice" name="partsincart" property="unit_price" type="java.lang.Double" />
<% Double sub_ttl; %>
<tr>
<td class=cartevenrow><bean:wr ite name="partsincart" property="partid" /></td>
<td class=cartevenrow><bean:wr ite name="partsincart" property="partdesc" /></td>
<td class=cartevenrow><bean:wr ite name="partsincart" property="qty" /></td>
<td class=cartevenrow><bean:wr ite name="partsincart" property="rfqpart" /></td>
<td class=cartevenrow><%=((Dou ble) pageContext.getAttribute(" units")).d oubleValue () * ((Double)
pageContext.getAttribute(" unitprice" )).doubleV alue() %></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(CartSessionDat a[] 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.getAttribut e("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
This is some part my Part of Struts-config.xml
==========================
<form-bean name="viewcartForm" type="com.pal.struts.form.
<action
attribute="viewcartForm"
input="/jsp/ViewCart.jsp"
name="viewcartForm"
path="/viewcart"
scope="request"
type="com.pal.struts.actio
<forward name="viewc" path="/jsp/ViewCart.jsp"><
</action>
<action
attribute="viewcartForm" name="viewcartForm" path="/updatecart"
type="com.pal.struts.actio
<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 class=aheader align=right>Sub-Total($)</
<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.g
alt='Delete from Cart'/></td></tr>
<logic:iterate name="viewcartForm" property="cartdata" id="partsincart">
<bean:define id="units" name="partsincart" property="qty" type="java.lang.Double" />
<bean:define id="unitprice" name="partsincart" property="unit_price" type="java.lang.Double" />
<% Double sub_ttl; %>
<tr>
<td class=cartevenrow><bean:wr
<td class=cartevenrow><bean:wr
<td class=cartevenrow><bean:wr
<td class=cartevenrow><bean:wr
<td class=cartevenrow><%=((Dou
pageContext.getAttribute("
<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(CartSessionDat
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()
userid=session.getAttribut
System.out.println("UserID
=================
Now in above code of UpdateCartAction.java on the line no. 5 ie.,
System.out.println("Size of Carts Items========="+ viewcartForm.getCartdata()
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
ASKER
Hi,
I tried this solution before. But Problem with this solution is that I am getting all the values of original "viewcartForm" when it is first time populated. & When I am checking any of the checkbox corresponding to each of parts in my ViewCart.jsp page I am getting its value in updatecartAction as "false" instead of true. I think here my updatecart action is not getting modified values of checkbox. How should i get list of all the parts in my ViewCart.jsp for which checkbox are checked?
Currently when I am using session attribute for my viewcartForm form-bean , I am getting all the values of checkboxes as false. Though I am checking some of them.
Why this is happening?
Please tell me
Thanx in advance.
prash
I tried this solution before. But Problem with this solution is that I am getting all the values of original "viewcartForm" when it is first time populated. & When I am checking any of the checkbox corresponding to each of parts in my ViewCart.jsp page I am getting its value in updatecartAction as "false" instead of true. I think here my updatecart action is not getting modified values of checkbox. How should i get list of all the parts in my ViewCart.jsp for which checkbox are checked?
Currently when I am using session attribute for my viewcartForm form-bean , I am getting all the values of checkboxes as false. Though I am checking some of them.
Why this is happening?
Please tell me
Thanx in advance.
prash
Just notice you are using nested beans in the formbean, so will need to use the struts <nested> tag.
Example:
<nestedc:iterate name="viewcartForm" property="cartdata" >
<nested:checkbox property="checkedfordel" />
</nested:iterate>
Example:
<nestedc:iterate name="viewcartForm" property="cartdata" >
<nested:checkbox property="checkedfordel" />
</nested:iterate>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you want to get the cartdata which is stored during the action "/viewcart" in action "/updatecart", you have to use scope session for action "/viewcart", so both action use the same formbean from httpsession.
Regards,
Boon Leng