Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a "constants"-class in JSF

I'm building a web-application using JSF/Primefaces. I need to have a "constants"-class, i.e. a class that will consist of constants. These constants are mainly navigational commands that will be used throughout the application. The reason I am doing this is to avoid having instantiated Strings on an ad-hoc basis.

How do I achieve this, making the constants accessible from both backing beans and XHTML files?

I have tried using @ApplicationScoped and using the Singleton-pattern (Singleton class) but I am unable to get it working due to scope issues.

Or maybe I am just using the wrong approach. Any ideas/suggestions are welcome.

like image 331
javaMS Avatar asked Oct 01 '12 06:10

javaMS


People also ask

How to define a constant in Java?

In Java, the constant values are defined by using the final keyword. The final keyword represents that the value of the variable cannot be changed. Note that the identifier name must be in capital letters. We can also define constants as static. Write the identifier name in capital letters that we want to declare as constant.

How to declare a constant in JavaScript?

Overall, constants can be declared whether using an upper case or lower case, the most used and common convention is to all upper case letters. Given below are the examples of Javascript constants: Use of const keyword. Assigning or changing the value of constant EMPLOYEE_NAME in the above code.

How do you write a constant in a static variable?

We can also define constants as static. Write the identifier name in capital letters that we want to declare as constant. For example, PRICE=21000. If we use the private access-specifier before the constant name, the value of the constant cannot be changed in that particular class.

What are real and floating point constants?

Numeric constants that have a decimal point are called real or floating-point constants. By default, the real constants are of double type. We can explicitly mention the type of a floating-point constant as a float by appending the letter f or F at the end of the constant. For example, 45f, -0.14f, 5.6F.


1 Answers

How do I achieve this, making the constants accessible from both backing beans and XHTML files?

In backing beans, it's obviously easy. As they're just Java classes, it's not different from the "normal" Java way. You could use enums or public static final fields. In views, this is a different story. Until the upcoming version 3.0, EL does namely not support constants in any way.

I'd suggest using enums as EL has implicit support for them in string comparisons. It does not do any compiletime/runtime type safety checks, but you can use the enum name as a string. E.g.

<h:someComponent ... rendered="#{order.status == 'SHIPPING'}" />

If you prefer more self documenting code and a runtime check (no, a compiletime check is not possible), then you could consider using OmniFaces <o:importConstants>.

<o:importConstants type="com.example.OrderStatus" />
<h:someComponent ... rendered="#{order.status == OrderStatus.SHIPPING}" />

This is IMO only a bit more clumsy. The runtime check is however nice during development. A typo is easily overseen.

In the upcoming EL 3.0 (JSR-341, part of Java EE 7), it's possible to reference constants the same way. See also How to reference constants in EL? This only requires programmatic import of the constants as there's no standard JSP/Facelets tag for that.

like image 161
BalusC Avatar answered Nov 11 '22 12:11

BalusC