PrimeFaces 6.1。 我有一个带字母的SelectOneListBox。更改后,将触发ajax事件并使用以所选字母开头的单词填充PickList。如果PickList包含单词XX,则其有效,其中X是所选字母。
如果PickList有效,一切正常。如果它失效,一切正常(ajax被触发,消息被重新呈现等),只有PickList在字母改变时不刷新,除非它再次有效。
有没有办法重新呈现PickList,即使它无效?
代码:Web.xhtml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
<f:view>
<h:form>
<p:selectOneListbox value="#{Web.letter}" required="true">
<f:selectItems value="#{Web.letters}" var="letter"
itemValue="#{letter}" />
<p:ajax event="change" update="words"
listener="#{Web.letterChanged}" />
</p:selectOneListbox>
<h:panelGrid columns="2" id="words">
<p:pickList id="pickList" value="#{Web.words}" var="word"
itemLabel="#{word}" itemValue="#{word}"
validator="#{Web.validateWords}" showSourceFilter="true"
filterMatchMode="startsWith" showTargetControls="true">
<p:ajax event="reorder" update="pickListMessages" />
<p:ajax event="transfer" update="pickListMessages" />
</p:pickList>
<p:message id="pickListMessages" for="pickList" />
</h:panelGrid>
</h:form>
</f:view>
</h:body>
</html>
Web.java:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.validator.ValidatorException;
import org.primefaces.model.DualListModel;
@ManagedBean (name = "Web")
@ViewScoped
@SuppressWarnings (
{ "javadoc", "nls", "unused" })
public class WebBean implements Serializable
{
private static final long serialVersionUID = 4067529596686991391L;
private Set<String> letters = new TreeSet<> (Arrays.asList ("A", "B", "C"));
private String letter = "A";
private DualListModel<String> words = new DualListModel<> (new ArrayList<> (),
new ArrayList<> ());
public Set<String> getLetters ()
{
return letters;
}
public String getLetter ()
{
return letter;
}
public void setLetter (String letter)
{
this.letter = letter;
}
public DualListModel<String> getWords ()
{
return words;
}
public void setWords (DualListModel<String> words)
{
this.words = words;
}
private void refreshWordsList ()
{
List<String> source = words.getSource ();
source.clear ();
source.addAll (Arrays.asList (letter + 'A', letter + 'B', letter + 'C'));
words.getTarget ().clear ();
}
public void letterChanged (AjaxBehaviorEvent ajaxEvent)
{
refreshWordsList ();
}
public void validateWords (FacesContext ctx, UIComponent component,
Object value) throws ValidatorException
{
@SuppressWarnings ("unchecked")
DualListModel<String> dualListModel = (DualListModel<String>) value;
List<String> validatedWords = dualListModel.getTarget ();
for (String word : validatedWords)
{
// OK?
if (word.charAt (0) == word.charAt (1))
{
return;
}
}
throw new ValidatorException (
new FacesMessage (FacesMessage.SEVERITY_ERROR, "No double", null));
}
}