Ropardo Sowftware development company

Experience software development with ROPARDO S.R.L.

RSS Feed
RSS Feed
  • Home
  • About ROPARDO S.R.L
  • Our websites

Adding error messages in JSF

When developing applications with JSF, there is the need to display error messages back to the user, if a user submitted form contains errors, or if the processing of the data went wrong.

For this request, JSF meets the <h:message/> tag.

Here’s how you can display error messages back to the user:

Practically, the tag acts as a holder, or as a div inside which the error message can be displayed. You can put this tag wherever you think it visually shows better for the user to notice it.
This tag used to display the most recent message for the component, and it needs to be linked with
a specific component (JSF component like an input field that needs to have its value checked), for which it will hold the error messages. This is done by using the for attribute for it.

<h:message for="catName"/>
<h:inputText value="#{managedBean.property}" id="catName" />

Notice how the for attribute’s value matches the component id. Each time an invalid value is entered inside the input, the error message associated for it, will display in place of the tag.

Note! that this binding will only work for JSF elements. If you try to bind the tag to a regular HTML element’s id, it will not display the error message on page, but rather only inside the application’s server log.

How do you decide which values are considered valid and which are not?

You can do two kinds of validation:

  • JSF Standard Validators
  • Custom server side validation

JSF Standard Validators
When using the standard Validator implementations, you don’t need to write any code to perform validation.
There are three standard JSF validators:

  • <f:validateLength/> for validating a String with a minimum and maximum number of characters
  • <f:validateDoubleRange/> for validating a double value within an optional range
  • <f:validateLongRange/&gt; for validating a long value within an optional range

Example
Validate whether the length of the value is in the [1,10] range

<h:message for="name" />

<h:inputText id="name" required="true">
      <f:validateLength maximum="10" minimum="1"/>
</h:inputText>

Custom server side validation
You can build your own custom validator (not described in this article, but you can find it here)
Or, at any time in your application logic, you can get the faces context and add an error message for a specific component.

FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage("Error message here");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(componentId, message);

And this will display the error message inside the <h:message/> tag associated with the JSF component (can be a <h:form>, <h:dataTable> or any other JSF component)

Styles
These error messages can also have a CSS style class associated with them, for each level of error severity. This can be set by using some of the provided attributes for the element:

  • errorClass CSS class applied to error messages
  • fatalClass CSS class applied to fatal messages
  • infoClass CSS class applied to information messages
  • warnClass CSS class for warning messages
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
Get Shareaholic
Tags: display errors error messages JSF validation

 Posted in: Java, Web Development
June 22, 2010 | Alina Hila | 2 Comments

2 Responses

  • How can I get access privileges for files on my external hard-drive? | SEM
    June 22, 2010
    1

    [...] Adding error messages in JSF | Ropardo Blog [...]

  • stan
    September 23, 2010
    2

    Its Definitely good bookmarking for future reference.

Leave a Reply

 


  • « Previous post
  • Next post »
  • Recent Posts

    • Checkboxes unchecked value
    • Meet Ropardo at CeBit 2012
    • Installing PyGraphviz on Windows
    • Convert python object to XML representation
    • Liferay Portlet Development
  • Ropardo is Hiring

  • Subscribe

    • Add to Google Reader or Homepage Add to netvibes TopOfBlogs
  • Recent Comments

    • Ioan Seicean on Installing PyGraphviz on Windows
    • NY on Installing PyGraphviz on Windows
    • NY on Installing PyGraphviz on Windows
    • NY on Installing PyGraphviz on Windows
    • NY on Installing PyGraphviz on Windows
  • Archives

    • March 2012 (1)
    • February 2012 (1)
    • November 2011 (1)
    • September 2011 (4)
    • July 2011 (3)
    • June 2011 (2)
    • May 2011 (4)
    • April 2011 (4)
    • March 2011 (3)
    • February 2011 (2)
    • January 2011 (2)
    • December 2010 (1)
    • November 2010 (4)
    • October 2010 (4)
    • August 2010 (3)
    • July 2010 (3)
    • June 2010 (6)
    • May 2010 (8)
    • April 2010 (7)
    • March 2010 (9)
    • February 2010 (6)
    • January 2010 (5)
    • December 2009 (7)
    • November 2009 (9)
    • October 2009 (10)
    • September 2009 (14)
    • August 2009 (10)
    • July 2009 (1)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (1)
    • March 2009 (1)
    • October 2008 (3)
    • October 2007 (3)
    • July 2007 (4)
    • June 2007 (1)
    • May 2007 (3)
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Categories

    • News (16)
    • Ropardo Team (8)
    • Ropardo Products (6)
      • File Tracking Client (4)
      • iManagement (2)
    • Software Development (83)
      • Microsoft.NET (22)
      • Java (41)
      • Oracle (8)
      • Power Builder (3)
      • Liferay (5)
      • Lotus Notes (9)
      • xWiki (4)
    • System Adminstration (13)
      • Linux (10)
      • Windows (3)
    • Programming (1)
    • Uncategorized (3)
    • Databases (10)
      • MSSQL (5)
      • PostgreeSQL (3)
    • Microsoft.NET (1)
    • Web Development (29)
      • ASP/ASPX (3)
      • Content Management Systems (1)
      • HTML/CSS (6)
      • Javascrip/AJAX (8)
      • PHP (7)
    • Oracle E Business Suite (6)
  • Tags

    .NET ajax blog C# certification client CMS control css database Debugging django Domino Eclipse extension file tracking filter fun gentoo google Hibernate how to html image iManagement import Java javascript jQuery liferay Linux Lotus Notes lotus script Oracle Oracle BI Publisher 11g PHP portal PostgreSQL powerbuilder Python SQL Telerik velocity xml Xwiki

© 2012 ROPARDO s.r.l..

Powered by WordPress. Styled by Ropardo