0

我有 2 个组件(select 和 inputText),其中的值相互依赖。例如,如果选择“选项 1”,则 inputText 必须是数字。

在我的 bean 中,我添加了用于绑定和验证方法的 2 个组件的属性,而在 jsp 中,我添加了“验证器”和“绑定”属性来选择和“绑定”到 inputText。

我使用绑定来获取两个组件的提交值以进行验证。

这是正确的方法吗?是否有替代方法来获取提交的值?

这样做的结果是显示重复的消息。如果我从 select 中删除绑定属性,那么它会按预期工作,但我无法获取所选值,而是使用缓存值(会话中的 bean 值)。

提前致谢。

阿尔梅罗

代码:

<p:selectOneMenu
value="#  {deploymentRequestViewBean.deploymentRequestDTO.deploymentRequest.requestLevel}"
id="requestLevel" required="true" label="requestLevel" 
validator="#{deploymentRequestViewBean.validateRequestDate}">
<p:ajax listener="#{deploymentRequestViewBean.processRequestLevelValueChanged}" 
binding="#{deploymentRequestViewBean.requestLevelSelectOne}"/>
<f:selectItem itemValue="" itemLabel="Select One" />
<f:selectItem itemValue="DEV" itemLabel="DEV" />
<f:selectItem itemValue="QUA" itemLabel="QUA" />
<f:selectItem itemValue="PRD" itemLabel="PRD" />
</p:selectOneMenu>

 <p:calendar
 value="#{deploymentRequestViewBean.deploymentRequestDTO.deploymentRequest.deployDate}"
 id="deployDate" required="true" label="deployDate" showOn="button" pattern="yyyy-  MM-dd" binding="#{deploymentRequestViewBean.requestDateInput}"/>
<p:spacer width="10" height="10" /> 

//for component-binding
private UISelectOne requestLevelSelectOne;
private UIInput requestDateInput;


//validation method
public void validateRequestDate(FacesContext facesContext,
    UIComponent component, Object newValue){

//get the current value of select;
requestLevelSelectOne.getSubmittedValue();
//get the current vallue of input;
requestDateInput.getSubmittedValue()

if(not valid combination){
           facesContext.addMessage(requestDateInput.getClientId(facesContext), new  FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", selectedLevel + " deployment request requires at least 2 days."));
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Deployment date must be at least 2 days."));
}
}
4

1 回答 1

0

您可以通过将隐藏的组件值与选择组件绑定来使用 hack 绕过。在您的“onchange”方法中,您<h:selectOneMenu>可以设置此隐藏组件的值并获取服务器中的值:

<h:form id="myForm">
    <h:selectOneMenu id="cmbOptions"
        onchange="document.getElementById('myForm:hidSelectOption').value=this.value">
        <f:selectItem itemLabel="Option 1" itemValue="1" />
        <f:selectItem itemLabel="Option 2" itemValue="2" />
        <f:selectItem itemLabel="Option 3" itemValue="3" />
    </h:selectOneMenu>
    <h:inputHidden id="hidSelectOption" value="#{bean.selectedOption}" />
    <h:commandButton value="Click me" action="#{bean.someAction}" />
</h:form>

托管 Bean

@RequestScope
@ManagedBean
public class Bean {
    private String selectedOption;
    //getters and setters...
    public Bean() {
    }

    public void someAction() {
        //showing the actual value of the hidden component...
        //remember that you should use a logger, this is a basic example
        System.out.println(selectedOption);
    }
}
于 2012-07-26T20:43:28.060 回答