【发布时间】:2015-03-17 20:41:55
【问题描述】:
TL;DR
<h:form>
<p:inputText required="true">
<p:ajax event="click" immediate="true" process="@this" update="@this" />
</p:inputText>
</h:form>
点击会导致验证,即使 ajax 是即时的。为什么?
我做不到终于成功了:
<h:form>
<p:dataGrid var="elem" value="#{immediateTestBean.elements}" columns="3">
<f:facet name="footer">
<p:commandButton action="#{immediateTestBean.newElement}" immediate="true"
process="@namingcontainer" update="@namingcontainer" value="#{bundle.add}" />
</f:facet>
<h:panelGrid columns="3">
<p:selectOneMenu value="#{elem.type}">
<p:ajax immediate="true" process="@form" update="@form" />
<f:selectItem itemValue="text" itemLabel="#{bundle.text}" />
<f:selectItem itemValue="date" itemLabel="#{bundle.date}" />
</p:selectOneMenu>
<h:panelGroup id="detail">
<p:inputText value="#{elem.text}" required="true" rendered="#{elem.type == 'text'}"
validator="#{immediateTestBean.testValidate}" label="#{bundle.text}">
<f:validateLength minimum="4" />
</p:inputText>
<p:calendar value="#{elem.date}" required="true" rendered="#{elem.type == 'date'}"
pattern="dd/MM/yyyy" readonlyInput="true" showOn="button"
validator="#{immediateTestBean.testValidate}" label="#{bundle.date}" />
</h:panelGroup>
<p:commandButton action="#{immediateTestBean.removeElement(elem)}" immediate="true"
process="@namingcontainer" update="@namingcontainer" value="#{bundle.remove}" />
</h:panelGrid>
</p:dataGrid>
<p:spacer height="10" style="display: block" />
<p:commandButton process="@form" update="@form" value="#{bundle.submit}" />
</h:form>
一切正常,除了<p:ajax immediate="true" process="@form" update="@form" /> 的行为不像UICommands 那样:验证发生(但在APPLY_REQUEST_VALUES 阶段)。
我试图挖掘 JSF 源代码并注意到 UICommands 调用 facesContext.renderResponse() 默认为 ActionListenerImpl.processAction(),所以我尝试了:
<p:ajax listener="#{facesContext.renderResponse}" immediate="true" process="@form" update="@form" />
现在,当我选择“日期”时,会跳过验证,但不会更新 <h:panelGroup id="detail">。
然后,我认为跳过验证也会导致更新模型的跳过,所以我打电话:
<p:ajax listener="#{immediateTestBean.skip}" immediate="true" process="@form" update="@form" />
终于成功了。
但是出现了两个问题:
- 为什么这么复杂?
- 是否有更简单的方法来实现相同的行为?
但是,这里是 bean 的代码:
@ManagedBean
@ViewScoped
public class ImmediateTestBean implements Serializable
{
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(ImmediateTestBean.class);
public class Element
{
private String type = "text";
private String text;
private Date date;
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}
private final List<Element> elements = new ArrayList<>();
public void testValidate(FacesContext context, UIComponent component, Object value)
{
logger.debug("phase: {}", context.getCurrentPhaseId());
}
public void skip()
{
logger.debug("Faces.getCurrentPhaseId(): {}", Faces.getCurrentPhaseId());
UIInput component = (UIInput) Components.getCurrentComponent();
logger.debug("component: {}", component);
FacesContext context = Faces.getContext();
component.validate(context);
if(component.isValid())
{
component.updateModel(context);
}
Faces.renderResponse();
}
public void newElement()
{
elements.add(new Element());
}
public void removeElement(Element elem)
{
elements.remove(elem);
}
public List<Element> getElements()
{
return elements;
}
}
【问题讨论】:
-
我将不胜感激对否决票的解释...