Web applications usually have two parts in their title. One indicating the current page and the other describing the entire application. It would be nice to put the application wide title and parameterize the page level title within your applications' layout. Starting from 0.5, EasyWeb4J allows you to annotate your action methods with custom annotation and access them easily within any part of your controller.
This sample application would have two pages with titles "Home - Dynamic Title Demo" and "Admin - Dynamic Title Demo". Here, "Home" and "Admin" are specific to the particular page while the rest of the title is common to the entire application.

Download the complete source code for this application.
Layout and Controller
The layout specifies the application wide title as template text and uses an EL expression to include the page specific title before it. It also provides the navigation links for the 2 pages.
src/main/webapp/WEB-INF/layouts/Application.jsp
The controller has 2 actions corresponding to the application's pages.
src/main/java/org/title/controllers/HomeController.java
Making it Work
Let's create a custom annotation specific to this application which will be used on the action methods to specify the page specific title
src/main/java/org/title/annotations/Title.java
Next, we must override the filterRequest() method to make use of the Title annotation when available on action methods. EasyWeb4J 0.5 and above provide the getActionAnnotation() method, which can be used in our filter to access the annotations on the current request's action method.
We then annotate our action methods with @Title("Home") and @Title("Admin") to specify their titles as shown below.
src/main/java/org/title/controllers/HomeController.java
In a real-world application the filter can be moved to an abstract super class to setup the appropriate titles for actions across all controllers.
This sample application would have two pages with titles "Home - Dynamic Title Demo" and "Admin - Dynamic Title Demo". Here, "Home" and "Admin" are specific to the particular page while the rest of the title is common to the entire application.

Download the complete source code for this application.
Layout and Controller
The layout specifies the application wide title as template text and uses an EL expression to include the page specific title before it. It also provides the navigation links for the 2 pages.
src/main/webapp/WEB-INF/layouts/Application.jsp
The controller has 2 actions corresponding to the application's pages.
src/main/java/org/title/controllers/HomeController.java
Making it Work
Let's create a custom annotation specific to this application which will be used on the action methods to specify the page specific title
src/main/java/org/title/annotations/Title.java
Next, we must override the filterRequest() method to make use of the Title annotation when available on action methods. EasyWeb4J 0.5 and above provide the getActionAnnotation() method, which can be used in our filter to access the annotations on the current request's action method.
We then annotate our action methods with @Title("Home") and @Title("Admin") to specify their titles as shown below.
src/main/java/org/title/controllers/HomeController.java
In a real-world application the filter can be moved to an abstract super class to setup the appropriate titles for actions across all controllers.
0 comments:
Post a Comment