I am using my own setup to set success/error messages into the session from managed-beans. Then I use a filter to grab them out of the session and stuff them into the request. This has worked quite well with other MVC frameworks. However, with JSF, the messages stay on the page - even though they've been removed from the session. I imagine this has something to do with how JSF tries to manage the state for me. How do I turn off saving state of a particular session/request attribute for a particular page?

The real strange thing is that messages actually stay on some JSPs that aren't even JSF pages (no <f:view>). Also, on other pages, I've experience strange behavior. For example, in a manage-bean's edit() method, I check to see if the user logged in with a cookie. If so, I set a message and display it on the page. For some reason, with MyFaces, the message won't show up on the page until I refresh the page. I'm guessing this might be because I'm including my message.jsp before any references are made to <f:view> or the managed-bean itself??

Files are below.

Thanks,

Matt


messages.jsp - included in SiteMesh decorator ---------------------------------------------------------

<%-- Error Messages --%>
<c:if test="${not empty errors}">
<div class="error"> <c:forEach var="error" items="${errors}">
<img src="<c:url value="/images/iconWarning.gif"/>"
alt="<fmt:message key="icon.warning"/>" class="icon" />
<c:out value="${error}" escapeXml="false"/><br />
</c:forEach>
</div>
<c:remove var="${sessionScope.errors}"/>
</c:if>


<%-- Success Messages --%>
<c:if test="${not empty messages}">
<div class="message"> <c:forEach var="msg" items="${messages}">
<img src="<c:url value="/images/iconInformation.gif"/>"
alt="<fmt:message key="icon.information"/>" class="icon" />
<c:out value="${msg}" escapeXml="false"/><br />
</c:forEach>
</div>
<c:remove var="${sessionScope.messages}"/>
</c:if>


BasePage.java - message/error setting methods:

public String getBundleName() {
// get name of resource bundle from JSTL settings, JSF makes this too hard
return FacesUtils.getServletContext().getInitParameter(jstlBundleParam);
}


public ResourceBundle getBundle() {
if (bundle == null) {
bundle =
ResourceBundle.getBundle(getBundleName(),
FacesUtils.getRequest().getLocale());
}


       return bundle;
   }

   public String getText(String key) {
       String message;

       try {
           message = getBundle().getString(key);
       } catch (java.util.MissingResourceException mre) {
           log.warn("Missing key for '" + key + "'");

           return "???" + key + "???";
       }

       return message;
   }

   public String getText(String key, Object arg) {
       if (arg == null) {
           return getBundle().getString(key);
       }

MessageFormat form = new MessageFormat(getBundle().getString(key));

       if (arg instanceof String) {
           return form.format(new Object[] { arg });
       } else if (arg instanceof Object[]) {
           return form.format(arg);
       } else {
           log.error("arg '" + arg + "' not String or Object[]");

           return "";
       }
   }

protected void addMessage(String key, Object arg) {
// JSF Success Messages won't live past a redirect, so I don't use it
//FacesUtils.addInfoMessage(formatMessage(key, arg));
List messages = (List) FacesUtils.getSession().getAttribute(MESSAGES);


       if (messages == null) {
           messages = new ArrayList();
       }

       messages.add(getText(key, arg));
       FacesUtils.getSession().setAttribute(MESSAGES, messages);
   }

   protected void addMessage(String key) {
       addMessage(key, null);
   }

protected void addError(String key, Object arg) {
// The "JSF Way" doesn't allow you to put HTML in your error messages, so I don't use it.
// FacesUtils.addErrorMessage(formatMessage(key, arg));
List errors = (List) FacesUtils.getSession().getAttribute(ERRORS);


       if (errors == null) {
           errors = new ArrayList();
       }

       errors.add(getText(key, arg));

       FacesUtils.getSession().setAttribute("errors", errors);
   }



Reply via email to