我正在设置一个primefaces选项列表,源列表和目标列表保持空白。
page.xhtml
<h:form>
<p:pickList id="picklist" value="#{bean.users}" var="user"
itemValue="#{user}" itemLabel="#{user.login}" converter="userConverter">
<f:facet name="sourceCaption">Source</f:facet>
<f:facet name="targetCaption">Target</f:facet>
<p:ajax event="transfer" listener="#{bean.onTransfer}" />
<p:column style="width:75%;">
<h:outputText value="#{user.login}" />
</p:column>
</p:pickList>
<h:commandButton id="submit" type="submit" actionListener="#{bean.submit}" value="Submit" />
<h:commandButton id="display" actionListener="#{bean.displayTargetList}" value="Display Target List" />
</form>
bean.java
@ManagedBean(name = "bean")
@ViewScoped
public class Bean {
private List<User> source;
private List<User> target;
private DualListModel<User> users;
public void setTarget(List<User> list) {
this.target = list;
}
public void setSource(List<User> list) {
this.source = list;
}
public void setUsers(DualListModel<User> list) {
this.users = list;
}
public DualListModel<User> getUsers() {
return this.users;
}
public List<User> getSource() {
return this.source;
}
public List<User> getTarget() {
return this.target;
}
@PostConstruct
public void init() {
this.source = myController.getUsers(); // return all my users in database
this.target = new ArrayList<User>();
this.users = new DualListModel<User>(this.source, this.target);
}
public void submit() {
}
public void displayTargetList() {
System.out.println("Entering function");
Iterator<User> iterator = this.target.iterator();
while(iterator.hasNext()) {
User u = iterator.next();
System.out.println(u.getLogin());
}
}
}
还有转换器
userConverter.java
@FacesConverter("userConverter")
public class UserConverter implements Converter{
private List<User> users;
public UserConverter(){
this.users = MyController.getAllUsers();
}
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return this.getUserById(value);
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
return ((User) value).getId();
}
public User getUserById(String id) {
// return the user searching from this.users
}
}
我的代码有点复杂,但我希望错误在这里。对我来说,它似乎像primefaces展示实现。我可以补充一点,转换器中的方法getAsObject返回一个对象(我在返回值之前尝试打印并且对象不为null)
如果可以提供帮助。 感谢