Selasa, 08 April 2014

The OmniFaces project has started


Together with my colleague Arjan Tijms, who you shall probably also know from stackoverflow.com and from the majority of the JSF related blogs at jdevelopment.nl, the project OmniFaces has been started.



OmniFaces is a library for JSF 2.x that focusses on utilities that ease everyday tasks. An important design goal will be to have as few dependencies as possible (so far, it only requires JSF 2.0, EL 2.1 and Servlet 2.5 APIs which is already minimally available in a bit modern container serving a JSF 2.0 web application) and to be minimally invasive. As such, OmniFaces should principally integrate perfectly well with most other JSF libraries. Characteristic of OmniFaces will be that it will not just be about components, but instead will have an equally strong focus on providing utility classes for working with the JSF API from Java code.



OmniFaces is still under development, but so far among others the following components are available:





Tree



The allows you to render a markup-less tree wherein you have the freedom to declare markup, such as

  • . You can give every individual node level its own markup. Here's an example of a menu wherein the first level (with a value of 0) is been rendered as a

    and all of its children are rendered as nested
    • .





      value="#{treeDemo.menu}" var="page">
      level="0">

      href="#{page.url}">#{page.title}









      • href="#{page.url}">#{page.title}









      The gives you the space to write markup around a single tree node. The level attribute specifies for which level the tree node should be rendered. In the above example, the content of will only be rendered for all tree nodes of the first level and the content of the level-less will be rendered for tree nodes of all other levels. The gives you the space to write markup around every single child of the current tree node. The indicates the insertion point where the associated with the child's level must be rendered.



      Here's what the Page bean look like:




      public class Page {

      private String url;
      private String title;

      public Page(String url, String title) {
      this.url = url;
      this.title = title;
      }

      // Getters/setters.
      }


      Here's what the backing bean look like:




      public class TreeDemo {

      private TreeModel menu;

      @PostConstruct
      public void init() {
      menu = new ListTreeModel();
      TreeModel general = menu.addChild(new Page("general.xhtml", "General"));
      TreeModel components = menu.addChild(new Page("components.xhtml", "Components"));
      TreeModel help = menu.addChild(new Page("help.xhtml", "Help"));

      TreeModel aboutUs = general.addChild(new Page("aboutus.xhtml", "About us"));
      TreeModel location = general.addChild(new Page("location.xhtml", "Location"));
      TreeModel contact = general.addChild(new Page("contact.xhtml", "Contact"));

      TreeModel tree = components.addChild(new Page("tree.xhtml", "Tree"));
      TreeModel validators = components.addChild(new Page("validators.xhtml", "Validators"));

      TreeModel allOrNone = validators.addChild(new Page("allOrNone.xhtml", "All or None"));
      TreeModel oneOrMore = validators.addChild(new Page("oneOrMore.xhtml", "All or None"));
      TreeModel allEqual = validators.addChild(new Page("allEqual.xhtml", "All equal"));
      TreeModel allUnique = validators.addChild(new Page("allUnique.xhtml", "All unique"));
      }

      public TreeModel getMenu() {
      return menu;
      }

      }


      Here's what the generated HTML output look like:





      href="general.xhtml">General




      • href="aboutus.xhtml">About us


      • href="location.xhtml">Location


      • href="contact.xhtml">Contact



      href="components.xhtml">Components




      • href="tree.xhtml">Tree


      • href="validators.xhtml">Validators


        • href="allOrNone.xhtml">All or None


        • href="oneOrMore.xhtml">All or None


        • href="allEqual.xhtml">All equal


        • href="allUnique.xhtml">All unique





      href="help.xhtml">Help





      For the fans of chaining, it's also possible to create the tree model by chaining:





      @PostConstruct
      public void init() {
      menu = new ListTreeModel();
      menu.addChild(new Page("general.xhtml", "General"))
      .addChild(new Page("aboutus.xhtml", "About us")).getParent()
      .addChild(new Page("location.xhtml", "Location")).getParent()
      .addChild(new Page("contact.xhtml", "Contact")).getParent().getParent()
      .addChild(new Page("components.xhtml", "Components"))
      .addChild(new Page("tree.xhtml", "Tree")).getParent()
      .addChild(new Page("validators.xhtml", "Validators"))
      .addChild(new Page("allOrNone.xhtml", "All or None")).getParent()
      .addChild(new Page("oneOrMore.xhtml", "All or None")).getParent()
      .addChild(new Page("allEqual.xhtml", "All equal")).getParent()
      .addChild(new Page("allUnique.xhtml", "All unique")).getParent().getParent().getParent()
      .addChild(new Page("help.xhtml", "Help"));
      }




      Multi field validators



      There are five special validators for multiple input fields:




      Here is an extract of relevance from the ValidateMultipleFields javadoc:




      General usage of all multiple field validators


      This validator must be placed inside the same UIForm as the UIInput components in question.
      The UIInput components must be referenced by a space separated collection of their client IDs in the
      components attribute. This validator can be placed anywhere in the form, but keep in mind that the
      components will be validated in the order as they appear in the form. So if this validator is been placed before all
      of the components, then it will be executed before any of the component's own validators. If this validator fails,
      then the component's own validators will not be fired. If this validator is been placed after all of the components,
      then it will be executed after any of the component's own validators. If any of them fails, then this validator
      will not be exeucted. It is not recommended to put this validator somewhere in between the referenced components as
      the resulting behaviour may be confusing. Put this validator either before or after all of the components, depending
      on how you would like to prioritize the validation.









      In an invalidating case, all of the referenced components will be marked invalid and a faces message will be added
      on the client ID of this validator component. The default message can be changed by the message
      attribute. Any "{0}" placeholder in the message will be substituted with a comma separated string of labels of the
      referenced input components.





      The faces message can also be shown for all of the referenced components using showMessageFor="@all".











      The showMessageFor attribute defaults to @this. Other values than @this or
      @all are not allowed.




      Let's look how is useful for password confirmation validation, for example:





      columns="3">
      ...

      for="password" value="Enter password" />
      id="password" value"#{register.user.password}" redisplay="true" required="true"
      requiredMessage="Please enter password" />

      for="password" />
      for="equal" />


      for="confirm" value="Confirm password" />
      id="confirm" redisplay="true" required="true"
      requiredMessage="Please confirm password" />

      for="confirm" />
      id="equal" components="password confirm"
      message="Passwords are not equal" />


      ...


      value="submit" action="#{register.submit}">
      execute="@form" render="@form" />




      If none of the fields are filled out, the component's own required validators will fire and the equal validator will not fire. If both fields are filled out and not equal, then the equal validator will fire and show the message on the associated with its own ID.



      Here's another example how the is useful for start date - end date validation, for example:





      columns="3">
      ...

      for="startDate" value="Start date" />
      id="startDate" value"#{booking.reservation.startDate}" required="true"
      requiredMessage="Please enter start date"
      converterMessage="Please enter format yyyy-MM-dd">
      pattern="yyyy-MM-dd" />

      for="startDate" />

      for="endDate" value="End date" />
      id="endDate" value"#{booking.reservation.endDate}" required="true"
      requiredMessage="Please enter end date"
      converterMessage="Please enter format yyyy-MM-dd">
      pattern="yyyy-MM-dd" />


      for="endDate" />
      for="order" />
      id="order" components="startDate endDate"
      message="End date must be after start date" />


      ...


      value="submit" action="#{booking.submit}">
      execute="@form" render="@form" />




      If none of the fields are filled out or filled out with invalid date format, then the component's own converter and required validators will fire and the order validator will not fire. If both fields are filled out with valid date formats but not in order, then the equal validator will fire and show the message on the associated with its own ID.



      Select items converter



      The SelectItemsConverter, written by Arjan Tijms, allows the developer to use complex objects like entities as the value of without the need to implement a custom converter which calls some DAO/service method to obtain the associated entity by for example its ID as submitted string value. The converter converts the submitted value based on the already-available values in the . The conversion is by default based on the toString() representation of the entity which is supposed to already return an unique enough representation.



      All you need to do is to specify the converter as converter="omnifaces.selectItemsConverter".



      value="#{bean.selectedEntity}" converter="omnifaces.SelectItemsConverter">
      value="#{bean.availableEntities}" var="entity"
      itemValue="#{entity}" itemLabel="#{entity.someProperty}" />




      Where the bean can look like this:





      private Entity selectedEntity;
      private List availableEntities;

      // Getters+setter.




      You can always extend the SelectItemsConverter class to offer a custom getAsString() implementation which returns for example entity.getId() instead of the default entity.toString() which may in case of some entities be unnecessarily long.




      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.convert.FacesConverter;

      import org.omnifaces.converter.SelectItemsConverter;

      @FacesConverter("entitySelectItemsConverter")
      public class EntitySelectItemsConverter extends SelectItemsConverter {

      @Override
      public String getAsString(FacesContext context, UIComponent component, Object value) {
      Long id = ((Entity) value).getId();
      return (id != null) ? String.valueOf(id) : null;
      }

      }


      Note that you do not need to override getAsObject()!



      To get it to work, just change the converter attribute in the above example to point to your custom converter in question.





      value="#{bean.selectedEntity}" converter="entitySelectItemsConverter">
      value="#{bean.availableEntities}" var="entity"
      itemValue="#{entity}" itemLabel="#{entity.someProperty}" />




      Notice



      The OmniFaces component library is still in early development! Although we strive to full backwards compatibility, there will until the first stable 1.0 release be no guarantee that no changes will be made to the class names, tag names, attibute names, EL function names, the general behaviour and so forth.



      This blog is purely informal so that you can check/try them out and/or leave feedback. Important issues can be reported to the OmniFaces issue list.





      Source:http://balusc.blogspot.com/2012/03/omnifaces-project-has-started.html

Tidak ada komentar:

Posting Komentar