Archive for the ‘JSF’ category

Popular Web Frameworks

September 7th, 2007

A post was made on Javalobby in the response of Rick Hightower observation on fastest growing frameworks. According to graph it is clear that there are more jobs of JSF and its demand is increasing as compared to other web frameworks.

Luckily I got chance to work on JSF in my previous job, and I really enjoy the work of JSF blended with some controls of Tomahawk. The framework is great to work.

jobgraph

And let me tell you, in Pakistan there are a lot of jobs that require skills in JSF. In my current job I am working on Struts, and I really missing JSF very much.Because in JSF things are very automated, clear and easy to configure rather in Struts. JSF is developed for RAD, and if you are creating a new application prefer JSF because JSF is built with integration and extensibility in mind. Strategically, JSF should be the target of new applications.I cant say anything about Rubby, Spring and Tapestry because I did not work in them. But I will surely express my experience if I got chance to dirty my hands on later said frameworks.

Accessing a selected row from h:dataTable

April 8th, 2007

Javascript to Select One Radio Button


function selectOne(form, button) {
turnOffRadioForForm(form);
button.checked = true;
}
function turnOffRadioForForm(form) {
for (var i = 0; i < form.elements.length; i++) {
form.elements[i].checked = false;
}
}

JSF Code of h:dataTable Table on JSP Page
We will use valueChangeListener for this, as when user check the radio button we will get the binded value of bean. It is one column. NOTE: Please remove all spaces after : sign. In f and h tags.

<h:column>
<f:facet name=”header”>
<f:verbatim>Select</f:verbatim>
</f:facet>
<h:selectOneRadio id="selectRadio" value="#{depositMoneyPageBean.creditCardDataBean.cardId}"
valueChangeListener="#{depositMoneyPageBean.handleRadioValueChange}"
onclick="selectOne(this.form , this)" >
<f:selectItem itemValue="#{creditCardData.cardId}" itemLabel=" " />
</h:selectOneRadio>
</h:column>

Page Bean Code

This is the page bean code, and here is the valueChangeListener method.

public void handleRadioValueChange(ValueChangeEvent valueChangedEvent){
try	{
// here we got the selected row of dataTable
this.creditCardDataBean = (CreditCardDataBean)getTable1().getRowData();
selectedCardId = creditCardDataBean.getCardId();
}catch (Exception e){
e.printStackTrace();
}
}
public HtmlDataTable getTable1(){
if (table1 == null) {
table1 = (HtmlDataTable) findComponentInRoot(&quot;table1&quot;);
}
return table1;
}

public static UIComponent findComponentInRoot(String id) {
UIComponent ret = null;
FacesContext context = FacesContext.getCurrentInstance();
if (context != null) {
UIComponent root = context.getViewRoot();
ret = findComponent(root, id);
}
return ret;
}

public static UIComponent findComponent(UIComponent base, String id) {

// Is the &quot;base&quot; component itself the match we are looking for?
if (id.equals(base.getId())) {
return base;
}

// Search through our facets and children
UIComponent kid = null;
UIComponent result = null;
Iterator kids = base.getFacetsAndChildren();
while (kids.hasNext() &amp;amp;&amp;amp; (result == null)) {
kid = (UIComponent) kids.next();
if (id.equals(kid.getId())) {
result = kid;
break;
}
result = findComponent(kid, id);
if (result != null) {
break;
}
}
return result;
}

Getting and setting Managed Bean objects in JSF

April 8th, 2007

Get appplication from FacesContext

FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();

Setting in Managed Bean Object

app.createValueBinding("#appSessionBean.userDataBean}").setValue(ctx, userDataBean);

Getting Managed Bean Object(s)

AppSessionBean appSessionBean = (AppSessionBean)app.createValueBinding("#{appSessionBean}").getValue(ctx);
userDataBean = (UserDataBean)appSessionBean.getUserDataBean();

where userDataBean is object in ApplicationSessionBean class and this said class is in faces-config with session scope.

Getting HTTP Session in JSF

April 8th, 2007
import javax.faces.context.FacesContext;

public void addInSession() {
MySession mySession = new MySession();
mySession.setName(getMsg());

/* .getSession(boolean), true if we want to create new session, if want touse oldsession then false*/
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
session.setAttribute(&quot;sessionObj&quot;, mySession);

if (session.getAttribute(&quot;sessionObj&quot;) != null) {
MySession mySavedSession = (MySession) session.getAttribute(&quot;sessionObj&quot;);
System.out.println(&quot;HTTP Session: &quot; + mySavedSession.getName());
} else {
// do something
}
}

Getting request parameters in JSF

April 8th, 2007

Here is the way, how we get request parameters from a JSP page (which is JSF enabled) in a HTTP request object.

import javax.faces.context.FacesContext;
//…your code
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
String controlName = request.getParameter(&quot;formId:controlId&quot;);

Where formName is refer to the id of your form and controlId is referred to the id of your control.

Faces Console: A utility for JSF configurations

April 8th, 2007

Faces Console is a free utility get integrated in many IDEs as plugin and provide JSF (faces-config.xml) configuration facility in a GUI. if you are using NetBeans5.0 or Eclipse3.x you must try it.

http://www.jamesholmes.com/JavaServerFaces/console/

faces-console