How to use enum values in f:selectItem(s)

JSF has a builtin converter for enum, so this should do: @Named @ApplicationScoped public class Data { public Status[] getStatuses() { return Status.values(); } } with <h:selectOneMenu value=”#{bean.question.status}” > <f:selectItems value=”#{data.statuses}” /> </h:selectOneMenu> (note: since JSF 2.0 there’s no need anymore to provide a SelectItem[] or List<SelectItem>, a T[] and List<T> are accepted as well … Read more

JSF 2.0 File upload

First of all, this (old) question and answer assumes JSF 2.0/2.1. Since JSF 2.2 there’s a native <h:inputFile> component without the need for 3rd party component libraries. See also How to upload file using JSF 2.2 <h:inputFile>? Where is the saved File? The easiest way would be using Tomahawk for JSF 2.0. It offers a … Read more

Localization in JSF, how to remember selected locale per session instead of per request/view

You need to store the selected locale in the session scope and set it in the viewroot in two places: once by UIViewRoot#setLocale() immediately after changing the locale (which changes the locale of the current viewroot and thus get reflected in the postback; this part is not necessary when you perform a redirect afterwards) and … Read more

ViewParam vs @ManagedProperty(value = “#{param.id}”)

<f:viewParam>: Sets the value during update model values phase only (since it extends UIInput). The set value is not available during @PostConstruct, so you need an additional <f:event type=”preRenderView” listener=”#{bean.init}” /> inside the <f:metadata> to do initialization/preloading based on the set values. Since JSF 2.2 you could use <f:viewAction> for that instead. Allows for nested … Read more

Understand Flash Scope in JSF2

In short, variables stored in the flash scope will survive a redirection and they will be discarded afterwards. This is really useful when implementing a Post-Redirect-Get pattern. If you try to navigate to another page by redirect and access the attributes on load, they will be there. After that request is done the values in … Read more

Pass an object between @ViewScoped beans without using GET params

Depends on whether you’re sending a redirect or merely navigating. If you’re sending a redirect, then put it in the flash scope: Faces.setFlashAttribute(“car”, car); This is available in the @PostConstruct of the next bean as: Car car = Faces.getFlashAttribute(“car”); Or, if you’re merely navigating, then put it in the request scope: Faces.setRequestAttribute(“car”, car); This is … Read more

Internationalization in JSF, when to use message-bundle and resource-bundle?

<message-bundle> The <message-bundle> is to be used whenever you want to override JSF default warning/error messages which is been used by the JSF validation/conversion stuff. You can find keys of the default warning/error messages in chapter 2.5.2.4 of the JSF specification. For example, Messages_xx_XX.properties files in com.example.i18n package as below which overrides the default required=”true” … Read more

Retaining GET request query string parameters on JSF form submit

Your concrete problem is caused because a JSF <h:form> submits by default to the current request URL without any query string. Look closer at the generated HTML output, you’ll see <form action=”/app/agreement.xhtml” …> You’d thus explicitly need to include those request parameters yourself. There are several ways to solve this. If you weren’t sending a … Read more

Best way to add a “nothing selected” option to a selectOneMenu in JSF

Just explicitly set the select item value to null. <h:selectOneMenu value=”#{bean.selectedItem}”> <f:selectItem itemValue=”#{null}” itemLabel=”–select–” /> <f:selectItems value=”#{bean.availableItems}” /> </h:selectOneMenu> No, an empty string like itemValue=”” is not sufficient. It really has to be null. Otherwise you run into trouble as described in this Q&A: Using a “Please select” f:selectItem with null/empty value inside a p:selectOneMenu. … Read more