在使用使用POJO列表的PrimeFaces组件时,如何编写自定义转换器?我的特殊问题是<p:pickList>
<p:pickList converter="????" value="#{bean.projects}" var="project"
itemLabel="#{project.name}" itemValue="#{project}">
如果没有转换器,我会得到java.lang.ClassCastException
因为JSF使用未转换的java.lang.String
提交的值设置提交的值。
答案 0 :(得分:38)
有可能,没有其他数据库访问,但我不知道最好的方法。我使用非常特殊的转换器,仅适用于选项列表。试试这个:
@FacesConverter(value = "primeFacesPickListConverter")public class PrimeFacesPickListConverter implements Converter {
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
Object ret = null;
if (arg1 instanceof PickList) {
Object dualList = ((PickList) arg1).getValue();
DualListModel dl = (DualListModel) dualList;
for (Object o : dl.getSource()) {
String id = "" + ((Project) o).getId();
if (arg2.equals(id)) {
ret = o;
break;
}
}
if (ret == null)
for (Object o : dl.getTarget()) {
String id = "" + ((Project) o).getId();
if (arg2.equals(id)) {
ret = o;
break;
}
}
}
return ret;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
String str = "";
if (arg2 instanceof Project) {
str = "" + ((Project) arg2).getId();
}
return str;
}
和领料单:
<p:pickList converter="primeFacesPickListConverter" value="#{bean.projects}" var="project"
itemLabel="#{project.name}" itemValue="#{project}">
为我工作,必须进行改进。
答案 1 :(得分:18)
在研究如何编写自定义转换器之后,这是解决方案
1.创建一个实现javax.faces.convert.Converter;
public class ProjectConverter implements Converter{
@EJB
DocumentSBean sBean;
public ProjectConverter(){
}
public Object getAsObject(FacesContext context, UIComponent component, String value){
return sBean.getProjectById(value);
//If u look below, I convert the object into a unique string, which is its id.
//Therefore, I just need to write a method that query the object back from the
//database if given a id. getProjectById, is a method inside my Session Bean that
//does what I just described
}
public String getAsString(FacesContext context, UIComponent component, Object value)
{
return ((Project) value).getId().toString(); //--> convert to a unique string.
}
}
2。在faces-config.xml
<converter>
<converter-id>projectConverter</converter-id>
<converter-class>org.xdrawing.converter.ProjectConverter</converter-class>
</converter>
3。所以现在在Primefaces组件中,你只需converter="projectConverter"
。请注意,projectConverter
是我刚刚创建的<convert-id>
。所以为了解决上面的问题,我这样做:
<p:pickList converter="projectConverter" value="#{bean.projects}" var="project"
itemLabel="#{project.name}" itemValue="#{project}">
答案 2 :(得分:5)
是的,您可以编写一个转换器来序列化/反序列化选项列表中的对象,如下所示:
@FacesConverter(value="PositionMetricConverter")
public class PositionMetricConverter implements Converter {
private static final Logger log = Logger.getLogger(PositionMetricConverter.class.getName());
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
try {
byte[] data = Base64.decodeBase64(value);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
Object o = ois.readObject();
ois.close();
return o;
} catch (Exception e) {
log.log(Level.SEVERE, "Unable to decode PositionMetric!", e);
return null;
}
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(value);
oos.close();
return Base64.encodeBase64String(baos.toByteArray());
} catch (IOException e) {
log.log(Level.SEVERE, "Unable to encode PositionMetric!", e);
return "";
}
}
}
然后将此转换器应用于您的选项列表,如下所示:
<p:pickList converter="PositionMetricConverter" value="#{bean.positionMetrics}"
var="positionMetric" itemLabel="#{positionMetric.name}" itemValue="#{positionMetric}"/>
并确保您的对象可序列化。
答案 3 :(得分:3)
这个问题与primefaces无关,只是一般的JSF相关。
为什么要再次访问数据库?您的bean已包含要在组件中显示的对象列表,还是请求作用域?
如果您通过bean中的get属性访问最终列表,或者您可以选择转换器中的嵌套属性。
public class SuperPojo
{
protected Integer id;
//constructor & getter
}
public class PojoTest extends SuperPojo
{
private String label = null;
//constructor & getter
}
public class SuperPojoConverter<T extends SuperPojo> implements Converter
{
private Collection<T> superPojos;
public IdEntityConverter(Collection<T> superPojos)
{
this.superPojos = superPojos;
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
//catch exceptions and empty or null value!
final int intValue = Integer.parseInt(value);
for(SuperPojo superPojo : this.superPojos)
if(superPojo.getId().intValue() == intValue)
return superPojo;
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value)
{
//catch null and instanceof
return String.valueOf(((SuperPojo)value).getId().intValue());
}
public Collection<T> getSuperPojos()
{
return this.superPojos;
}
}
public class Bean
{
private SuperPojoConverter<PojoTest> pojoTestConverter = null;
public Bean()
{
final List<PojoTest> pojoTests = //get list from hibernate
this.pojoTestConverter = new SuperPojoConverter<PojoTest>(pojoTests);
}
public SuperPojoConverter<PojoTest> getPojoTestConverter()
{
return this.pojoTestConverter;
}
}
<h:selectOneMenu value="#{selPojoTest}" converter="#{bean.getPojoTestConverter}">
<f:selectItems value="#{bean.getPojoTestConverter.getSuperPojos}" var="varPojoTest" itemLabel="#{varPojoTest.label}" itemValue="#{varPojoTest}"/>
</h:selectOneMenu>
答案 4 :(得分:2)
有没有办法在没有2个数据库命中的情况下实现它?
我的意思是,当你有
时#{bean.projects}
这是一个数据库命中。
当转换器放置
时sBean.getProjectById(value);
是一个不必要的数据库命中,因为bean.projects已经具有对象的id和值
答案 5 :(得分:1)
是的,可能:
public class DocumentSBean sBean implements Serializable{
private List<Document> projects;
// projects methods...
// ...
public Converter getDocumentConverter(){
return docConverter;
}
private Converter docConverter = new Converter() {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return projects.stream().filter(p -> p.getName().equals(value)).findFirst().orElse(null);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return (value != null)
? ((Document) value).toString()
: null;
}
};
}
<p:pickList converter="#{sBean.documentConverter}" value="#...