2

I have my Struts2 Action class as below and I am expecting the actionerror message should be displayed in my JSP page using the tag: <s:actionerror />

However the message did not show up, and I've found that if I change in the getModel() method return form; to return null;, the error message could be displayed again! How can I show the error message at the same time returning the form object in getModel() method?

public class StartSearchApplicationAction 
                                      extends ActionSupport 
                                   implements ModelDriven, SessionAware {

    protected Map<String, Object> session;

    private Formbean form;

    public String execute() {             
        addActionError("Testing Error Message");
        session.put("form", form);
        return "success";
    }

    public Formbean getModel() {
        form = (Formbean) session.get("form");
        if (form == null) {
            form = new Formbean();
        }
        return form;
    }

    public void setSession(Map<String, Object> session){
        this.session = session;
    }

}

Updated on 20-Oct-2015 - My JSP (it is the tiles template page)

Note that even I change the statement <s:if test='%{#session.hasError == "Y"}'> to <s:if test="hasActionErrors()">, the result is the same

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page errorPage="/jsp/error.jsp" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

<html>
<head>
<title><s:text name="global.heading.title"/></title>
</head>

<body>
<table class="main" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
      <table border="0" cellspacing="0" cellpadding="0">
    <tr>
          <td width="860" valign="top" bgcolor="#FFFFFF">
        <table border="0" valign="top" align="left">
<s:if test='%{#session.hasError == "Y"}'>
                  <tr>
            <table width="500" border="0" cellspacing="0" cellpadding="2" align="center" bgcolor="#006600">
            <tr> 
                <td width="16" class="ErrorMessageBoxTitle"><img src="/<s:text name="global.system.root"/>/images/smessage.gif" width="16" height="14" align="absmiddle" alt="System Errors"></td>
            </tr>
            <tr> 
                <td colspan="2" class="FixTdSize"> 
                <table width="496" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
                <tr> 
                    <td align="center" class="FixTdSize"> 
                    <table border="0" cellspacing="12" cellpadding="0" width="480">
                    <tr> 
                        <td width="35" class="ErrorMessageTitle"><img src="/<s:text name="global.system.root"/>/images/messager.gif" width="31" height="31" alt="System Errors"></td>
                        <td class="ErrorMessageTitle" width="409">&nbsp</td>
                    </tr>
                    <tr> 
                        <td width="35" class="ErrorMessageBody">&nbsp;</td>
                        <td class="label" width="409"><font color=red><s:actionerror/></font></td>
                    </tr>
                    </table>
                    </td>
                </tr>
                </table>
                </td>
            </tr>
            </table>
                  </tr>
          <tr><td>&nbsp;</td></tr>      
</s:if>
          <tr>
            <td height="30"><tiles:insertAttribute name="searchpanel"/></td>
          </tr>
          <tr>
            <td><img src="/<s:text name="global.system.root"/>/images/line.gif" width="100%" height="2"></td>
          </tr>
          <tr>
            <td><tiles:insertAttribute name="message"/></td>
          </tr>
          <tr>
            <td><tiles:insertAttribute name="body"/></td>
          </tr>
        </table>
      </td>
    </tr>
      </table>
    </td>
  </tr>
</table>
</table>

<tiles:insertAttribute name="menu"/>

</body>
</html>
Roman C
  • 49,761
  • 33
  • 66
  • 176
HK Cheung
  • 21
  • 3
  • Just stop using modeldriven. – Aleksandr M Oct 19 '15 at 08:06
  • I would like to keep action class implementing ModelDriven as it brings much convenient to me – HK Cheung Oct 19 '15 at 08:34
  • *it brings much convenient to me* - Such as? Not working validation? – Aleksandr M Oct 19 '15 at 08:37
  • Please, show your interceptor stack configuration – Andrea Ligios Oct 19 '15 at 08:37
  • ... and post your JSP. – Aleksandr M Oct 19 '15 at 08:48
  • @Aleksandr M : For example I can directly use the variable name in the jsp tag. Is it necessary to post the jsp since it is too long. And the only tag to display error message is which I can put it anywhere – HK Cheung Oct 19 '15 at 09:26
  • @Andrea Ligios : I am a newbie for Struts2, I did not add any interceptors to my struts.xml, is this answered your question? – HK Cheung Oct 19 '15 at 09:27
  • Is your `` in some `if`? Does `execute` method is being called? – Aleksandr M Oct 19 '15 at 09:36
  • @HKCheung yes, I was initially thinking of an exception caused by null session because of ModelDriven interceptor put before the ServletConfig interceptor (that is the one injecting the session in SessionAware actions). But it can't be, because it wouldn't change anything between returning null or form from the method. Post your JSP, really :) – Andrea Ligios Oct 19 '15 at 09:53
  • 1) Use `` and 2) Try printing `` before the s:if 3) Tell us IF THE EXECUTE METHOD IS RUN, by putting a system out inside it – Andrea Ligios Oct 20 '15 at 07:44
  • @Andrea Ligios : 1) Done 3) Yes, system out message appears 2) If `return form;`, the answer is FALSE, If `return null;`, the naswer is TRUE – HK Cheung Oct 20 '15 at 08:10
  • Maybe something with the tiles configuration ? Very strange – Andrea Ligios Oct 20 '15 at 08:20
  • 1. Make sure that your `getModel()` doesn't throw unhandled exceptions. 2. Does `execute` method being called in both cases? BTW your markup is invalid. – Aleksandr M Oct 20 '15 at 09:13
  • @HKCheung You should read how modeldriven works http://stackoverflow.com/a/18044786/573032 – Roman C Oct 20 '15 at 10:25
  • @Roman C : You mean one of the default interceptors cleared my actionerror value? But I still wonder why the result is different between `return form;` and `return null;` – HK Cheung Oct 22 '15 at 09:30
  • @HKCheung huh? it returns a model instance, the model affects the behavior of interceptors and valuestack state. – Roman C Oct 22 '15 at 09:35
  • @Roman C : The action of pushing model instance to the value stack is before addActionError, still it cleared my error message? And stop using modeldriven is my only choice? – HK Cheung Oct 23 '15 at 08:49
  • @HKCheung the validation action is also before it, if stopping the use of modeldriven is a choice why not you don't use it? – Roman C Oct 23 '15 at 11:30

1 Answers1

0

after investigation I finally did it with referencing to this question: Passing parameters to action through ModelDriven in Struts 2

I think the reason is Modeldriven interceptor pushes the model on top of the value stack (i.e. 0-index) and thus the jsp could not access the actionError (the original 0-index was the action class).

Instead of using <s:actionerror/>, the page could able to display the actionError using <s:property value="[1].actionErrors[0]"/>, I am not sure whether this is a good approach but it serve the purpose.

Community
  • 1
  • 1
HK Cheung
  • 21
  • 3
  • @Roman C : I finally come up with an answer from your another post :) thanks for your help! As a newbie of Struts2, would like to ask if you think this is an acceptable approach? I am worrying if there will be other drawbacks. – HK Cheung Oct 26 '15 at 07:56