Re: Custom types and select lists

2010-04-20 Thread Alex Rodriguez Lopez
How I do it with selects is having getter/setter for whatever you write 
in name= (in your case campus) as type Long (should be ListLong if 
select is multiple). In prepare() you put in campus the campus id of the 
campus that should be preselected (the one acording to the student id, 
right?), and when the select is displayed it should show the 
pre-selected campus. After, when the form is submited, you get the 
selected campus id back in the action with the getter for campus and 
do whatever you want with it (e.g. update student info).


I don't know if there is a more correct way to do things, but this way 
you don't need custom converters.


I believe, by the type of your error, you could still use that converter 
maybe just switching things from String to Long.


Regards.

Alex

Em 19-04-2010 15:38, Lance Hill escreveu:

I  am trying to find an example of how to populate a select list of objects,
show the correct item as selected, and if the selection is saved, properly
update the object containing the item. In the example below, I would want to
show a group of fields for Student information with a select list of
Campuses. If the Student is already stored with a specific Campus, that
should be selected. If the selection choice is changed, saving the Student
should update what Campus object is contained by the Student.



I have a custom TypeConverter  to convert between the Campus id string and
Campus objects and I can see the conversion code executes fine.
Unfortunately, saving a selection results in an invalid field value for
field 'campus' error and when I first call up the page, the selected Campus
is not the correct one.





public class Student {



   private Campus campus;



   public Campus getCampus() {return campus; }

   public void setCampus(Campus campus) {this.campus = campus; }

}



public class Campus {



   private Long id;

private String name;



   public Long getId() {return name; }

   public void setId(Long id) {this.id = id; )

   public String getName() {return name; }

   public void setName(String name) {this.name = name; }

}



public class StudentAction extends ActionSupport implements ModelDriven,
Preparable {



protected Object model;

private Student student;

   private Campus campus;

private ListCampus  campusList;

   private DB db;

   private long id;



   public void prepare() throws Exception {

 campusList = db.findAllCampuses();

 if (getId() == 0) {

student = db.createNewStudent();

 } else {

student = db.findByStudentId(getId());

 }

   }



   public String save() {

   try {

   db.save(getModel());

 } catch (Exception e) {

   return ERROR;

 }

   return SUCCESS;

 }

   public Student getModel() {return student; }

public long getId() {return id; }

public void getId() {return id; }



}



public class CampusConverter extends StrutsTypeConverter {



private DB db;



   public Object convertFromString(Map context, String[] values, Class
toClass) {

   Campus campus = null;

 if (values != null  values.length  0  values[0] != null
values[0].length()  0) {

 campus = db.findCampusById(new Long(values[0]));

 }

 return campus;

   }

   public String convertToString(Map context, Object o) {

 if (o instanceof Campus) {

 return ((Campus)o).getId().toString();

 }

 return ;

   }

}



body

   s:form action=student_save method=post

 s:select name=campus list=campusList listValue=name
listKey=id label=%{getText('label.campus')} /

 s:hidden name=id value=%{id} /

 s:submit value=%{getText('button.label.submit')}/

   /s:form

/body



Suggestions, corrections, pointers, or links to examples/tutorials of how to
do this properly would be greatly appreciated.





Regards,

Lance Hill





-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



Custom types and select lists

2010-04-19 Thread Lance Hill
I  am trying to find an example of how to populate a select list of objects,
show the correct item as selected, and if the selection is saved, properly
update the object containing the item. In the example below, I would want to
show a group of fields for Student information with a select list of
Campuses. If the Student is already stored with a specific Campus, that
should be selected. If the selection choice is changed, saving the Student
should update what Campus object is contained by the Student.

 

I have a custom TypeConverter  to convert between the Campus id string and
Campus objects and I can see the conversion code executes fine.
Unfortunately, saving a selection results in an invalid field value for
field 'campus' error and when I first call up the page, the selected Campus
is not the correct one.

 

 

public class Student {

 

  private Campus campus;

  

  public Campus getCampus() {return campus; }

  public void setCampus(Campus campus) {this.campus = campus; }

}

 

public class Campus {

 

  private Long id;

private String name;

  

  public Long getId() {return name; }

  public void setId(Long id) {this.id = id; )

  public String getName() {return name; }

  public void setName(String name) {this.name = name; }

}

 

public class StudentAction extends ActionSupport implements ModelDriven,
Preparable {

 

protected Object model;

private Student student;

  private Campus campus;

private ListCampus campusList; 

  private DB db;

  private long id;

 

  public void prepare() throws Exception {   

campusList = db.findAllCampuses();

if (getId() == 0) {

student = db.createNewStudent(); 

} else { 

student = db.findByStudentId(getId()); 

} 

  }

 

  public String save() {

  try {

  db.save(getModel());

} catch (Exception e) {

  return ERROR;

}

  return SUCCESS;

}

  public Student getModel() {return student; }

public long getId() {return id; }

public void getId() {return id; }

 

}

 

public class CampusConverter extends StrutsTypeConverter {

  

private DB db;

  

  public Object convertFromString(Map context, String[] values, Class
toClass) {

  Campus campus = null;

if (values != null  values.length  0  values[0] != null 
values[0].length()  0) {

campus = db.findCampusById(new Long(values[0]));

}

return campus;

  }

  public String convertToString(Map context, Object o) {

if (o instanceof Campus) {

return ((Campus)o).getId().toString();

}

return ;

  }

}

 

body

  s:form action=student_save method=post

s:select name=campus list=campusList listValue=name
listKey=id label=%{getText('label.campus')} /

s:hidden name=id value=%{id} /

s:submit value=%{getText('button.label.submit')}/

  /s:form

/body

 

Suggestions, corrections, pointers, or links to examples/tutorials of how to
do this properly would be greatly appreciated.

 

 

Regards,

Lance Hill