Struts 2.5:验证发生但未显示任何消息

时间:2017-06-22 13:15:50

标签: java validation jsp struts2

在过去的两天里,我的头撞在砖墙上,因为我的验证消息不会在JSP上输出,尽管验证显然正在发生......

我已通过action-validation.xml

为必填字段设置了简单验证
<validators>
    <field name="firstname">
        <field-validator type="requiredstring">
            <message>First name is required</message>
        </field-validator>
    </field>
</validators>

Action类扩展了ActionSupport和接口ServletRequestAware,Preparable,ModelDriven

执行,准备和验证方法都在操作类中被覆盖,其配置为

<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()viamy validate函数,它会在页面上正确显示,但同样缺少validation.xml消息(并且不应该)

正在使用struts.ui.theme=simple

我可以找到与我的问题有关的最近的帖子可以是found here但是消息的拦截器似乎没有存储或拦截我的消息...我试图通过打印输出ActionErrors的数量getActionErrors集合的大小,在我的执行和验证函数中返回0 ...如果工作正常,我不确定是否应返回validation.xml错误

1 个答案:

答案 0 :(得分:-1)

在我希望打印出错误信息的区域内,我有以下代码

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

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

以上是ActionErrors和ActionMessages,不适用于字段错误。你还需要的是

<s:fielderror />

我通过action-valiation.xml文件定义了字段错误!

通过this post

帮助

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

相关问题