JSF - h:message


Advertisements


The h:message tag displays message corresponding to UI element.

JSF Tag

<h:inputText  id="username"  size="20" label="UserName" required="true">
   <f:validateLength for="username" minimum="5" maximum="20" />			
</h:inputText>
<h:message for="username" style="color:red" />

Rendered Output

In case username entered is more than 20 characters.

<span style="color:red">UserName: Validation Error: 
Length is greater than allowable maximum of '20'</span>

In case username entered is less than 5 characters.

<span style="color:red">UserName: Validation Error: 
Length is less than allowable minimum of '5'</span>

In case username is not entered.

<span style="color:red">UserName: Validation Error: 
Value is required</span>

Tag Attributes

S.N.Attribute & Description
1id

Identifier for a component

2binding

Reference to the component that can be used in a backing bean

3rendered

A boolean; false suppresses rendering

4styleClass

Cascading stylesheet (CSS) class name

5for

The ID of the component whose message is displayed�applicable only to h:message

6errorClass

CSS class applied to error messages

7errorStyle

CSS style applied to error messages

8fatalClass

CSS class applied to fatal messages

9fatalStyle

CSS style applied to fatal messages

10globalOnly

Instruction to display only global messages�applicable only to h:messages. Default: false

11infoClass

CSS class applied to information messages

12infoStyle

CSS style applied to information messages

13layout

Specification for message layout: table or list�applicable only to h:messages

14showDetail

A boolean that determines whether message details are shown. Defaults are false for h:messages, true for h:message

15showSummary

A boolean that determines whether message summaries are shown. Defaults are true for h:messages, false for h:message

16tooltip

A boolean that determines whether message details are rendered in a tooltip; the tooltip is only rendered if showDetail and showSummary are true

17warnClass

CSS class for warning messages

18warnStyle

CSS style for warning messages

19style

Inline style information

20title

A title, used for accessibility, that describes an element. Visual browsers typically create tooltips for the title’s value

Example Application

Let us create a test JSF application to test the above tag.

StepDescription
1Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Application chapter.
2Modify home.xhtml as explained below. Keep rest of the files unchanged.
3Compile and run the application to make sure business logic is working as per the requirements.
4Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
5Launch your web application using appropriate URL as explained below in the last step.

home.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>JSF Tutorial!</title>
</head>
<body>
   <h2>h:messages example</h2>
   <hr />
   <h:form>
      <h:panelGrid id="panel" columns="3" border="0" cellpadding="10" 
         cellspacing="1">		
         <h:outputLabel value="Enter Username" />
         <h:inputText  id="username"  size="20" label="UserName" 
            required="true">
            <f:validateLength for="username" minimum="5" maximum="20" />			
         </h:inputText>
         <h:message for="username" style="color:red" />
         <h:outputLabel value="Enter Password" />
         <h:inputSecret id="password" size="20" label="Password" 
            required="true" redisplay="true" >
            <f:validateLength for="password" minimum="5" maximum="10" />	
         </h:inputSecret>
         <h:message for="password" style="color:red" />			
         <h:commandButton id="submit" value="Submit" action="result"/>
      </h:panelGrid>		   
   </h:form>
</body>
</html>

Once you are ready with all the changes done, let us compile and run the application as we did in JSF - First Application chapter. If everything is fine with your application, this will produce following result:

JSF h:message
jsf_basic_tags.htm

Advertisements