h:selectBooleanCheckbox总是将false传递给验证器

时间:2016-01-21 09:13:49

标签: jsf jsf-2

我有h:selectBooleanCheckbox但它始终将'false'传递给validator.

<h:panelGroup id="tncPanel">
 <label>
  <h:selectBooleanCheckbox id="tncSelection" value="#{businessBean.tncSelection}">
   <f:validator validatorId="checkboxValidator"/>
   <f:attribute name="label" value="Please agree terms and conditions."/>
  </h:selectBooleanCheckbox>
  I have read and agreed to all the
  <a href="#" data-toggle="modal" data-target="#modal-term-and-conditions">
     Terms and Conditions.
  </a>
 </label>
</h:panelGroup>

<h:panelGroup id="buttonPanel">
   <h:commandLink id="nextButton" action="#{businessBean.goNext}">
        Submit
   </h:commandLink>
</h:panelGroup>

为什么我panelGroup这里是基于页面顶部的逻辑,我有一个逻辑来显示/ buttoncheckbox

这是我的Validator

    @FacesValidator("checkboxValidator")
public class CheckboxValidator implements Validator {

    private static final String DEFAULT_LABEL = "Please confirm the address.";

    @Override
    public void validate(FacesContext context, UIComponent component, Object value){
        String checkBoxValue = String.valueOf(((HtmlSelectBooleanCheckbox) component).getSubmittedValue());

        System.out.println("checkBoxValue " + checkBoxValue);
        System.out.println("value2: " + value);   

        String label = String.valueOf(component.getAttributes().get("label"));
        if ("null".equals(label)) {
            label = DEFAULT_LABEL;
        }
        if("false".equalsIgnoreCase(checkBoxValue)){
            FacesMessage msg = new FacesMessage(null, label);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }
}

验证程序sysout始终打印checkBoxValue false

更新

在对Balusc发表评论后,我添加了另一个sysout来直接打印参数value。但它仍然以value2: false打印。

1 个答案:

答案 0 :(得分:1)

有一个与这种情况有关的错误,当你渲染一个容器从另一个表单中保存一个from时,你渲染的表单会丢失它的视图状态,因此表单被提交给没有目标解决方案就是渲染表单容器和表单来自其他形式的自我

例如,这不起作用:

secondForm

第二次表单提交将是这样的:

enter image description here

javax.faces.ViewState将不提供其视图状态表单已提交,但服务器上没有目标,因此未在需要f:ajax值获取的<f:ajax render=":panel :secondForm" listener="#{testAction.submit()}"/> 值的服务器上获取视图对象你必须在这里做的是在javax.faces.ViewState

中渲染表格
function getFtRSS(){
    //Create a object to parse XML input from Finanial Time's UK companies RSS feed
    $rssFeed = new DOMdocument();
    //Gather the information and load it into the object
    $rssFeed->load('http://www.ft.com/rss/companies/uk');
    $articles = array();
    //Lookp through all elements in the XML document
    foreach($rssFeed->getElementsByTagName('item') as $newsArtcle){
        $title = $newsArtcle->getElementsByTagName('title')->item(0)->nodeValue;
        $desc = $node->getElementsByTagName('description')->item(0)->nodeValue;
        $link = $node->getElementsByTagName('link')->item(0)->nodeValue;
        $date = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;

        echo   '<li class="list-group-item news-item"  onclick="window.location=\'$link\'">
                    <h3 class=\'top-element\'>$title</h3>
                    <h4>$desc</h4>
                    <h5>$date</h5>
                </li>';
    }
}

通知服务器备份使用Warning: DOMDocument::load(http://www.ft.com/rss/companies/uk): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /home/ubuntu/workspace/MOT/includes/functions.php on line 46

呈现的第二个表单

请参阅该帖子:Rendering other form by ajax causes its view state to be lost, how do I add this back?了解更多信息

希望这是问题:)