Discussions

Web tier: servlets, JSP, Web frameworks: STRUTS - Not able to display Messages Using ActionMessage

  1. I am encountering a problem with ActionMessages. The problem is that the message is not getting displayed in the jsp. I dont know why. I have place application.properties file under WEB-INF/.
     
     Pls advise. Thanks for your help.
     
     ActionCode
     ==========
      ActionMessages messages = new ActionMessages();
     
      try
        {
           messages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage(MessageConstant.DELETED));
        }
        catch (Exception e) {
           e.printStackTrace();
        }
        finally
        {
        }

        if(messages.size()>0)
        {
          saveMessages(request,messages);
        }

        return (mapping.findForward("success"));
      }

      ===================================
      
      MessageConstant.java
      ---------------------
      public static final DELETED="1111";
      
      ===================================
      
      struts-config.xml
      ------------------
      <message-resources parameter="application"/>

      application.properties
      ----------------------
      1111=Record Succesfully Deleted.
      
      ===================================
      Jsp Page.
    <logic:messagesPresent message="true">
    Action result(s):
    <ul><span>
             <html:messages id="message" message="true">
    <logic:present name="message">
  2. <bean:write name="message"/>

  3. </logic:present>
             </html:messages></span>
    </ul>
    </logic:messagesPresent>
      =====================================

