Class ActionMessages


  • public class ActionMessages
    extends Object
    Storage for various messages (success messages, information messages and so on). The stored messages can be shown in JSP with help of <bs:messages/> tag where bs prefix is bounded to /WEB-INF/tags URI. The messages are stored in the session, once the message is shown it is deleted automatically after the request processing is finished.

    ActionMessages class is responsible for proper messages XML escaping. If the client wants to pass a raw HTML there, he/she should be using addRawMessage(String, String, String...) explicitly. In this case the responsibility is passed to the client.

    See Also:
    ActionMessage
    • Constructor Detail

      • ActionMessages

        public ActionMessages()
        Creates a new storage
    • Method Detail

      • getOrCreateMessages

        @NotNull
        public static ActionMessages getOrCreateMessages​(@NotNull
                                                         javax.servlet.http.HttpServletRequest request)
        Obtains ActionMessages object from the specified request or creates a new one if there is no such object.
        Parameters:
        request - request
        Returns:
        ActionMessages object
      • getMessages

        @Nullable
        public static ActionMessages getMessages​(@NotNull
                                                 javax.servlet.http.HttpServletRequest request)
        Obtains ActionMessages object from the specified request.
        Parameters:
        request - request
        Returns:
        ActionMessages object or null
      • addMessage

        public void addMessage​(@NotNull
                               ActionMessage message)
        Adds a message with specified key to the storage.
        Parameters:
        message - message
      • addMessage

        public void addMessage​(@NotNull
                               String key,
                               @NotNull
                               String message)
        Adds a message with specified key to the storage.

        The caller cannot pass raw HTML message using this method, as the message will be escaped.

        Parameters:
        key - message key
        message - message
      • addMessage

        public void addMessage​(@NotNull
                               String key,
                               @NotNull
                               String message,
                               @NotNull
                               String... parameters)
        Adds a message with specified key to the storage. Message can contain placeholders for parameters.

        The caller cannot pass raw HTML message using this method, as the message will be escaped.

        Parameters:
        key - message key
        message - message
        parameters - if specified then substrings like {0}, {1}, {2} and so on will be replaced with parameter with corresponding index. Each parameter will be passed through WebUtil.escapeXml(String) function before inserted into the resulting message.
        See Also:
        addRawMessage(String, String, String...)
      • addRawMessage

        public void addRawMessage​(@NotNull
                                  String key,
                                  @NotNull
                                  String message,
                                  @NotNull
                                  String... parameters)
        Adds a message with specified key to the storage. Message can contain placeholders for parameters.

        The caller is allowed to use raw HTML in a message (and thus is responsible of escaping of any untrusted user data there). The parameters are escaped, no HTML can be passed there.

        Parameters:
        key - message key
        message - message
        parameters - if specified then substrings like {0}, {1}, {2} and so on will be replaced with parameter with corresponding index. Each parameter will be passed through WebUtil.escapeXml(String) function before inserted into the resulting message.
        Since:
        7.1
        See Also:
        addMessage(String, String, String...)
      • removeMessage

        public void removeMessage​(@NotNull
                                  String key)
        Removes the message with the specified key from the storage.
        Parameters:
        key - message key
        Since:
        8.0
      • getMessage

        @Nullable
        public String getMessage​(@NotNull
                                 String key)
        Returns a string message with specified key and marks this message as read.
        Parameters:
        key - message key
        Returns:
        a message with specified key
      • getActionMessage

        @Nullable
        public ActionMessage getActionMessage​(@NotNull
                                              String key)
        Returns a message with specified key and marks this message as read.
        Parameters:
        key - message key
        Returns:
        a message with specified key
      • getMessages

        @NotNull
        public Collection<ActionMessage> getMessages()
        Returns all messages and all of them as read.
        Returns:
        collection of messages
      • hasMessage

        public boolean hasMessage​(@NotNull
                                  String key)
        Returns true if this storage contains a message with specified key
        Parameters:
        key - message key
        Returns:
        true if this storage contains a message with specified key
      • clearReadMessages

        public void clearReadMessages()
        Removes all read messages from the storage.
      • hasMessages

        public boolean hasMessages()
        Returns:
        true if this storage contains at least one message.
      • clearMessages

        public void clearMessages()
        Removes all currently stored messages