• 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

Collection Storing problem

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//Bean Object
public class BeanVO {
private String a;
private String b;
private String c;

public String geta (){return a;}
public void setA(String a) {this.a=a};
public String getB (){return b;}
public void setB(String b) {this.b=b};
public String getC (){return c;}
public void setC(String c) {this.c=c};
}

//Form bean
public class MyForm extends ActionForm{
private BeanVO beanVO;
private String submit; //submit property of html:button

public String getSubmit (){return submit;}
public void setSubmit(String submit) {this.submit=submit};

public MyForm (){
beanVO = new BeanVO();
}

public BeanVO getBeanVO(){ return beanVO;}
public void setBeanVO(){ this.beanVO = beanVO;}
}

//Action Class
pubic class MyAction extends Action {
ActionForward execute(.....){
MyForm form = (ActionForm)MyForm;
if (form!=null){
String action = form.getSubmit();
if (action == null){
this.loadForm(req,form);//this method will get the sql data and display in jsp
return mapping.findForward("success");
}
if ("Save".equals(action)){
this.updateForm(req,form);
return mapping.findForward("save");
}

public void loadForm(HttpServletRequest req, ActionForm actionForm){
//get collection of other objects from databaseand extract field a,b,c of other objects
//and put them in Collection<BeanVO> then set this collection in session
//for loading them in jsp to display to web browser
Collection<MyObject> myObjectCollection = new Collection<MyObject>();
Collection<OtherObject> records = retriveFromDAO(baseOnSomeCriteriaField);
Iterator<OtherObject> iter = records.iterator();
while(iter.hasNext()){
MyObject myObject = new MyObject();
myObject.setA(iter.getFielda);
myObject.setB(iter.getFieldB);
myObjectCollection.add(myObject);
}
setSessionObject(req,"myObjectCollection",myObjectCollection);
}

//when some value is entered in textbox c and
//Save button is clicked updateForm is called
public void updateForm(HttpServletRequest req, ActionForm actionForm){
//get the MyObject Collection from session stored in loadForm
Collection<MyObject> myObjectCollection = (Collection<MyObject>)
getSessionObject(req,"myObjectCollection");
MyForm form = (ActionForm) MyForm;
//here comes the null problem: after ** execute,
// c is null when I Step over the Eclipse debug mode
String c = form.getMyObject().getC(); //**
//could not do any thing at thispoint until I can get some value from c
// the rest is held because of this problem
}
}

//JSP

public class MyJsp {
-----skip the long unrelated issues----
---some common header and body from tiles

<html:form action="/myAction.do" method="post">
some craps.....
//here come the logic:iterate over the myObjectCollection
<table>
<tr><th>A</th><th>B</th><th>C</th></tr>
<logic:iterate id="myObject" name="myObjectCollection" >
<tr>
<td><html:text><bean:write name="myObject" property="a"/><td>
<td><html:text><bean:write name="myObject" property="b"/><td>
<td><html:text><bean:write name="myObject" property="c"/><td>
</tr>
</logic:iterate>

<tr>
<td>
<html:submit property="submit" value="Save"/>
</td>
</tr>
</table>
</html:form>

In summary, I was able to load all the instances in myObjectCollection in jsp.
But when I entered some value in text box c and hit Save button, I went back
to eclipse debug mode and see String c = form.getMyObject().getC() is null

To test the MyForm class, I also put in the property c
so that i can use String c = form.getC(); but it is also null.

Please help. I would be very greatful for all the ideas and suggestions to
make this work. Thanks



 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags. Unformatted code/config/etc. is difficult to read. You can edit your post to include them using the button or re-post the question with proper formatting.
 
Thomas Bui
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//Bean Object


public class BeanVO {
private String a;
private String b;
private String c;

public String geta (){return a;}
public void setA(String a) {this.a=a};
public String getB (){return b;}
public void setB(String b) {this.b=b};
public String getC (){return c;}
public void setC(String c) {this.c=c};
}



//Form bean


public class MyForm extends ActionForm{
private BeanVO beanVO;
private String submit; //submit property of html:button

public String getSubmit (){return submit;}
public void setSubmit(String submit) {this.submit=submit};

public MyForm (){
beanVO = new BeanVO();
}

public BeanVO getBeanVO(){ return beanVO;}
public void setBeanVO(){ this.beanVO = beanVO;}
}



//Action Class


pubic class MyAction extends Action {
ActionForward execute(.....){
MyForm form = (ActionForm)MyForm;
if (form!=null){
String action = form.getSubmit();
if (action == null){
this.loadForm(req,form);//this method will get the sql data and display in jsp
return mapping.findForward("success");
}
if ("Save".equals(action)){
this.updateForm(req,form);
return mapping.findForward("save");
}

public void loadForm(HttpServletRequest req, ActionForm actionForm){
//get collection of other objects from databaseand extract field a,b,c of other objects
//and put them in Collection<BeanVO> then set this collection in session
//for loading them in jsp to display to web browser
Collection<MyObject> myObjectCollection = new Collection<MyObject>();
Collection<OtherObject> records = retriveFromDAO(baseOnSomeCriteriaField);
Iterator<OtherObject> iter = records.iterator();
while(iter.hasNext()){
MyObject myObject = new MyObject();
myObject.setA(iter.getFielda);
myObject.setB(iter.getFieldB);
myObjectCollection.add(myObject);
}
setSessionObject(req,"myObjectCollection",myObjectCollection);
}

//when some value is entered in textbox c and
//Save button is clicked updateForm is called
public void updateForm(HttpServletRequest req, ActionForm actionForm){
//get the MyObject Collection from session stored in loadForm
Collection<MyObject> myObjectCollection = (Collection<MyObject>)
getSessionObject(req,"myObjectCollection");
MyForm form = (ActionForm) MyForm;
//here comes the null problem: after ** execute,
// c is null when I Step over the Eclipse debug mode
String c = form.getMyObject().getC(); //**
//could not do any thing at thispoint until I can get some value from c
// the rest is held because of this problem
}
}




//JSP


public class MyJsp {
-----skip the long unrelated issues----
---some common header and body from tiles

<html:form action="/myAction.do" method="post">
some craps.....
//here come the logic:iterate over the myObjectCollection
<table>
<tr><th>A</th><th>B</th><th>C</th></tr>
<logic:iterate id="myObject" name="myObjectCollection" >
<tr>
<td><html:text><bean:write name="myObject" property="a"/><td>
<td><html:text><bean:write name="myObject" property="b"/><td>
<td><html:text><bean:write name="myObject" property="c"/><td>
</tr>
</logic:iterate>

<tr>
<td>
<html:submit property="submit" value="Save"/>
</td>
</tr>
</table>
</html:form>



In summary, I was able to load all the instances in myObjectCollection in jsp.
But when I entered some value in text box c and hit Save button, I went back
to eclipse debug mode and see String c = form.getMyObject().getC() is null

To test the MyForm class, I also put in the property c
so that i can use String c = form.getC(); but it is also null.

Please help. I would be very greatful for all the ideas and suggestions to
make this work. Thanks
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those are "quote" tags, not "code" tags.That shouldn't even compile.
 
Thomas Bui
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


<logic:iterate id="myObject" name="myObjectCollection" >
<tr>
<td><html:text><bean:write name="myObject" property="a"/><td>
<td><html:text><bean:write name="myObject" property="b"/><td>
<td><html:text><bean:write name="myObject"

property="c" disable="true"/

><td>
</tr>
</logic:iterate>



Thanks for your reply. I am able to populate them to the jsp and display to the browser. I wonder if setting the textbox not editable is the problem of getting back the value on the form. I am able to get the value from box b but not c.
The problem is box c is not editable. It will get the sum from a+b using the javascript. Can you give me some idea? Thanks
 
Thomas Bui
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. I got all the collection in the form resolved but now I am facing the new problem.
I am using logic:iterate on a different collection and put the objects in my collection which is a property of my form bean and set it in session. When I populate the form bean's collection, it gets displayed properly. But, when I get the sesion back and try to update and retrieve it back in my action, I only get the first object in my collection and the rest are null. Please help. Thanks

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please start a new thread and post the code. With *code* tags.
 
reply
    Bookmark Topic Watch Topic
  • New Topic