I want to make a selectOneMenu dropdown so I can select a status on my question. Is it possible to make the f:selectItem more flexible considering what happens if the order of the enums changes, and if the list was large? And could I do this better? And is it possible to automatically "select" the item that the question have?
Enum class
public enum Status { SUBMITTED, REJECTED, APPROVED }
Question entity
@Enumerated(EnumType.STRING) private Status status;
JSF
<div class="field"> <h:outputLabel for="questionStatus" value="Status" /> <h:selectOneMenu id="questionStatus" value="#{bean.question.status}" > <f:selectItem itemLabel="Submitted" itemValue="0" /> <f:selectItem itemLabel="Rejected" itemValue="1" /> <f:selectItem itemLabel="Approved" itemValue="2" /> </h:selectOneMenu> <hr /> </div>
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: “permanent”, “temp”, “apprentice”), or flags (“execute now”, “defer execution”).
We can an enumeration inside a class. But, we cannot define an enum inside a method.
valueOf() method returns the enum constant of the specified enumtype with the specified name. The name must match exactly an identifier used to declare an enum constant in this type.
You could use <f:selectItems value="# {carBean.carList}" /> and return a list of SelectItem instances that wrap the enum (use Status.values () to get all possible values). You can use following utility el function to obtain the enum values and use them in a SelectOneMenu for example. No need to create beans and boilerplate methods.
C# Enums. An enum is a special "class" that represents a group of constants (unchangeable/read-only variables). To create an enum, use the enum keyword (instead of class or interface), and separate the enum items with a comma: Example. enum Level { Low, Medium, High } You can access enum items with the dot syntax:
By default, the values // of the constants are as follows: // constant1 = 0, constant2 = 1, constant3 = 2 and // so on. enum flag {constant1, constant2, constant3, ....... }; Variables of type enum can also be defined.
If you intend to control the labels as well, you could add them to the Status enum: public enum Status { SUBMITTED ("Submitted"), REJECTED ("Rejected"), APPROVED ("Approved"); private String label; private Status (String label) { this.label = label; } public String getLabel () { return label; } }
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 and you can access the current item by var
attribute)
If you happen to use JSF utility library OmniFaces, then you could use <o:importConstants>
instead of a bean.
<o:importConstants type="com.example.Status" /> <h:selectOneMenu value="#{bean.question.status}" > <f:selectItems value="#{Status}" /> </h:selectOneMenu>
If you intend to control the labels as well, you could add them to the Status
enum:
public enum Status { SUBMITTED("Submitted"), REJECTED("Rejected"), APPROVED("Approved"); private String label; private Status(String label) { this.label = label; } public String getLabel() { return label; } }
with
<f:selectItems value="#{data.statuses}" var="status" itemValue="#{status}" itemLabel="#{status.label}" />
Or, better, make the enum value a property key of a localized resource bundle (EL 3.0 required):
<f:selectItems value="#{data.statuses}" var="status" itemValue="#{status}" itemLabel="#{text['data.status.' += status]}" />
with this in a properties file associated with resource bundle #{text}
data.status.SUBMITTED = Submitted data.status.REJECTED = Rejected data.status.APPROVED = Approved
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With