JSF notes

原创 2015年11月19日 17:33:12

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>

相关文章推荐

jsf web.xml配置解析

JSF笔记 – JSF配置文件的说明和常用配置元素 本文由arthinking发表于4年前 | J2EE | 暂无评论 |  被围观 6,005 views+ 在web.xml中配置F...

JSF复习系列(2)--迭代器的直观体现dataTable使用详解

一、标签简介1.1 概念  JSF允许使用h:dataTable标签来放置组件,利用迭代器原理,遍历数据来创建html表格。1.2 适用条件  遍历的内容应为以下类型的数据:   java对象 数组...

Spring与JSF2的整合配置的整理

目的:在JSF2中可以使用Spring中的Bean,可以通过注解方式配置使用。环境:在JSF2.2,Spring3.2.7版本测试通过。配置过程概述: 整合过程主要包括一下三个方面的配置:web.xm...

JSF标签之数据表h:dataTable

以下代码可以在我的资源中下载 JavaServer Faces核心编程(第3版)源码@ManagedBean @SessionScoped public class TableData { ...

JSF2.0实战 - 10、自定义Ajax更新方式

前面实现了简单的组件开发

【转】服务化框架技术选型与京东JSF解密

声明:本文转载自微信公众号“开涛的博客”,转载务必声明。   作者:章耿,原京东资深架构师,曾负责京东服务框架,配置中心等基础平台。近十年工作经验,专注于基础中间件等底层技术架构,对分布式...

JSF框架下使用searchableSelect实现搜索及级联功能

searchableSelect.js插件可将select下拉框渲染为由div组成的一个具搜索功能的下拉框,该插件会将原生的select隐藏然后利用div及input实现一个新的下拉框,所以该插件不支...

【第十章】集成其它Web框架 之 10.3 集成Struts2.x 集成JSF ——跟我学spring3

先进行通用配置, 【第十章】集成其它Web框架 之 10.1 概述    10.3  集成Struts2.x 10.3.1  概述 Struts2前身是WebWork,核心并没有改变,...

如何用JSF技术上传文件(PrimeFaces)

如何用JSF技术上传文件(PrimeFaces)

JSF+Spring+Hibernate框架的搭建示例图解

File>New>Project弹出如下对话框 选择Web Project后,单击Next按钮。如图 输入项目名称(如JSHExam)后,单击Finish按钮。如图 单击Yes按钮。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JSF notes
举报原因:
原因补充:

(最多只允许输入30个字)