【问题标题】:Jsp ActionErrors StrutsJsp ActionErrors Struts
【发布时间】:2012-01-12 12:08:34
【问题描述】:

我的时区下午好。

我正在为一家公司完成一个 Java Web 项目,我需要编写在 actionForm 验证方法中检测到的错误。 在 validate 方法中,我以这种方式添加错误(代码的 sn-p):

ActionErrors ae = new ActionErrors();
ae.add("dummy",new ActionMessage("message"));
...java...code
 ae.add("dummy",new ActionMessage("message"));

在我的 jsp 中,我想做的是遍历找到的所有错误,并打印它们。 我不想通过以下方式把所有的错误:

<html:errors property="prop1"/>
<html...code/>
<html:errors property="prop2"/>

我想做的是这样的(伪代码):

if(there is errors)
   for(erros.hasNext)
      <td>errors.next()</td>

如果需要,我可以使用普通的 scriplets。 有谁能帮我听听吗?

提前致谢 最好的问候

【问题讨论】:

    标签: jsp struts struts-action


    【解决方案1】:

    只需使用&lt;html:errors/&gt;。请参阅 http://struts.apache.org/1.2.x/userGuide/struts-html.html#errors 了解标签的工作原理:

    property 错误消息应包含的属性名称 显示。 如果未指定,所有错误消息(无论 属性)显示

    (强调我的)

    【讨论】:

    • 但是我如何控制消息的显示?如果只使用 ,这会打印一个表格,一个列表?我想控制它,并且我想为每个错误消息添加一些 html id
    • 这在我链接到的文档中有所描述。您可能有自定义页眉和页脚,以及自定义前缀和后缀。我真的不明白 ID 有什么用处。如果您需要更大的灵活性,您还可以使用同一页面上描述的&lt;html:messages&gt; 标签遍历所有错误。
    • @tt0686 为每个错误元素添加一个 ID 似乎有点没有意义,如果有的话,使用一个类。不过,仍然没有必要,因为您可以将它们嵌入到另一个 DOM 容器中并使用 CSS 为其子项设置样式。
    【解决方案2】:

    我使用以下代码生成 Javascript 错误数组,您的情况应该非常相似。请注意使用 html:messages 而不是 html:errors

    <logic:messagesPresent>    
        ErrorMessages = new Array ();
         <html:messages id="message"> 
             ErrorMessages.push("<bean:write name='message' filter='false'/>");
        </html:messages>
    </logic:messagesPresent>
    <logic:messagesNotPresent>
    //some logic here
    </logic:messagesNotPresent>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多