I'm not too familiar with using the <h:messages>
element in JSF. What I'm trying to do is use it to display a list of global messages of varying severity that are generated by a method in my backing bean. Getting the messages into the FacesContext
isn't too much of a problem, and my code is along these lines:
FacesMessage message;
FacesContext context =
FacesContext.getCurrentInstance();
message = new FacesMessage(
FacesMessage.SEVERITY_INFO,
"test message summary 1",
"test message detail 1");
context.addMessage(null, message);
message = new FacesMessage(
FacesMessage.SEVERITY_WARN,
"test message summary 2",
"test message detail 2");
context.addMessage(null, message);
message = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"test message summary 3",
"test message detail 3");
context.addMessage(null, message);
// add more messages...
That all works fine. My problem is with trying to make the output of the <h:messages>
tag look good on the page. Here is the relevant piece of my JSP file:
<h:panelGrid border="1" columns="1" id="messagesPanelGrid">
<h:messages
id="messagesOutput"
showSummary="true"
showDetail="true"
layout="table"
infoClass="info-message"
warnClass="warn-message"
errorClass="error-message"
fatalClass="fatal-message"
globalOnly="true" />
</h:panelGrid>
This consistently looks like crap on the page. I am using an external CSS stylesheet to define styling classes. I've tried using layout="list"
but then the list items span the whole page and make any block styling look bad, so I switched to layout="table"
. I framed the <h:messages>
inside a <h:panelGrid>
so I would have some block-level element that I could apply styles to. It still doesn't look particularly nice.
What I'm getting now:
<table border="1" id="myForm:messagesOutput">
<tr><td class="error-message"><span>no port name match Could not match reported port name XXXX to a known port for vessel VESSEL A</span></td></tr>
<tr><td class="error-message"><span>no port name match Could not match reported port name YYYY to a known port for vessel VESSEL B</span></td></tr>
<tr><td class="error-message"><span>no port name match Could not match reported port name YYYY to a known port for vessel VESSEL C</span></td></tr>
<tr><td class="error-message"><span>no port name match Could not match reported port name ZZZZ to a known port for vessel VESSEL D</span></td></tr>
<tr><td class="warn-message"><span>arrival date missing No arrival date provided for vessel VESSEL B</span></td></tr>
</table>
What I'm trying to get:
<table border="1" id="myForm:messagesOutput" class="myMessagesTableClass">
<thead><tr"><td>SUMMARY</td><td>DETAIL</td></tr></thead>
<tr><td class="error-message-summary"><span>no port name match</span></td><td class="error-message-detail"><span>Could not match reported port name XXXX to a known port for vessel VESSEL A</span></td></tr>
<tr><td class="error-message-summary"><span>no port name match</span></td><td class="error-message-detail"><span>Could not match reported port name YYYY to a known port for vessel VESSEL B</span></td></tr>
<tr><td class="error-message-summary"><span>no port name match</span></td><td class="error-message-detail"><span>Could not match reported port name YYYY to a known port for vessel VESSEL C</span></td></tr>
<tr><td class="error-message-summary"><span>no port name match</span></td><td class="error-message-detail"><span>Could not match reported port name ZZZZ to a known port for vessel VESSEL D</span></td></tr>
<tr><td class="warn-message-summary"><span>arrival date missing</span></td><td class="warn-message-detail"><span>No arrival date provided for vessel VESSEL B</span></td></tr>
</table>
- Messages are still in a table layout, but the "summary" and "detail" are in separate columns
- Generated table has a CSS class directly assigned to it
- The "summary" and "detail" parts of the message have separate style classes
- NICE TO HAVE: Table header row
SCREENSHOT:
Does anyone out there work with <h:messages>
tags and have some ideas on how I can achieve this?
I'm not sure if it matters for this question, but I'm using MyFaces 1.2. I can't upgrade to 2.x at the moment.