Threaded Messages (22)

  • Place the ApplicationResources.properties file under WEB-INF\classes
    Also, it is recommended to place a copy of ApplicationResources.properties under the WEB-INF/src folder, as some build tools may clear all the contents of the classes dir if you build from scratch.
  • Hi Preeti,

    Thanks for your response, i tried placing the ApplicationResources.properties
    under WEB-INF/classes/...

    I am still encountering the same problem, could there any other reason.

    Kindly let me know.


    Following are the entries in struts-confign.xml and ApplicationResources.properties.

    <message-resources parameter="classes.ApplicationResources"/>
    ========
    1111=Record Succesfully Deleted
  • Change your JSP to read like this:

    <%@ taglib uri="struts/bean-el" prefix="bean" %>
    <%@ taglib uri="struts/html-el" prefix="html" %>
    <%@ taglib uri="struts/logic-el" prefix="logic" %>

    ..blah blah...

    <logic:messagesPresent message="true">
       Action result(s):
       <html:messages id="message" message="true">
         <span><c:out value="$null"/></span><br>
       </html:messages>
    </logic:messagesPresent>

    ....

    This will loop thru and display all the messages.
    Let me know if it works.
  • blah, blah?
  • that just stands for stuff like <head> <title> etc...
  • Preeti,

    I still cannot make it, I feel that the server is not able to locate the
    ApplicationResources.properties.
  • ok - i'll need some more info. Are you unable to see any message or are you getting some kind of an error?

    If you see a blank page (no error), change the entry in the struts-config.xml to:
    <message-resources parameter="ApplicationResources" null="false" />
    null=false means that if the JSP cannot locate the value for the key (in your case that is 1111), it will simply display the key.

    If you are getting an error, please post the stack trace. Also post the directory structure under the Webapps directory (assume you are using Tomcat...?)

    Also have a look at this page which lists common sstruts errors and causes.
  • Actually i am not coming across any error messages. I tried giving null=false
    i could not see the key being displayed.

    Only the test "Action Results : " is getting displayed. This text could be
    displayed only if <logic:messagesPresent> evaluates to true so <html:messges> is getting failed (My point of view).

    <logic:messagesPresent message="true">
       Action result(s):
       <html:messages id="message" message="true">
         <span><c:out value="$null"/></span><br>
       </html:messages>
    </logic:messagesPresent>


    Following is the location of applicationresources.properties location.

    WEB-INF
    --------|
    classes---------|
    com
    ApplicationResources.properties
    hibernate.properties
  • Following is the structure of my WEB_INF

    WEB-INF/classes/com
    WEB-INF/classes/ApplicationResources.properties
    WEB-INF/classes/hibernate.properties
  • I have the same problem but I can see what in the messages with this code I Found

    <%
      // Print all attributes in the request object
      out.println("All Attributes in request scope:");
      Enumeration paramNames = request.getAttributeNames();
      while (paramNames.hasMoreElements()) {
        String name = (String) paramNames.nextElement();
        Object values = request.getAttribute(name);
        out.println("<br> " + name + ":" + values);
      }
      
      // Print all attributes in the session object
      out.println("All Attributes in session scope:");
      paramNames = session.getAttributeNames();
      while (paramNames.hasMoreElements()) {
        String name = (String) paramNames.nextElement();
        Object values = session.getAttribute(name);
        out.println("<br> " + name + ":" + values);
      }

      out.println("Data in ActionMessages:");

      // Get the ActionMessages
      Object o = request.getAttribute(Globals.MESSAGE_KEY);
      if (o != null) {
        ActionMessages ae = (ActionMessages)o;

        // Get the locale and message resources bundle
        Locale locale =
          (Locale)session.getAttribute(Globals.LOCALE_KEY);
        MessageResources messages =
          (MessageResources)request.getAttribute
          (Globals.MESSAGES_KEY);

        // Loop thru all the labels in the ActionMessage's
        for (Iterator i = ae.properties(); i.hasNext();) {
          String property = (String)i.next();
          out.println("<br>property " + property + ": ");

          // Get all messages for this label
          for (Iterator it = ae.get(property); it.hasNext();) {
            ActionMessage a = (ActionMessage)it.next();
            String key = a.getKey();
            Object[] values = a.getValues();
            out.println(" [key=" + key +
              ", message=" +
              messages.getMessage(locale,key,values) +
              "]");
          }
        }
      }
    %>
  • Look at your view source[ Go to top ]

    Make sure your view source shows the desired contents. Im my case. view source was having the desired contents "style" class in the following table was forcing the tble to start from 10 pixels distance from the top left screen. It was stopping my message to be shown, no matter what. So what I mean to say is that there can be some HTML related problems stopping you from displaying the desired contents.

    Cheers
    Upen
  • Change your JSP to read like this:

    <%@ taglib uri="struts/bean-el" prefix="bean" %>
    <%@ taglib uri="struts/html-el" prefix="html" %>
    <%@ taglib uri="struts/logic-el" prefix="logic" %>

    ..blah blah...

    <logic:messagesPresent message="true">
    &nbsp;&nbsp;&nbsp;Action result(s):
    &nbsp;&nbsp;&nbsp;<html:messages id="message" message="true">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><c:out value="$null"/></span><br>
    &nbsp;&nbsp;&nbsp;</html:messages>
    </logic:messagesPresent>

    ....

    This will loop thru and display all the messages.
    Let me know if it works.
  • I did the same but I get this error

    org.apache.jasper.JasperException: File "/struts/bean-el" not found
    at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:105)
    at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:430)
    at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:154)
    at org.apache.jasper.compiler.TagLibraryInfoImpl.(TagLibraryInfoImpl.java:180)
    at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:354)
    at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:381)
    at org.apache.jasper.compiler.Parser.parseElements(Parser.java:795)
    at org.apache.jasper.compiler.Parser.parse(Parser.java:122)
    at org.apache.jasper.compiler.ParserController.parse(ParserController.java:199)
    at org.apache.jasper.compiler.ParserController.parse(ParserController.java:153)
    at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:227)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:369)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:473)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:190)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:193)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)
    at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:589)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:666)
    at java.lang.Thread.run(Thread.java:534)
  • Thanks for the post, helped me a lot, the c tag though was the only thing keepin me from the desired results, sorry if this post is uninformed and the c tag was implied. <%@ taglib uri="struts/bean-el" prefix="bean" %> <%@ taglib uri="struts/html-el" prefix="html" %> <%@ taglib uri="struts/logic-el" prefix="logic" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> ..blah blah... Action result(s):
  • can any one help me to diplay different messages in Action Messages in different colours.
  • Hi refer to

    http://www.systemmobile.com/articles/strutsMessageResources.html

    for info on ApplicationResource
  • Hi all, I am also having difficulty on displaying the struts error messages.

    Mine was a weird case:

    I was testing my app on my development server, and everything works fine. All the messages come up as they are supposed to. But when I deployed my application to another server, which is having the same configuration as my development server(as told by the server guy), the strut error messages did not come out when they are supposed to.
    I mean, it's the same .ear file. Is there anything that I missed here that will possibly affect the behavior of struts' error handling?
  • Hi Surayan,
      Make sure that ur web.xml contains the following entry..

    <init-param>
            <param-name>application</param-name>
            <param-value>ApplicationResources</param-value>
        </init-param>
        <init-param>

    and I m sure it will work.
  • bean taglib[ Go to top ]

    Hi! I hope this helps.

    I think you need this (found below):

    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

    if you're going to use this (found) :

    <bean:write name="message"/>
  • I had the exact same problem. There is no problem with the resource bundle beacause the contents are showing up elsewhere. Silly error on my part though. I had redirect="true" in the action definition in the struts-config file. As soon as I changed it to redirect="false" the ActionMessages starting showing up.
  • From Horst[ Go to top ]

    Dears,
    I had solved the situation by putting a copy of ApplicationResources.properties at /WEB-INF/classes/ .
  • I need a help on Struts ActionMessage class. I am using ActionMessages to Show Errors collected in processing Action class. I am unable to get the messages displayed in my JSP. Even messages are present in ActionErrors Object, JSP shows Blank space instead of message. Can some buddy look into my problem. Code in my action is like------- Try{ //Action Specific code. } catch(ExecuteOperationException eoe){ eoe.printStackTrace(); ActionMessage actionMessage = new ActionMessage("error.UserForm.LastName.NotPresent",false); errors.add("ErrorMessage",actionMessage); System.out.println("Key = "+actionMessage.getKey()+" /n Value = "+actionMessage.getValues()+" /n isResource = "+actionMessage.isResource()); saveErrors(request, errors); System.out.println("Error = "+ errors); Iterator it = errors.get(); while(it.hasNext()){ System.out.println(" Item == "+it.next()); } forward = mapping.findForward("failure"); Struts Entry for Action and message is as given below. also my message Resource file is error.UserForm.FirstName.NotPresent=Please insert value for First Name. error.UserForm.LastName.NotPresent=Please insert value for Last Name. error.UserForm.action.Problem={0}Sandeep error.field.required=Value Required at given spot. And My JSP is <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <script language="javascript"> resetPaintMessage(); <logic`:messagesPresent> <html:messages id="msg" property="ErrorMessage" bundle="error"> paintErrorMessage('<bean:write name="msg"/>'); </html:messages> </logic:messagesPresent> closePaintMessage(); </script> Can Someone try to Solve my problem. Thanking you Sandeep