With the current version of Java EE 8 / JSF 2.3 we can implement an internal navigation using enums. And this is what we use for Alumni’s [1] navigation. As an example we pick the forgot password link.
1 <div> 2 #{msg.lblForgotPassword} 3 <h:link value="#{msg.lblRequestPassword}" 4 outcome="#{Page.RequestPassword.url()}"/> 5 </div>
Take a look onto line 4. Here the outcome is directly composed by the enum’s url. We simply use the enum like we do within a Java source file.
The page enum looks like this:
1 public enum Page { 2 3 RequestPassword("/public/requestPassword"), 4 <other pages here> 5 ; 6 7 private Page(String url) { 8 _url = url; 9 } 10 private final String _url; 11 12 public String getUrl() { 13 return _url + ".xhtml"; 14 } 15 16 public String getRedirectUrl() { 17 return _url + ".xhtml?faces-redirect=true"; 18 } 19 ... 20 }
Using an enum for the navigation decouples the page definition from the navigation targets within our source code (without any need for using a xml file to declare navigation cases). If we want to relocate the page, we only need to update the enum.
As a pre-requisite we need to tell the EL about our enum. That’s what the new importConstants tags comes into play for. Within the metadata we need to declare this tag in conjunction with the full class name.
1 <f:metadata> 2 <f:importConstants type="de.muellerbruehl.alumni.gui.enums.Page"/> 3 </f:metadata>
Although Alumni only imports enums, you may use the importConstants tag for any kind of constants.
Stay tuned!
[1] Alumni is the social web application I describe in my book
Do you want to support this blog? You may help to finance the needed hardware.
Want to read more about software development? Purchase one of my books:
Start application development with Java. Learn development foundation, quality control and more.
A Journey through Java EE Technologies whilst developing Web Applications with JavaServer Faces.
Java Lambdas and Parallel Streams
The compact starter: Foundation, supporting structures, parallel processing
No software development, just nature:
A photographic image book. Get it for free. Or pay whatever you like.
No Comments Yet