Struts 2.5:正在验证但未显示任何消息

Struts 2.5: Validation occurring but no message displayed

好吧,过去 2 天我一直在用头撞砖墙,因为我的验证消息不会在 JSP 上输出,尽管验证显然正在发生......

我已经通过操作为必填字段设置了简单验证-validation.xml

<validators>
    <field name="firstname">
        <field-validator type="requiredstring">
            <message>First name is required</message>
        </field-validator>
    </field>
</validators>

Action class 扩展 ActionSupport 并连接 ServletRequestAware、Preparable、ModelDriven

Execute、prepare 和 validate 方法都已在操作 class 中被覆盖,其配置为

<package name="main" namespace="/" extends="struts-default">
    <action name="myform" class="com.uk.MyFormAction">
        <interceptor-ref name="store">
            <param name="operationMode">STORE</param>
        </interceptor-ref>
        <interceptor-ref name="defaultStack" />

        <result name="update">s2/user.jsp</result>
        <result name="input">s2/user.jsp</result>
    </action>
</package>

无论出于何种原因,来自 validation.xml 文件的验证都不会在我的 JSP 中输出任何内容

<s:if test="hasActionErrors()">
    <s:actionerror />
</s:if>

<s:textfield name="firstname" />

页面上还有其他字段,我只是想先让这个字段起作用。我已经添加了拦截器存储,它没有任何区别,并且对 type="redirectAction" 很感兴趣,但是当它被添加到我的输入结果中时,我在验证和提交时为操作准备方法之间得到一个无限循环...

我确定实际配置是正确的。当该字段存在时,提交会正确完成,但是当它为空时 struts 会重定向回 JSP 但不会给出错误消息。

我现在完全不知道哪里出了问题,有什么建议吗?

编辑:顺便说一句,如果我添加 addActionError() 通过我的验证函数,它会在页面上正确显示,但 validation.xml 消息再次丢失(不应该)

还有struts.ui.theme=simple正在使用

最近的 post 我发现与我的问题相关的可能是 但是消息拦截器似乎没有存储或拦截我的消息...我试图输出通过打印 getActionErrors 集合的大小来计算 ActionErrors 的数量,在我的执行和验证函数中 returns 0...我不确定如果正常工作是否应该 return validation.xml 错误

在我想要打印出错误消息的区域内,我有以下代码

<s:if test="hasActionErrors()">
    <s:actionerror />
</s:if>

<s:if test="hasActionMessages()">
    <s:actionmessage />
</s:if>

以上是针对 ActionErrors 和 ActionMessages 的,而不是针对字段错误的。你还需要的是

<s:fielderror />

因为我已经通过我的操作-valiation.xml 文件定义了字段错误!

通过 this post

获得帮助

您还可以为特定字段定义位置。 This post 解释说...