2

我的 Struts2 Action 类如下所示,我希望 actionerror 消息应该使用标记显示在我的 JSP 页面中:<s:actionerror />

但是消息没有显示,我发现如果我将getModel()方法更改return form;return null;,错误消息可能会再次显示!如何在getModel()方法中返回表单对象的同时显示错误消息?

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;
    }

}

更新于 2015 年 10 月 20 日 - 我的 JSP(它是瓦片模板页面)

请注意,即使我将语句更改<s:if test='%{#session.hasError == "Y"}'><s:if test="hasActionErrors()">,结果也是一样的

<!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>
4

1 回答 1

0

经过调查,我终于参考了这个问题: Passing parameters to action through ModelDriven in Struts 2

我认为原因是模型驱动拦截器将模型推送到值堆栈的顶部(即 0-index),因此 jsp 无法访问 actionError(原始 0-index 是操作类)。

<s:actionerror/>页面可以使用而不是 using来显示 actionError <s:property value="[1].actionErrors[0]"/>,我不确定这是否是一个好方法,但它可以达到目的。

于 2015-10-26T07:49:31.330 回答