Annotations support is coming to PrettyFaces: URL-rewriting
Christian Kaltepoth
The PrettyFaces team is currently working on an alternative way to configure URL mappings. PrettyFaces will soon allow to use annotations instead of the classic XML configuration file to declare mappings. We encourage everyone interested in PrettyFaces to take a look at this new way of configuration and share his or her opinion with us.
Examples
This section shows some simple examples for the annotation support of PrettyFaces. For details refer to the Annotation-based configuration chapter in the current snapshot of the PrettyFaces Reference Documentation (note, this documentation will be merged into the primary PrettyFaces reference guide once released.)
Declaring a URL mapping using annotations is straight forward. Just add a @URLMapping to the class primarily responsible for the page. The annotation requires you to specify the ID of the mapping, the URL pattern and the view to render:
@URLMapping(id="login", pattern="/login", viewId="/login.jsf") public class LoginBean { /* your code */ }
Path parameters can be embedded in the URL pattern as already known from the XML configuration:
@URLMapping(id="viewStory", pattern="/story/#{storyBean.currentStoryId}", viewId="/story-view.jsf") public class StoryBean { private Long currentStoryId; /* your code */ }
To use query parameters, just annotate the target property with a @URLQueryParameter annotation. The query parameter will automatically refer to the mapping declared on the class.
@URLMapping(id="viewStory", pattern="/story/view", viewId="/story-view.jsf") public class StoryBean { @URLQueryParameter("id") private Long currentStoryId; /* your code */ }
If you want to use page actions, just add a @URLAction annotation to the methods you want to be executed.
@URLMapping(id="viewStory", pattern="/story", viewId="/story.jsf") public class StoryBean { @URLQueryParameter("id") private Long currentStoryId; private Story story; @URLAction public void loadStory() { // load the story story = myStoryDao.getById( currentStoryId ); } /* more code */ }
Please note that all annotations accept a wide range of additional options to customize the mapping. As they are very similar to the well known syntax of the classic XML configuration you should quickly get familiar with them.
Give it a try
Want to take a look at the new annotations support in PrettyFaces? Please do! We are always looking for people helping us testing the new features.
The current snapshots are regularly deployed to the following Maven repository:
<repository> <id>ocpsoft</id> <name>OcpSoft</name> <url>http://ocpsoft.com/repository/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository>
Depending on your JSF version, add the PrettyFaces dependency to your Maven pom.xml:
For JSF 2.0:
<dependency> <groupId>com.ocpsoft</groupId> <artifactId>prettyfaces-jsf2</artifactId> <version>3.0.2-SNAPSHOT</version> </dependency>
For JSF 1.2:
<dependency> <groupId>com.ocpsoft</groupId> <artifactId>prettyfaces-jsf2</artifactId> <version>3.0.2-SNAPSHOT</version> </dependency>
PrettyFaces will support annotations out of the box, nevertheless, it is strongly recommended to specify the packages to scan for annotations; just add the following configuration to your web.xml:
<context-param> <param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name> <param-value>com.example.myapp</param-value> </context-param>
That’s it!
If you have any feedback or questions, please ask on the OcpSoft Support Forums, or the OcpSoft Users Group Mailing List. For more information, visit the PrettyFaces home page. Thanks!
|
|
About the author:Christian Kaltepoth is Senior Developer at Klute-Thiemann Informationstechnologie in Dortmund, Germany. He is an active PrettyFaces committer and creator of Criteria4JPA, the type-safe query library for JPA. Beside his interest for Java EE technologies, he enjoys developing applications for the Android platform. |
Posted in PrettyFaces
[...] Get PrettyFaces!Features & Enhancements:Introducing URL mapping configuration with annotations (tutorial + docs)Added path and query-parameter validation wth managed bean methods (#50 + r353)Added support [...]
[...] http://ocpsoft.com/prettyfaces/annotations-support-is-coming-to-prettyfaces-url-rewriting/ [...]