CodesJava

Easy learning with example program codes

Jsf messages html tag

JSF h:messages tag is used to render all messages for JSF UI Components in current page.

JSF tag:

<h:messages style="color:red;" />

Rendered HTML tag:

<ul style="color:red">
   <li>	UserName: Validation Error: Value is required </li>
   <li>	Password: Validation Error: Value is required </li>
</ul>

Attributes of h:messages tag.

AttributeDescription
idid for the tag
bindingReference to the component used in a backing bean
renderedA boolean value; false would suppress rendering
styleClassCascading stylesheet (CSS) class name
forThe component ID whose message is displayed
errorClassCSS class applied to error messages
errorStyleCSS style applied to error messages
fatalClassCSS class applied to fatal messages
fatalStyleCSS style applied to fatal messages
globalOnlyInstruction to display only global messages. Default: false
infoClassCSS class applied to information messages
infoStyleCSS style applied to information messages
layoutSpecification for message layout: table or list
showDetailA boolean that determines whether message details are shown. Defaults are false for h:messages, true for h:message
showSummaryA boolean that determines whether message summaries are shown. Defaults are true for h:messages, false for h:message
tooltipA boolean to set whether message details are rendered in a tooltip; the tooltip is only rendered if showDetail and showSummary are true
warnClassCSS class for warning messages
warnStyleCSS style for warning messages
styleInline style information
titleA title used for accessibility. Browsers typically create tooltips for the title’s value

Example:

welcome.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!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"
	xmlns:ui="http://java.sun.com/jsf/facelets">
	<h:head>
		<title>JSF messages example.</title>
	</h:head>
<h:body>
	<h3>JSF messages example.</h3>
	<h:form>
	<h:panelGrid id="panel" columns="2">
		<h:outputLabel value="UserName" />
		<h:inputText id="username" required="true">
		 <f:validateLength for="username" minimum="5" maximum="15"/>
		</h:inputText>
		<h:outputLabel value="Password" />
		<h:inputSecret id="password" required="true">
		 <f:validateLength for="password" minimum="5" maximum="10"/>
		</h:inputSecret>
		<f:facet name="footer">
		  <h:panelGroup style="display:block;text-align:center">
			<h:commandButton id="submit" value="Submit" />
		  </h:panelGroup>
		</f:facet>
	</h:panelGrid>
	<h:messages style="color:red;margin:10px;" />
	</h:form>
</h:body>
 
</html>

faces-config.xml

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="2.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xi="http://www.w3.org/2001/XInclude" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>
          javax.faces.webapp.FacesServlet
        </servlet-class>
    </servlet>
 
	<servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
 
</web-app>

URL:

http://localhost:7001/JSFExample21/faces/welcome.xhtml

Output:

JSF example21-1
Enter UserName and Password with less than 5 characters.
JSF example21-2
Click on Submit button.
JSF example21-3
 
Download this example.
 

Please follow and like us:
error
Posted in JSF   








Copyright © 2019 CodesJava DMCA.com Protection Status