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 List<Campus> 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