验证错误发生后,如何使用PrimeFacesAjax填充文本字段?我在视图中有一个表单,它为自动完成和GMAP本地化执行Ajax部分处理。我的后台bean实例化了一个实体对象“Address”,并将表单的输入引用到这个对象:@ManagedBean(name="mybean")@SessionScopedpublic class Mybean implements Serializable {
private Address address;
private String fullAddress;
private String center = "0,0";
....
public mybean() {
address = new Address();
}
...
public void handleAddressChange() {
String c = "";
c = (address.getAddressLine1() != null) { c += address.getAddressLine1(); }
c = (address.getAddressLine2() != null) { c += ", " + address.getAddressLine2(); }
c = (address.getCity() != null) { c += ", " + address.getCity(); }
c = (address.getState() != null) { c += ", " + address.getState(); }
fullAddress = c;
addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Full Address", fullAddress));
try {
geocodeAddress(fullAddress);
} catch (MalformedURLException ex) {
Logger.getLogger(Mybean.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Mybean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Mybean.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(Mybean.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(Mybean.class.getName()).log(Level.SEVERE, null, ex);
} catch (XPathExpressionException ex) {
Logger.getLogger(Mybean.class.getName()).log(Level.SEVERE, null, ex);
}
}在我在提交时处理整个表单之前,自动完成和映射Ajax请求可以很好地工作。如果验证失败,Ajax仍然工作正常,除非字段Full Address无法在视图中更新,即使在Ajax请求之后在后台bean上正确设置了它的值。如果刷新页面,验证错误消息就会消失,Ajax将按预期完成FullAddress字段。在验证过程中还会发生另一个奇怪的行为:我已经禁用了表单字段的bean验证,如代码中所示。这个工作正常,直到找到其他验证错误,那么,如果我重新提交表单,JSF将对此字段进行bean验证!我想我在验证状态中遗漏了一些东西,但是我不知道它有什么问题。有人知道如何调试JSF生命周期吗?有什么想法吗?
3 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
您还可以添加一个可重用的监听器来清除所有输入值,例如:
public class CleanLocalValuesListener implements ActionListener {@Overridepublic void processAction(ActionEvent actionEvent) throws AbortProcessingException { FacesContext context = FacesContext.getCurrentInstance(); UIViewRoot viewRoot = context.getViewRoot(); List<UIComponent> children = viewRoot.getChildren(); resetInputValues(children);}private void resetInputValues(List<UIComponent> children) { for (UIComponent component : children) { if (component.getChildCount() > 0) { resetInputValues(component.getChildren()); } else { if (component instanceof EditableValueHolder) { EditableValueHolder input = (EditableValueHolder) component; input.resetValue(); } } } }}
并在需要清理本地值时使用它:
<f:actionListener type="com.cacib.bean.CleanLocalValuesListener"/>
添加回答
举报
0/150
提交
取消