问题描述
我想知道如何在类似于Spring集合属性编辑器的Java Server Faces中使用转换器
假设下面的模型
public class Group {
private String name;
List< User> users = new ArrayList< User>();
// getter和setter的
}
< form ...>
< h1>分组表单< / h1>
< label for =name>输入名称< / label>
< input type =textname =name/>
< label for =users>选择用户< / label>
<! - value属性存储userId - >
< input type =checkboxvalue =1name =users/> User 1
< input type =checkboxvalue =2name =users/>用户2
< input type =checkboxvalue =3name =users/>用户3
< / form>
如果我使用Spring绑定Group类中的用户属性,我调用
binder.registerCustomEditor(List.class,new CustomCollectionEditor(){
protected Object convertElement(Object userId){
return new User (Integer)userId);
}
});
如何在使用Java Server Faces时获得相同的效果?
您可以实现。它的API是相当自解释:相应地写入 getAsString()
方法,它返回 String
code> Object ,其可以在每个技术ID下,例如 userId
。然后,要在应用请求参数阶段期间让JSF设置正确的 Object
,您需要实现 getAsObject()
返回与给定的 String
值相关联的 Object
。
基本上:
public class UserConverter implements Converter {
private UserDAO userDAO = SomeDAOManager.getUserDAO();
public String getAsString(FacesContext context,UIComponent component,Object value){
return String.valueOf(((User)value).getId());
}
public Object getAsObject(FacesContext context,UIComponent component,String value){
return userDAO.find(Long.valueOf(value));
}
}
在 faces-config.xml
,如下所示:
< converter&
< converter-for-class> com.example.model.User< / converter-for-class>
< converter-class> com.example.converter.UserConverter< / converter-class>
< / converter>
这应该是。要了解更多信息,您可以或文章有用。
I would like to know how to use Converters in Java Server Faces similar to Spring collection property editor
Suppose the following model
public class Group {
private String name;
List<User> users = new ArrayList<User>();
// getter's and setter's
}
And equivalent form
<form ...>
<h1>Group form</h1>
<label for="name">Enter name</label>
<input type="text" name="name"/>
<label for="users">Select users</label>
<!--value attribute stores userId-->
<input type="checkbox" value="1" name="users"/> User 1
<input type="checkbox" value="2" name="users"/> User 2
<input type="checkbox" value="3" name="users"/> User 3
</form>
If i use Spring to bind users property in Group class, i call
binder.registerCustomEditor(List.class, new CustomCollectionEditor() {
protected Object convertElement(Object userId) {
return new User((Integer) userId);
}
});
How do i get the same effect when using Java Server Faces ?
regards,
For that you can implement javax.faces.convert.Converter
. Its API is pretty self-explaining: write the getAsString()
method accordingly that it returns the String
representation of the Object
, which can be under each the technical ID such as userId
. Then, to get JSF set the right Object
during apply request parameters phase, you need to implement getAsObject()
that it returns the Object
associated with the given String
value.
Basically:
public class UserConverter implements Converter {
private UserDAO userDAO = SomeDAOManager.getUserDAO();
public String getAsString(FacesContext context, UIComponent component, Object value) {
return String.valueOf(((User) value).getId());
}
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return userDAO.find(Long.valueOf(value));
}
}
Register it in faces-config.xml
as follows:
<converter>
<converter-for-class>com.example.model.User</converter-for-class>
<converter-class>com.example.converter.UserConverter</converter-class>
</converter>
That should be it. For more insights you may this or this article useful.
这篇关于使用JSF时的等效Spring自定义集合属性编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!