ok It's working.
I spend some time on this because save doesn't work. It was due to a bad
configuration of my hibernate annotation :-! :
--------------------------------------
@Entity
public class Entreprise implements java.io.Serializable {
...
@ManyToOne
@JoinColumn(name="USER_ID", insertable=false, updatable=false) // ->
insertable=true, updatable=true
public User getDirecteur() {
return directeur;
}
...
}
--------------------------------------
My code if someone have the same problem :
the controller :
--------------------------------------
public class EntrepriseFormController extends BaseFormController {
...
@Override
@SuppressWarnings("unchecked")
protected Map referenceData(HttpServletRequest request) throws
Exception {
log.debug("entering 'refenceData' function ..." );
Map model = new HashMap();
List<User> directeurs = getUserManager().getUsers(new User());
model.put( "directeurs", directeurs );
return model;
}
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) {
super.initBinder(request, binder);
binder.registerCustomEditor( User.class, new
UserEditor(getUserManager()) );
}
...
}
--------------------------------------
UserEditor
--------------------------------------
package webapp.binder;
import java.beans.PropertyEditorSupport;
import org.appfuse.model.User;
import org.appfuse.service.UserManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @author Julien Chanséaume
*/
public class UserEditor extends PropertyEditorSupport {
private final UserManager userManager;
protected final transient Log log = LogFactory.getLog(getClass());
public UserEditor(UserManager userManager) throws
IllegalArgumentException {
this.userManager = userManager;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
log.debug("entering UserEditor.setAsText function : " + text );
Long id = null;
User user = null;
if (text != null && text.length() > 0) {
try {
id = new Long(text); // new Integer(text);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("wrong argument:
id="+text, nfe);
}
user = userManager.getUser(id.toString());
/*
if (user == null) log.warn("user.getUser(" + text + ") retrn
null value");
else log.debug("user has been find : " + user.toString());
*/
}
this.setValue(user);
}
/**
* Format the Number as String, using the specified NumberFormat.
*/
public String getAsText() {
return getValue().toString();
}
}
--------------------------------------
my jsp
--------------------------------------
...
<form:form commandName="entreprise" method="post"
action="entrepriseform.html" id="entrepriseForm" onsubmit="return
validateEntreprise(this)">
...
<li>
<!-- todo: change this to read the identifier field from the
other pojo -->
<form:select path="directeur" itemValue="id"
itemLabel="lastName" items="${directeurs}" ></form:select>
<p><appfuse:label key="entreprise.directeur"/></p>
</li>
...
</form:form>
...
--------------------------------------
I don't think it's perfect but it's working.
Julien Chanséaume.
Julien C. a écrit :
Thank you Michael. I will try this.
Julien C.
Michael Horwitz a écrit :
Hi,
You will need to implement and register a custom property editor for
you Directeur class which Spring will use to convert the class to and
from String form. To do this:
1) Create a class (I often do this as a nested class on the
controller) that extends PropertyEditorSupport. You will need to
override the setAsText() and getAsText() methods. Please be sure to
include null value handling here.
2) Register your new class on your controller using the initBinder()
method.
If you need more detail do a search for PropertyEditorSupport on the
user list in Nabble - something should pop up!
Answers to your other questions below:
On 7/25/07, *Julien C.* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:
Hi,
I want to add a drop down list of user in a page (Spring MVC) :
so I implement the referenceData of my FormController :
@Override
@SuppressWarnings("unchecked")
protected Map referenceData(HttpServletRequest request) throws
Exception {
log.debug("entering 'refenceData' function ..." );
// return super.referenceData(request);
Map model = new HashMap();
List<User> directeurs = getUserManager().getUsers(new User());
model.put( "directeurs", directeurs );
return model;
}
in my jsp :
<form:select path="directeur" itemValue="id" itemLabel="lastName"
items="${directeurs}" ></form:select>
the data appears correctly on my page but when I save my form
I've got
an error ( a other one;-) ) :
-----------------------
Failed to convert property value of type [java.lang.String] to
required
type [org.appfuse.model.User] for property directeur; nested
exception
is java.lang.IllegalArgumentException : Cannot convert value of type
[java.lang.String] to required type [org.appfuse.model.User] for
property directeur: no matching editors or conversion strategy found
-----------------------
I don't know how to correct the problem ( where and how do I make
the
conversion ).
I have 2 small other questions :
- Is it possible to show in the list a label like surname +
lastname ?
Yes - just implement a method on your class that returns the label in
the correct format and set the itemLabel attribute accordingly.
- I have seen a <s:select tag in my generated controller which
come from
Struts (I think). Is there a reason . better than Spring select
tag ?
Not sure on this one. The Spring tag is likely to work better with a
Spring controller due to binding paths.
Mike
Thank you. Julien C.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]