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>