Property Editors is the way I've always dealt with this issue. I am
not sure whether its best practise or not, but it works. I personally
do find having to create a PE each time laboursome though. This has to
be one big gripe I've had with creating forms with Spring MVC - I
think a simple many-to-one mapping, with a drop-down select list is
really fundamental functionality in a web application, and its
something that most agile frameworks can do really easily. This
doesn't seem to be a very DRY approach.
Alex
On 16 Dec 2007, at 12:42, Łukasz Bachman wrote:
Dear Alejandro,
thanks for a reply! You were right, Property Editors are exactly the
things that I am looking for. But I have encountered one error while
handling my "comment form".
This is my property editor for Type class:
public class TypeEditor extends PropertyEditorSupport {
private TypeManager typeManager = null;
public TypeEditor(TypeManager typeManager) {
this.typeManager = typeManager;
}
public void setAsText(String id) {
if (!StringUtils.isEmpty(id)) {
Type type = typeManager.get(Long.parseLong(id));
setValue(type);
}
}
public String getAsText() {
Type type = (Type) getValue();
if (type != null)
return (type.getValue() == null ? "" : type.getValue());
else
return "";
}
}
also, it is correctly (I hope ;-) ) initialized insisde InitBinder
method in my form controller:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) {
binder.registerCustomEditor(Type.class, new
TypeEditor(typeManager));
}
Everything works fine, when I try to add a new Comment. But when I
click on one of comments listed in the "comments.html" page which has
a "type_id" column not equal to NULL, my form is not displayed at all.
I can see just a blank page.
Moreover, I cannot find any error message in console. Just a warning
message:
WARN [btpool0-1] LoadContexts.cleanup(108) | fail-safe cleanup
(collections) : org.hibernate.engine.lo
ading
.CollectionLoadContext
@105013c<[EMAIL PROTECTED]>
Since this is my first attempt to create Property Editor, I don't have
any ideas where the eror might be. Any ideas?
Best regards,
Łukasz
16-12-07, Alejandro Castro <[EMAIL PROTECTED]>
napisał(a):
Lukasz,
I think what you need here is to crear a property editor for your
Type
class.
you will then use "type" as your path instead of "type.id" since
what you
are actually setting is a type.
your property editor will now how to set a type based on its id.
you might want to check out this link for further information on
property
editos with spring mvc
http://static.springframework.org/spring/docs/2.5.x/reference/validation.html
hope this helps
Alejandro
On Dec 15, 2007 3:21 PM, Łukasz Bachman < [EMAIL PROTECTED]>
wrote:
Dear AppFuse users,
well, I figured out that the best way is to use Spring's
<form:select
...> tag, but I have following error when I try to display my form:
... ERROR [btpool0-1] SelectTag.doStartTag(84) | Invalid property
'type' of bean class [...Comment]: Value of nested property 'type'
is
null
org.springframework.beans.NullValueInNestedPathException:
Invalid
property 'type' of bean class [...Comment]: Value of nested property
'type' is null
This is my JSP page:
<li>
<appfuse:label styleClass="desc" key="label.type"/>
<form:errors path="type" cssClass="fieldError"/>
<form:select path=" type.id">
<form:options items="${CommentTypesList}"
itemValue="id"
itemLabel="value"/>
</form:select>
</li>
I've managed to figure out, that error lies in "path" attribute of
<form:select> element. I think that "type.id" is proper value,
because
that is what I want to set up in Controller, right? But when I've
changed it to "type" (like in example from Spring reference) the
page
was displayed properly, but I got binding errors after form
submission.
What am I doing wrong?
Best regards,
Łukasz Bachman
---------- Forwarded message ----------
From: Łukasz Bachman <[EMAIL PROTECTED] >
Date: 14-12-2007 17:23
Subject: Custom binding of SELECT element
To: [email protected]
Dear AppFuse users,
I was wondering which is the most elegant way of handling HTML's
"SELECT - OPTION" elements? Normally, I was fetching all the form
fields using "request.getParameter()" method, but I find it very
inelegant and it does not help in maintaining code. So, I'm very
curious how are You guys doing it? Let's sketch an example:
Model classes (I got rid of the Hibernate annotations):
public class Comment extends BaseObject {
....
private Type typeOfComment;
....
}
public class Type extends BaseObject {
private Long id;
private String tag; // 'Type' entity will
hold types for different elements
private String value; // The name of the type
....
}
JSP:
( CommentTypesList is of course the list of Types elements)
<select name="type">
<c:forEach var="type" items="${CommentTypesList}">
<option value="<c:out value="${type.id}"/>"><c:out
value="${type.value}"/></option>
</c:forEach>
</select>
And now, the controller:
1) I have overrided "showForm(...)" method which gets
"CommentTypesList" and sends it to the view.
2) If I leave everything as it is in Tutorial's example, I'll get
parsing error because I'm passing types ID.
So I have following questions:
Q1) Which is the best way to avoid that?
Q2) Is there any way to create custom binding rules in pretty
elegant
manner?
Q3) Do You know of any tags that support displaying "SELECT -
OPTION"
elements using custom classes (such as "Type" in my particular
example)?
Best regards,
Łukasz Bachman
P.S. Hope this post is not too long or has inappropriate form. If
that's true - let me know.
P.P.S. I'm using Appfuse 2.0.x, Hibernate + Spring MVC framework
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]