使用来自Javascript的bean中设置的枚举值

时间:2014-11-27 02:43:13

标签: java javascript jsf enums

我想从enum中的java script检查托管bean中设置的JSF值。我怎么能这样做?

我的JSF有类似下面的内容。

<script language="javascript">

      function displayMsg() {
          setwindowOverlayPos('popup_window',  - 100,  - 150);
      }

      window.onload = function () {

     // this is the enum value I want to check. if the value is cust1 ,  I call the popup window.

          var cust1= '#{myBean.customerType.custType == "cust1"}';  

          if(cust1){
            displayMsg();
          }

      }
 </script>

我有客户类型的枚举

public enum CustomerTypeEnum {


CUST1("CUST1"),
CUST2("CUST2");

private String custType;

private CustomerTypeEnum(String s) {
    custType = s;
}

/**
 * @return customer type
 */
public String CustomerTypeEnum() {
    return custType;
}

}

我的托管bean有

@ManagedBean(name = "home")
@ViewScoped
public class HomeBean extends BaseOrderStepBean {

     private CustomerTypeEnum customerType;

     public void method1(){

         customerType = CustomerTypeEnum.CUST1;
     }


}

1 个答案:

答案 0 :(得分:0)

我写这篇文章是为了别人的利益。我解决了这个问题。只需要像下面那样更改javascript。

          var od = '#{MyBean.customerType.custType == "OD"}';
          var alreadyHasIC = '#{sessionBean.icNumber}'
          if ("true" == od) {
              if(!alreadyHasIC){
                  displayMyForm();
              }
          }

这意味着可以像这样从Java脚本访问Bean的Enum。