July 6th, 2010 by Christian Kaltepoth

Annotations support is coming to PrettyFaces: URL-rewriting

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!

Posted in PrettyFaces

5 Comments

  1. […] 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 […]

    1. Sandeep says:

      I can totally acetppiare the effort that Google is making to penetrate the social media sphere/crowdsourcing . There might be some traction with this latest attempt although the way it’s being executed could be detrimental to the whole campaign. Just a couple of things.1: “+1” is hard to relate to. I noticed the Google has the Microsoft problem. They have brilliant engineers but they lack the coolness/connection factor. I think the coolness/connection factor is integral to the success of facebook and apple. When you use apple’s and facebook’s technology you feel like you connect with something deeper than the technology itself. There is a feeling associated with using the technology. For some reason it seems that Microsoft and Google don’t put as much emphasis on design and how it “feels” to interact with their technology. I don’t have much time so I’ll get to the point I want to make. “+1” has no emotional appeal. Where in your life have you ever “+1ed”something. I like stuff all the time whether I do in internally or express it externally. +1 has no meaning outside of the Google context. So my first point has to do with what they named the technology. They could have come up with something that was already a part of the common lexicon and something a little richer . A synonym for like, Or something cool like sweet , hot , value , love . These might be a little corny but not as corny as +1 Secondly, I know they will be adding the +1 to pages on the web. They need to release that function right away. It should have been released for the pages before it was released for the SERPs. Right now it seems like Google is asking you to like how a SERP result looks. Because of how it’s positioned it seems as though it’s not asking how much you like the content on a page. For instance, to like something I’ll have to run a query in Google. Visit the page. Rerun the query or hit the back button a couple of times and then hit the +1 button. That’s a lot of work for a lazy guy like me and unfortunately I’m not getting paid by Google to do this work so instead of going back and +1ing a result I’ll just continue onto my business. Great Idea. Poor Execution. Please Fix. I want you guys to stay around.

  2. Tiago says:

    Do URLMapping supports regex?

    1. Yes. Just like with the XML configuration.

Leave a Comment




Please note: In order to submit code or special characters, wrap it in

[code lang="xml"][/code]
(for your language) - or your tags will be eaten.

Please note: Comment moderation is enabled and may delay your comment from appearing. There is no need to resubmit your comment.