只需让required
属性评估true
提交按钮何时真正被按下。
然而,答案取决于您的提交按钮如何执行其逻辑(标准f:ajax
、、ICEfaces 等)。但它基本上归结为您可以检查请求参数映射中的请求参数,该参数指示已按下所需的提交按钮。
例如,如果它是标准命令按钮:
<h:form id="form">
...
<h:commandButton id="submit" value="Submit" action="#{bean.submit}" />
</h:form>
然后您可以通过检查按钮的客户端 ID 是否存在于请求参数映射中来检查它:
<c:set var="submitButtonPressed" value="#{not empty param['form:submit']}" />
...
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
或者,如果它是一个<f:ajax>
按钮:
<h:form id="form">
...
<h:commandButton id="submit" value="Submit" action="#{bean.submit}">
<f:ajax execute="@form" ... />
</h:commandButton>
</h:form>
然后您可以通过检查javax.faces.source
参数是否等于按钮的客户端 ID 来检查它:
<c:set var="submitButtonPressed" value="#{param['javax.faces.source'] == 'form:submit'}" />
...
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
您甚至可以将两者结合起来:
<c:set var="submitButtonPressed" value="#{not empty param['form:submit'] or param['javax.faces.source'] == 'form:submit'}" />
...
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />