我不太熟悉使用 <h:messages>
JSF 中的元素。我想要做的是使用它来显示由我的支持 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"
但是随后列表项跨越整个页面并使任何 block 样式看起来很糟糕,所以我切换到 layout="table"
.我陷害了 <h:messages>
在 <h:panelGrid>
里面所以我会有一些可以应用样式的 block 级元素。它看起来仍然不是特别好。
我现在得到的:
<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。
最佳答案
毕竟,您想要更改 HTML 输出。这真的不能用 CSS 来完成。 CSS更多的是给已有的HTML元素一个布局,而不是修改它们。除了修改 JavaScript(在这种特殊情况下这并不容易)之外,您还想让 JSF 更改其 HTML 输出。基本上有两种方式:
自己提供
MessagesRenderer
以便它以您想要的方式呈现消息。扩展 MyFaces 中使用的基本渲染器类(抱歉,因为我不使用 MyFaces,所以我无法从头顶判断它是用于 MyFaces 的类,您需要查阅它的 API 文档)并在faces-config
中注册它如下:<render-kit> <renderer> <component-family>javax.faces.Messages</component-family> <renderer-type>javax.faces.Messages</renderer-type> <renderer-class>com.example.CustomMessagesRenderer</renderer-class> </renderer> </render-kit>
在这里,
com.example.CustomMessagesRenderer
因此是您实现的自定义渲染器。你想覆盖encodeEnd()
标准渲染器的方法让它以所需的方式输出 HTML。此处作为示例发布的代码太多,但仅查看默认消息渲染器的源代码应该会提供足够的提示。收集
List<FacesMessage>
中的消息在FacesContext#getMessages()
的帮助下并在c:forEach
的帮助下显示它们或t:dataList
和普通的 HTML。例如public List<FacesMessage> getMessages() { List<FacesMessage> messages = new ArrayList<FacesMessage>(); Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(); while (iter.hasNext()) { messages.add(iter.next()); } return messages; }
与
<table border="1" id="myForm:messagesOutput" class="myMessagesTableClass"> <thead><tr><td>SUMMARY</td><td>DETAIL</td></tr></thead> <tbody> <c:forEach items="#{bean.messages}" var="message"> <c:set var="type" value="#{message.severity == 'SEVERITY_ERROR' ? 'error' : 'warn'}" /> <tr> <td class="#{type}-message-summary"><span>#{message.summary}</span></td> <td class="#{type}-message-detail"><span>#{message.detail}</span></td> </tr> </c:forEach> </tbody> </table>
在 JSF2 中,您会使用
#{facesContext.messageList}
直接代替#{bean.messages}
.
关于java - JSF - 如何在 <h :messages> in a visually appealing way? 中格式化我的全局消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3709152/