ManagedBean

时间:2015-07-01 09:15:24

标签: jsf jsf-2 toggle selectbooleancheckbox

我的jsf页面中有两个布尔类型复选框,根据第一个复选框状态禁用第二个复选框,第一次加载页面时,取消选中复选框。当我选中第一个复选框时,我通过脚本更新第二个复选框禁用状态。然后我选择第二个复选框。        但问题是当我提交我的页面时,只有第一个复选框状态正在更新。这是我的Managed Bean

@ManagedBean(name = "checkBoxTest")
@SessionScoped
public class CheckBoxTest {
    private boolean check1;
    private boolean check2;
    public boolean isCheck1() {
        return check1;
    }
    public void setCheck1(boolean check1) {
        this.check1 = check1;
    }
    public boolean isCheck2() {
        return check2;
    }
    public void setCheck2(boolean check2) {
        this.check2 = check2;
    }
    public String update(){
        boolean status = this.isCheck1();
        boolean status2 = this.isCheck2();
        return "checkboxExample.xhtml";
    }
} 

这是我的jsf页面

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:p="http://primefaces.org/ui">
   <h:head>
      <title>JSF tutorial</title>
      <link href="css/styles.css" rel="stylesheet" type="text/css" />
      <script type="text/javascript" src="js/myFunction.js">
      </script>
   </h:head>
   <h:body>
      <h2>CheckBox Example</h2>
      <h:form id="form">
        <h:outputLabel value="First Check"/>
        <h:selectBooleanCheckbox name="check1" id="check1" value="# {checkBoxTest.check1}" onclick="changeCheckBox2Status()">  </h:selectBooleanCheckbox>
        <h:outputLabel value="Second Check"/>
        <h:selectBooleanCheckbox name="check2" id="check2" value="#{checkBoxTest.check2}" disabled="#{checkBoxTest.check1?false:true}">   </h:selectBooleanCheckbox>
        <h:commandButton action="#{checkBoxTest.update()}" value="Submit2">
        </h:commandButton>
      </h:form>      
   </h:body>
</html>

我的脚本在下面给出

function changeCheckBox2Status(){
        var checkBox1Status = document.getElementById("form:check1");
        if(checkBox1Status.checked){
            document.getElementById("form:check2").disabled=false;
        }
    }

1 个答案:

答案 0 :(得分:0)

您尝试过类似的事情吗?

public class Model : INotifyPropertyChanged
{
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    public string Item { get; set; }

    public ICollectionView Items { get; set; }
    public Model()
    {
        Items = CollectionViewSource.GetDefaultView(new ObservableCollection<string>(new List<string> { "aaa", "bbb" }));
    }
    public void DoFirst()
    {
        Items.Filter = o => ((string)o).StartsWith("a");
    }

    public void DoSecond()
    {
        Item = null;
        OnPropertyChanged("Item");
        Items.Filter = o => false;
    }

    public void DoThird()
    {
        Items.Filter = o => ((string)o).StartsWith("b");
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

和另一个类似。