JSF notes

Below are some notes for developing a web application using JSF2.

1- @ManagedBean(name="somename") - annotation to register a normal java POJO into JSF to be managed;

2- @SessionScoped - put a bean into HttpSession

3- @RequestScoped - put a bean into HttpRequest

4- @ViewScoped - put a bean into HttpSession untill the next form post request comes in; this will consume server memory/disk, should take that into account while designing;

5- Get Spring context -

    ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());

6- Get managed beans annonated with @SessionScoped -

 FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("beanname");

7- Get managed bean annonated with @ViewScoped -

 FacesContext.getCurrentInstance().getViewRoot().getViewMap().get("beanname");

8- Get managed bean annonated with @RequestScoped -

 FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("beanname");

9- Get managed bean annonated with @ApplicationScoped -

 FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("beanname");

10- ExternalContext is a context which wrapped servletContext -

       ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
       ec.redirect(ec.getRequestContextPath() + "/pagesRichfaces/datadumpreport.jsf");

11- Use to trigger some back beans action with request attriutes -

    <c:if test="#{param['fromclient'] eq 'true'}">
           <f:event listener="#{sessionBean.autoLogin}" type="postAddToView"/>
           <f:attribute name="fromclient" value="true"/>
           <f:attribute name="username" value="#{param['username']}"/>
           <f:attribute name="pass" value="#{param['userpass']}"/>
    </c:if>
   #{param['somekey']} - to get parameters in URL
    public void autoLogin(ComponentSystemEvent event) {
        String clientKey = (String) event.getComponent().getAttributes().get("fromclient");
        if (clientKey != null) {
            this.userName = (String) event.getComponent().getAttributes().get("username");
            this.pass = (String) event.getComponent().getAttributes().get("pass");
        }
          ... do somthing
    }

12- Select element in JavaScript -

    <h:form id="hform">....</h:form>
    <script>
         #{rich:element('hname')}.value="test";
    </script>

13- Ajax to access backend bean

    <h:selectBooleanCheckbox>
        <f:ajax event="click" listener="#{helloJsf.selectAll}" />
    </h:selectBooleanCheckbox>
    /*
     * Accept <f:ajax/> request 
     */
    public void selectAll(AjaxBehaviorEvent event){
       System.out.println("selectAll...");
   }

14- valueChangeListener for <h:selectBooleanCheckbox/>
valueChangeListener is totally a server side event, and only be captured after the form was submitted.

<h:form id="form1" prependId="false">
    <h:selectBooleanCheckbox valueChangeListener="#{helloJsf.valueChange}"/>
    <h:commandButton value="submit"/>
</h:form>
    /*
     * Accept valueChangeEvent request 
     */
    public void valueChange(ValueChangeEvent event){
       System.out.println("valueChange...");
   }

15- Use JavaScript in JSF
Below JavaScript will set all checkbox to be checked or nonchecked.

<h:form id="form1" prependId="false">
    <h:selectBooleanCheckbox  onclick="setCheckbox(this.checked)"/>
    <h:selectBooleanCheckbox/>
    <h:selectBooleanCheckbox/>
</h:form>
<script>
function setCheckbox(checked){
    var elements = document.forms['form1'].getElementsByTagName("input");
    for (var e in elements){
        if(elements[e].type == "checkbox")
            elements[e].checked = checked;
    }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值