【发布时间】:2010-09-14 13:09:30
【问题描述】:
我不太熟悉在 JSF 中使用 <h:messages> 元素。我想要做的是使用它来显示由我的支持 bean 中的方法生成的不同严重程度的全局消息列表。将消息放入FacesContext 并不是什么大问题,我的代码是这样的:
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...
一切正常。我的问题是试图使<h:messages> 标签的输出在页面上看起来不错。这是我的 JSP 文件的相关部分:
<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>
这在页面上一直看起来像废话。我正在使用外部 CSS 样式表来定义样式类。我试过使用layout="list",但列表项跨越整个页面,使任何块样式看起来很糟糕,所以我切换到layout="table"。我将<h:messages> 框在<h:panelGrid> 中,这样我就有了一些可以应用样式的块级元素。看起来还是不是特别好看。
我现在得到的:
<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>
我想得到什么:
<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>
- 消息仍在表格布局中,但“摘要”和“详细信息”位于不同的列中
- 生成的表有一个直接分配给它的 CSS 类
- 消息的“摘要”和“详细信息”部分具有不同的样式类
- 很高兴:表格标题行
屏幕截图:
有没有人使用<h:messages> 标签并对我如何实现这一点有一些想法?
我不确定这个问题是否重要,但我使用的是 MyFaces 1.2。我目前无法升级到 2.x。
【问题讨论】:
-
这是 CSS 的问题。您的实际问题可能只是 CSS 技能不佳。在不知道它的外观的情况下,很难给出答案。请张贴一些图片/屏幕截图,说明它应该(不)是什么样子。
-
@BalusC 没错,我不是 CSS 专家。我将尝试模拟一些 HTML 来说明我的问题并将其附加到问题中。我没有任何地方可以托管屏幕截图。
-
您可以在这里上传图片。单击消息编辑器工具栏中的图像按钮以选择本地图像文件。
-
@BalusC 完成。我以前从未注意到截图上传按钮。
标签: java css web-applications jsf myfaces