Ajax验证阻止页面导航

时间:2019-05-03 16:30:42

标签: html ajax java-ee xhtml

我使用命令链接来调用Bean来更改显示的页面,用于提交输入数据的命令按钮应该直到所有字段都正确输入后才允许提交页面。但是命令链接充当命令按钮,并且在正确输入字段之前不允许切换页面。

具有required="true"的作用相同,命​​令链接在字段正确之前不起作用。我不确定命令链接或命令按钮中需要更改的内容,以便在未提交命令按钮时切换页面。

切换页面的命令链接:

<h:commandLink title="UNITS"
  styleClass="#{ tp.buttonStyle }"
  actionListener="#{ tp.otherPageAction }">
    Link Text
</h:commandLink>

输入文字:

<h:inputText id="unit" 
value="#{ unitBean.value }" 
validator="#{ unitBean.valueValidator }">
    <f:ajax event="blur" render="valueError" />
</h:inputText>

<h:message id="valueError" for="unit" style="color: red;" />

验证者:

public void valueValidator(FacesContext fc, UIComponent c, Object value)
{
    if( value == null)
    {
        throw new ValidatorException(new FacesMessage("Value can not be empty"));
    }
}

命令按钮提交页面:

<h:commandButton value="Insert Values" 
action="#{ unitBean.insertValues }" 
actionListener="#{ tp.otherPageAction2 }" />

命令链接应该切换页面,但验证不允许页面切换。

0 个答案:

没有答案