Martin,
First of all, thanks for the reply. The full action is listed below:
I guess what is not clear to me is whether I need to create a
StrutsTypeConverter to convert the selected item from the list back to a
model.
As I have been thrashing about trying to get this to work, I have tried
creating a converter to convert the id passed back to an object...
public class StatusConverter extends StrutsTypeConverter {
private static final Log _log =
LogFactory.getLog(StatusConverter.class);
@Override
public Object convertFromString(Map context, String[] values,
Class toClass) {
if (values.length > 0 && values[0] != null &&
values[0].trim().length() > 0) {
Status status= new Status();
status.setId(new Integer(values[0]));
return status;
}
return null;
}
@Override
public String convertToString(Map context, Object status) {
if (status instanceof Status) {
return ((Status)status).getId().toString();
}
return "";
}
}
This seems to work, but only the ID is passed to the converter, which
means I have to look up the reference to the Status object in the
database. This just feels as if I am going down the wrong path. Again,
below is my action. Thanks for any suggestions you might have.
Best Regards,
MG
...
public class CallAction extends BaseCallAction implements
ServletRequestAware, ModelDriven<Call>, Preparable {
/**
*
*/
private static final long serialVersionUID =
9001475066131203883L;
private Call Call;
private HttpServletRequest request;
CallService CallService;
UserService userService;
StatusService statusService;
Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getView() throws Exception{
return SUCCESS;
}
public String list() throws Exception {
List<Call> results= CallService.getCalls();
request.setAttribute(SEARCH_RESULTS, results);
return SUCCESS;
}
public String execute() throws Exception {
return SUCCESS;
}
public Call getModel() {
return Call;
}
public void prepare() throws Exception {
if(id == null || id.intValue() == 0){
PermissionedUser permissionedUser=
this.getAuthenticatedUser();
Call= new Call();
Call.setRequestor(permissionedUser.getUser());
}else{
Call= CallService.getCall(id);
}
request.getSession().setAttribute("Call", Call);
}
public String update() throws Exception{
CallService.update(Call);
return SUCCESS;
}
public void setServletRequest(HttpServletRequest request) {
this.request= request;
}
public void setCallService(CallService CallService) {
this.CallService = CallService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void setStatusService(StatusService statusService){
this.statusService= statusService;
}
public List<Status> getStatusList(){
return statusService.findAll();
}
}
-----Original Message-----
From: Martin Gainty [mailto:[EMAIL PROTECTED]
Sent: Friday, April 18, 2008 12:22 PM
To: Griffith, Michael *
Cc: Struts Users Mailing List
Subject: Re: Clarification on Type Conversion?
Good Afternoon Mike-
Tough to diagnose without seeing the service interface defined in your
action
e.g.
public class HHSAction implements Preparable {
private HHSService service;
}
package quickstart.service;
import java.util.List;
import quickstart.model.Person;
public interface HHSService
{
public List<Person> findAll();
public void save(Person person);
public Person find(int id);
}
?
Martin-
----- Original Message -----
From: "Griffith, Michael *" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <[email protected]>
Sent: Friday, April 18, 2008 10:33 AM
Subject: Clarification on Type Conversion?
Hello All,
I am having a problem with Struts 2 form submission, because I believe a
related <s:select> field is not being converted correctly. I have read
the documentation at: http://struts.apache.org/2.x/docs/select.html and
http://struts.apache.org/2.0.11.1/docs/type-conversion.html It is my
understanding that I should not need to provide any custom converter,
that this conversion should be automatic. When I submit the form, I
receive the error: Invalid field value for field "status".
Here is the setup:
I have a form with a <s:select> field:
<s:select key="form.status" name="status"
list="statusList"
listKey="id"
listValue="description"/>
The action has the method:
public List<Status> getStatusList(){
return statusService.findAll();
}
The model object that status is mapped to looks like this:
@Entity
@Table(name="STATUS")
@Validation
public class Status implements Identifiable, Serializable {
private static final long serialVersionUID =
-8017026685032314796L;
private Integer id;
private String description;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable= false)
public Integer getId() {
return id;
}
public void setId(Integer id){
this.id= id;
}
@Column(name="DESCRIPTION", nullable= false)
public String getDescription() {
return description;
}
@RequiredStringValidator(message="Validation Error",
key="validate.notEmpty", trim=true)
public void setDescription(String description) {
this.description = description;
}
I tried creating a mapping properties file called
actionName-conversion.properties:
KeyProperty_StatusList=id
Element_StatusList=Status
CreateIfNull_statusList=true
No avail. I'm sure my problem is obvious, but I can't see it. Any help
or suggestions would be much appreciated...
Thanks in advance!
Michael Griffith
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]