有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Struts2 actionError在实现模型驱动的操作时未在jsp上显示

我的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(这是tiles模板页面)

注意,即使我将语句<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>

共 (1) 个答案

  1. # 1 楼答案

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

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

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