Annotations support is coming to PrettyFaces: URL-rewriting

Posted by: Lincoln Baxter III on 07/06/2010

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 Lincoln Baxter III

Lincoln Baxter III

Creator of PrettyFaces - Url Rewriting for JSF, PrettyTime - Social-style date & time formatting for Java, and a community contributor to the JSF2 Expert Group; his latest project is ScrumShark, an open-source agile project management tool.

Beginning his career in C, C++ development for hardware signal testing automation, Lincoln soon moved on to Perl, dynamic programming languages, artificial intelligence and, more recently - web application frameworks such as Java Server Faces and Groovy on Grails for financial and small business solutions.

When he is not swimming, running, or playing Ultimate Frisbee, Lincoln is focused on improving the openness of Java, the Java Community Process(JCP), and bringing the J2EE platform to small businesses and freelancers.

More About Lincoln »

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 7
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 3
  • Automation Tools: Git, Hudson, Sonar
  • HTML5, Ajax, jQuery, Usability
  • Mobile Applications - iPhone and Android
  • More...
Learn More »

NFJS, the Magazine

May Issue Now Available
  • Client-Side MVC with Spine.js, Part 1

    by Craig Walls
  • On Prototypal Inheritance, Part 2

    by Raju Gandhi
  • Making use of Scala Lazy Collections

    by Venkat Subramaniam
  • Integration Testing Web Applications Using Gradle

    by Kenneth Kousen
Learn More »