Sunday, May 10, 2009

JSF: Refresh Bean AND get different Language bundle per host (Properties)

Hi again,

two small gadgets for you:

1. Get and default bean attributes for different websites or different languages

Sometimes you have the same portlet-code for different websites. I use multiple properties files and select the same within the bean with the following code:

private void initBeanData() {
FacesContext fCtx = FacesContext.getCurrentInstance();

ResourceBundle bundle =
ResourceBundle.getBundle( this.getBundleByHost() , fCtx.getViewRoot().getLocale());
_name = bundle.getString("name_default");
_entryText = bundle.getString("text_default");
}




FacesContext fCtx = FacesContext.getCurrentInstance();
String host = ((PortletRequest)fCtx.getExternalContext().getRequest()).getServerName();
if (host.contains("localhost"))
return "LocalBundle";
...



2. Refresh (backing) Bean

After submitting some input I asked myself how to refresh the backing bean? After some research I found a LOT of people having the same problem.
That seems pretty funny in the meanwhile to me as I realized now how easy that is. Maybe there are some people like me that didn't get it the first time. ;)

Here is the code:

public void reset(ActionEvent actionEvent) {

this.initBeanData();
}


(For the init method: see above :) )

Then of course you have to call the method as always from the view (here: JSP):

3 comments:

  1. I want to refresh only the data table in JSF page when i tried to add a row to the table. Rt now the total page is being refreshed.Please provide me any solution for this.

    ReplyDelete
  2. Online JSF Training JSF Training Institutes in Chennai Java Training Institutes Java Training Institutes Struts2 Training Institutes in Chennai Struts2 Training Institutes in Chennai

    EJB Training Institutes in Chennai EJB Training Institutes in Chennai Java EE Training Institutes in Chennai Java EE Training Institutes in Chennai Java Training in CHennai

    ReplyDelete

I'm happy about every feedback! :)