Calling Primefaces dialog box from Managed Bean function

You can, by using the RequestContext (or PrimeFaces if you are using the version 6.2 or higher) class. Suppose you have the following: <p:dialog id=”myDialogID” widgetVar=”myDialogVar”> …. </p:dialog> So the way you do in the facelet itself, i.e. onclick=myDialogVar.show();, the same can be done in your managed bean like so: For PrimeFaces <= 3.x RequestContext … Read more

How can I get a message bundle string from inside a managed bean?

You can get the full qualified bundle name of <message-bundle> by Application#getMessageBundle(). You can get the current locale by UIViewRoot#getLocale(). You can get a ResourceBundle out of a full qualified bundle name and the locale by ResourceBundle#getBundle(). So, summarized: FacesContext facesContext = FacesContext.getCurrentInstance(); String messageBundleName = facesContext.getApplication().getMessageBundle(); Locale locale = facesContext.getViewRoot().getLocale(); ResourceBundle bundle = ResourceBundle.getBundle(messageBundleName, … Read more

Spring JSF integration: how to manage a bean and inject a Spring component/service?

@ManagedBean vs @Controller/@Component First of all, you should choose one framework to manage your beans. You should choose either JSF or Spring (or CDI) to manage your beans. Whilst the following works, it is fundamentally wrong: @ManagedBean // JSF-managed. @Controller // Spring-managed (same applies to @Component) public class BadBean {} You end up with two … Read more

How do I process GET query string URL parameters in backing bean on page load?

Yes, you can use the <f:viewParam> to set a request parameter as a managed bean property. <f:metadata> <f:viewParam name=”companyId” value=”#{bean.companyId}” /> </f:metadata> You can if necessary invoke a bean action using <f:viewAction> (JSF 2.2+ only) or <f:event type=”preRenderView”>. <f:metadata> <f:viewParam name=”companyId” value=”#{bean.companyId}” /> <f:viewAction action=”#{bean.onload}” /> </f:metadata> When using <f:viewAction> you can even return a … Read more

JSF page redirecting from java bean

Not sure what you’re after, but the ExternalContext#dispatch() does only a forward, not a redirect. You’d like to use ExternalContext#redirect() instead. externalContext.redirect(“foo.xhtml”); or even external (which is not possible with dispatch) externalContext.redirect(“http://stackoverflow.com”); You’d normally like to do this in bean’s action method. Since you mentioned JavaScript in the comments, here’s how you could redirect using … Read more

Pass input text value to bean method without binding input value to bean property

Bind the component as UIInput to the view and use UIInput#getValue() to pass its value as method argument. <h:inputText binding=”#{input1}” /> <h:commandButton value=”Test” action=”#{myBean.execute(input1.value)}” /> with public void execute(String value) { // … } Note that the value is this way already converted and validated the usual JSF way. See also: How does the ‘binding’ … Read more

Canonical way to obtain CDI managed bean instance: BeanManager#getReference() vs Context#get()

beanManager#getReference gives you a new instance of a client proxy but the client proxy will forward method calls to the current contextual instance of a particular context. Once you obtain the proxy and keep it and the method calls will be invoked on the current instance (e.g. current request). It is also useful if the … Read more

Why are there different bean management annotations

javax.enterprise.context.SessionScoped(JSR 346) and all other annotations under the javax.enterprise.context.* package maintain the context of CDI. CDI provides an alternative, versatile and more powerful mechanism for dependency injection, bean and general resource management within the Java EE space. It’s an alternative to JSF managed beans and it’s set to even supersede the JSF bean management mechanism … Read more

How do I force an application-scoped bean to instantiate at application startup?

My first thought was to use an old style ServletContextListener contextInitialized() method and from there use an ELResolver to manually request the instance of my managed bean (thus forcing the initialization to happen). Unfortunately, I can’t use an ELResolver to trigger the initialization at this stage because the ELResolver needs a FacesContext and the FacesContext … Read more

Calling a JavaScript function from managed bean

PrimeFaces 6.2+ Use PrimeFaces#executeScript(): public void submit() { // … PrimeFaces.current().executeScript(“alert(‘peek-a-boo’);”); } NOTE: works only when submit() is invoked by Ajax. PrimeFaces 6.2- Use RequestContext#execute(): public void submit() { // … RequestContext.getCurrentInstance().execute(“alert(‘peek-a-boo’);”); } NOTE: works only when submit() is invoked by Ajax. JSF 2.3+ Use PartialViewContext#getEvalScripts(): public void submit() { // … FacesContext.getCurrentInstance().getPartialViewContext().getEvalScripts().add(“alert(‘peek-a-boo’);”); } NOTE: … Read more