JSF 2 html messages

JSF 2 html messages


Posted in : Java Posted on : July 25, 2012 at 10:28 AM Comments : [ 0 ]

In this tutorial you will learn about the JSF 2 h messages tag.

JSF 2 html messages

In this tutorial you will learn about the JSF 2 h messages tag.

<h:messages> tag of JSF HTML tag library is used to show the messages, based on the type of its severity i.e. an error, information message etc., for the concerned components. These generated messages can be customized by applying the different CSS styles.

In comparison to JSF h message and JSF h messages tag, <h:message> tag is used to show the severity message for an individual concerned component whereas the <h:messages> tag is used to show all the severity messages for the all concerned components like as a summary.

Attributes of JSF html messages

  • for : This is not a required attribute. This attribute refers to the component id for which messages has to be displayed.
  • globalOnly : This is not a required attribute. This attribute evaluates to a boolean value which is used to display the messages that are not associated to any component id. Default value is 'false'.

  • id : This is not a required attribute. This attribute acted as a component's identifier. If this attribute is used it makes the component uniquely identifiable inside it's closest parent.

  • rendered : This is not a required attribute. It evaluates a boolean expression. Presence of this attribute specifies that whether the current component should be rendered in the Rendered Response phase or not or worked on any later form submission. The default value of this attribute is defined to true.

  • showDetail : This is not a required attribute. This attribute evaluates to a boolean value that specifies whether the message should be displayed in detail or not. The default value of this attribute is 'false'.

  • showSummary : This is not a required attribute. This attribute evaluates to a boolean value that specifies whether the message should be displayed as a summary or not. The default value of this attribute is 'true'.

  • dir : This is not a required attribute that evaluates to the java.lang.String value which suggests the direction for the text that doesn't inherit the property of maintaining a direction (directionality). The allowable values are "LTR (Left-to-Right)" and "RTL (Right-to-Left)".

  • errorClass : This is not a required attribute. Using this attribute CSS style class can be applied to the displayed error messages.
  • errorStyle : This is not a required attribute. Using this attribute CSS style(s) can be applied to the error messages that has to be displayed.
  • fatalClass : This is not a required attribute. Using this attribute CSS style class can be applied to the fatal messages that has to be displayed.
  • fatalStyle : This is not a required attribute. Using this attribute CSS style(s) can be applied to the fatal messages that has to be displayed.
  • infoClass : This is not a required attribute. Using this attribute CSS style class can be applied to the information messages that has to be displayed.
  • infoStyle: This is not a required attribute. Using this attribute CSS style(s) can be applied to the information messages that has to be displayed.
  • lang : This is not a required attribute. This attribute is used to specify the language code for the component.
  • layout : This is not a required attribute. This attribute is used to specify the layout value for the error messages. Valid values of this attribute are the HTML's 'table' and 'list'. The default value is 'list'.

  • style : This is not a required attribute but, is used to specify the CSS style will be applied to this component when rendered.
  • styleClass : This is not a required attribute but, is used to specify the applicable CSS style space-separated list to be applied at the time of rendering.
  • title : This is not a required attribute. This attribute is used to specify the title information which will be displayed as tooltip about markup elements.
  • tooltip : This is not a required attribute. This attribute evaluates to a boolean value which specifies whether the message should be displayed in detail as tooltip text or not.

  • warnClass : This is not a required attribute. Using this attribute CSS style class can be applied to the warning messages.
  • warnStyle : This is not a required attribute. Using this attribute CSS style(s) can be applied to the warning message.
  • binding : This is not a required attribute. This attribute evaluates to a javax.faces.component.UIComponent specified for linking the component with the property of backing bean.

Example :

Here I am giving a simple example which will demonstrate you about how to use the JSF 2 h messages tag in the JSF applications. In the above sections as we have read about what the <h:messages> tag does. In this example we have used this tag to display the error messages when the components are specified with the 'required' attribute i.e. the input fields that are specified in the JSF view page are submitted without filled then the specified error messages will be displayed. So, to show how it is possible I have made the two JSF view pages one for taking the input and the other for showing the value what are filled by the user. And created a JavaBeans with the 'name' and 'address' properties with their setter and getter method to set and get the value.

Directory Structure

UserBean.java

package devmanuals;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="userBean")
@RequestScoped
public class UserBean {

String name;
String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

input.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"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<head>
<title>JSF 2 html messages</title>
</head>
<body>
<f:view>
<h3>User Detail</h3>
<h:form>
<h:messages style="color:red;margin:8px;" />
<table>
<tr>
<th><h:outputText value="Enter Your Name : "/></th>
<td><h:inputText id="text1" value="#{userBean.name}" required="true" label="Name" requiredMessage="Name Field is required" title="Name field"/> <br/></td>
</tr>
<tr>
<th><h:outputText value="Enter Your Address : "/></th>
<td><h:inputTextarea id="textarea1" value="#{userBean.address}" required="true" label="Address box" requiredMessage="Address field is required" title="Address field"/> <br/></td>
</tr>
<tr>
<td></td><td><h:message for="textara1" style="color:red" /> </td>
</tr>
<tr>
<td></td>
<td><h:commandButton value="submit" action="output" title="submit"/></td>
</tr>
</table>
</h:form>
</f:view>
</body>
</html>

output.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"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<head>
<title>JSF 2 html messages</title>
</head>
<body>
<h:outputText value="Name : #{userBean.name}" /> <br/>
<h:outputText value="Address : #{userBean.address}" />
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsfHMessages</display-name>
<welcome-file-list>
<welcome-file>input.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>

How To Run this Example

After copying the above code paste them to their respective files or by downloading the code from Download Source Code start your web server and then use the following link http://localhost:8181/jsfHMessages/input.xhtml in the address bar of the web browser.

Output :

When you will execute the above code you will get the output as follows :

1. The main page will be looked as follows :

2. When you will submit the page without giving any value then the error messages for the related components will be displayed like as a summary.

3. But, when you will submit the page after giving values to the corresponding input fields then the output will be as follows

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics