Display newsletters in jsp for Spring WebFlow 2.0

I have a model with a list of selected topics. Each theme can be represented in a room, and there are more rooms that must be selected based on availability. The user can select the preferred room for presentation. If it is not available, but some other one is available, an informational message should appear at the selected time that the room has been changed. Sort of:

<form:errors path="selectedTopics[${loop.index}].room" /> 

loop - varStatus attribute in c: foreach tag
I add messages to messageContext as follows:

 MessageContext messages = context.getMessageContext(); String source= "selectedTopics[" + i + "].room"; messages.addMessage(new MessageBuilder().info() .source(source) .code("topic.room.changed") .build()); 

The error tag works if I add them as errors, but they are not errors. Is there a way to display these informational messages in an elegant way, like errors?

+6
source share
1 answer

From DefaultMessageContext, I saw that there is a getter for all messages that gives an array of messages in the context of the message and than by cyclizing through the array, I can find the messages for the current field rendering:

 <c:forEach var="topic" items="${model.selectedTopics}" varStatus="loop"> //omitted displaying of topic details <c:forEach items="${flowRequestContext.messageContext.allMessages}" var="message"> <c:set var="msgSrc" value="selectedTopics[${loop.index}].room"></c:set> <c:if test="${message.source eq msgSrc}"> <c:if test="${message.severity eq 'INFO'}"> <span class="infoText">${message.text}</span> </c:if> </c:if> </c:forEach> </c:forEach> 

But in this way, iteration over all messages is performed for each field, which should be renderd, and if you have many fields, this can be slow.

Another way this can be achieved is to get a message map that is in the context of the message. Here is an example of the context output in jsp, I used $ {flowRequestContext.messageContext}:

 [ DefaultMessageContext@2de69e99 sourceMessages = map[[null] -> list[[empty]], 'selectedTopics[2].room' -> list[[ Message@12329bcb source = 'selectedTopics[2].room', severity = INFO, text = 'Room changed from ALU1-M1 to ALU1-M2']], 'selectedTopics[4].room' -> list[[ Message@87abf31 source = 'selectedTopics[4].room', severity = INFO, text = 'Room changed from ALU1-M1 to ALU2-M1']]]] 

There is a sourceMessages map that contains all the messages for a field that can be retrieved using the source as a key. But the problem is that there is no getter org.springframework.binding.message.DefaultMessageContext to implement the map implementation in the implementation. However, there is a getMessagesBySource method (source java.lang.Object) that gives an array of messages for the specified source. Therefore, we can use this in an EL expression.

IMPORTANT! Transmission Method Arguments in EL - This is only the EL specification supported in EL 2.2. EL 2.2 is shipped by default in Servlet 3.0 / JSP 2.2 containers. See JSTL or JSP 2.0 EL for getter with argument

Information can now be displayed using:

 <c:forEach var="message" items="${flowRequestContext.messageContext.getMessagesBySource(msgSrc)}"> <c:if test="${message.severity eq 'INFO'}"> <span class="info">${message.text}</span></td> </c:if> </c:forEach> 

If you need to use a previous version than Servlet 3.0 / JSP 2.2 containers, I think the best way is to restore the sourceMessages map and insert it into flashScope before rendering the view.

+5
source

Source: https://habr.com/ru/post/903766/


All Articles