PDA

View Full Version : Bind, PropertyEditor and Map


kantorn
08-17-2004, 03:04 AM
After too many days sitting and trying in vain I'm now turning to the 'Wizards of Spring' with a hopeful mind.....

This is the situation:
I want to bind a DB-relation object in a form. Designed JavaBean style, nothing unusual.


public class Delegation implements Serializable, TernaryAssociation {

private Event event;
private Function delegate;
private Date until;
private DelegationLevel level;


public Delegation(Event event, Function delegate, Date until,
DelegationLevel level) {
this.event = event;
this.delegate = delegate;
this.until = until;
this.level = level;
}

[standard setters/getter]
}


A couple of these are bound to form object


public class DelegationForm {

private Function function; //The function of the current user
private Map delegations; //All the delegation node objects per function in the system

public DelegationForm(Function function){
this.function = function;
}

public DelegationForm(){}

public Function getFunction() { return (this.function); }
public void setFunction(Function function) { this.function = function; }

public Map getDelegations() { return (this.delegations); }
public void setDelegations(Map delegations) { this.delegations = delegations; }
}

Relevant JSP-code:


<c:forEach var="functionnaire" items="${functionnaires}">
<c:set var="fid" value="${functionnaire.function.id}"/>
<TR>
<spring:bind path="command.delegations[${fid}]">
<TD<b><c:out value="${functionnaire.function.description} ${functionnaire.user.fname} ${functionnaire.user.lname} "/></b></TD>
<c:forEach var="level" items="${levellist}">
<TD><input type="radio" name="<c:out value="${status.expression}"/>"
value="<c:out value="${status.value}"/>"
></TD>
</c:forEach>
</spring:bind>
</TR>
</c:forEach>


where 'functionnaires' is a list of objects containing a User and a Function, provided by controller function referenceData. 'command.delegations' is the form objects Map, which is preloaded with existing data from database.

What I cannot seem to accomplish is to get PropertyEditor



public class DelegationEditor extends PropertyEditorSupport {

private LogicFacade logic;

public static Category log = Category.getInstance(DelegationEditor.class.getNam e());

public LogicFacade getLogic() { return (this.logic); }
public void setLogic(LogicFacade logic) { this.logic = logic; }

public void setAsText(String text) {
log.debug("setAsText got: " + text);
Delegation status = null;
if(text.trim().equals("") || text == null){
this.setValue(null);
}
else{
try{
this.setValue(new Delegation(null, null, null, null));
}catch(Exception e){

}
}
}

public String getAsText(Delegation status) throws Exception {

log.debug("getAsText got: " + status);
if(status == null){
return "Hejsan";
}//if
else{
return status.getEvent().getId() + "@" + status.getDelegate().getId();
}//else
}
}


to print out 'Hejsan' or the wanted string 'xxx@xxx'. Instead it somehow uses Delegation's internal toString()-function, resulting in



<TD><input type="radio" name="delegations[5]" value="ks.rah.avik2.domain.Delegation@1644c9[event: =1,delegate: =5,level: =Inga rättigheter]" ></TD>


despite the fact that


protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
super.initBinder(request,binder);
binder.registerCustomEditor(Delegation.class, "delegations.*", delegEditor);
}


is invoked in the DelegationFormController class.

In short: I don't see any PropertyEditor impact on my presented HTML-page.

What am I doing wrong in binding here? Is invoking the contents of a Map in a Command object with 'mapname[keystring]' not doable? Any tips and tricks?

Thanks in advance,