Showing posts with label jsf. Show all posts
Showing posts with label jsf. Show all posts

Wednesday, March 2, 2011

Getting objects created in current java thread

There are two ways to resolve object dependency in java e.g. objects created in one method will be needed in other method in the same thread request. One way of resolving this dependency is that you pass newly created objects as parameter to method but this approach has drawback e.g. if you have created an object in one method, then that method is calling another which is calling another method and that method requires object created in first method then using first approach you will have to pass object in the parameter of second method and then second method will pass it to third method as parameter. As second method does not require it but we will have to pass it to resolve dependency of third method.
Second and most efficient approach is using ThreadLocal class. You can set object in first method to this class by this:
threadLocal.set(contextObject);
Now in third method you can get this object using this:
threadLocal.get();
Usually this approach is used with objects which are current request's context related e.g. FacesContext object is the key object in JSF and is required to use it anywhere in the request. so JSF creates instance of FacesContext in each request and set it to ThreadLocal. and then later on returns it from ThreadLocal.



Sunday, December 19, 2010

Setting locale programmatically in jsf

In jsf you can set locale programmatically by this


UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
viewRoot.setLocale(new Locale("de"));


Friday, December 17, 2010

Setting value through EL in JSF

Sample method can be used to set new object programmatically:


public static void setObject(String expression, Object newValue) {
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext, expression,
Object.class);

//Check that the input newValue can be cast to the property type
//expected by the managed bean.
//Rely on Auto-Unboxing if the managed Bean expects a primitive
Class bindClass = valueExp.getType(elContext);
if (bindClass.isPrimitive() || bindClass.isInstance(newValue)) {
valueExp.setValue(elContext, newValue);
}
}


Resolving Method expression programmatically In JSF

EL is used in JSF to bind java methods with userinterface actions like commandbutton and commandlink. But sometimes It is needed to resolve method binding programmatically. Sample method can be used for this purpose:


public static Object resloveMethodExpression(String expression,
Class returnType,
Class[] argTypes,
Object[] argValues) {
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
MethodExpression methodExpression =
elFactory.createMethodExpression(elContext, expression, returnType,
argTypes);
return methodExpression.invoke(elContext, argValues);
}


Resolving EL programmatically to retrieve value

EL is used in JSF to set values from java classes with User Interface. Sometimes, EL is needed to resolve programmatically to retrieve value. Sample method can be used for this purpose:


public static Object resolveExpression(String expression) {
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext, expression,
Object.class);
return valueExp.getValue(elContext);
}


Thursday, October 14, 2010

ActionListener versus Action

Action method is used to perform navigation and ActionListener is used to perform some logic in backing bean.While it is possible to define a backing bean method that performs navigation and binds the method to the actionListener attribute, this is not best practice. Instead, application logic should be placed in the backing bean method to which the actionListener is bound. Following successful execution of that method, the ADF Faces lifecycle will call the method or control flow case specified by the action attribute. If the method bound to the actionListener fails, developers can change the action outcome to suppress navigation so that the user has the opportunity to correct the issue.

Wednesday, August 18, 2010

In order get application's context path in JSF, do the following:
FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();