在页面加载而不是提交时进行struts验证 [英] struts validation occurring when page loads instead of on submit

查看:44
本文介绍了在页面加载而不是提交时进行struts验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我;我对Struts有点陌生.我遇到一个问题,即页面加载而不是我实际提交表单时发生了验证.我整日都在论坛上搜寻和搜寻,没有任何运气.我显然做错了一些事情,应该很容易确定,但是我还没有发现我的问题所在.

Please forgive me; I am sort of new to struts. I am having an issue where my validation is occurring when the page loads instead of when i actually submit my form. I have been googling and scouring through forums all day and haven't had any luck. I'm obviously doing something incorrectly that should be very easy to nail down, but i haven't found what my issue is yet.

这是我的struts.xml的摘录:

Here is a snippet of my struts.xml:

<action name="*Test" method="{1}" class="testClass">
            <interceptor-ref name="defaultStack"/>          
            <result name="init">jsp/index.jsp</result>
            <result name="input">jsp/index.jsp</result>
            <result name="submit">jsp/results.jsp</result>
</action>

如您所见,如果验证成功,我有一个动作要提交给results.jsp.否则,我希望它再次显示我的index.jsp.据我所知,这些页面可以正确导航.我可以导航到app/initTest.action并提交表单,它将带我到结果页面并显示我的预期结果.我的问题在于验证.

As you can see, I have one action that I want to submit to results.jsp if the validation is successful. Otherwise, i want it to show my index.jsp again. the pages navigate correctly, as far as i can tell. I can navigate to app/initTest.action and submit my form and it will take me to the results page and show me my expected results. My issue lies with the validation.

似乎验证工作在错误的时间进行.我在与操作绑定到的类所在的相同位置创建了[Action] -validation.xml,并且在页面最初加载时,我已经在页面上看到此xml文件的错误消息.

It seems as though the validation is working, just at the wrong time. I have a [Action]-validation.xml created in the same location as my class that the action is tied to and when the page loads initially, i see the error message from this xml file on the page already.

这是我的Validation.xml文件中的内容:

Here's what i have in my validation.xml file:

<validators>
    <field name="TestBean.idNumber">
        <field-validator type="requiredstring">
            <message>Please select an item.</message>
        </field-validator>
    </field>
</validators>   

您能提供的任何帮助将不胜感激!

Any assistance you can provide would be greatly appreciated!

谢谢!

@nmc,我正在尝试可视化使用单独操作的选项.会是这样吗?如果验证失败,它将重定向回原始表单页面吗?

@nmc, I'm trying to visualize the option of using a separate action. Would it be something like this? If the validation fails, will it redirect back to the original form page?

<action name="*Test" method="{1}" class="testClass">         
        <result name="init">jsp/index.jsp</result>
        <result name="submit">initTestResults</result>
</action>
<action name="*TestResults" method="{1}" class="testClass2">
        <interceptor-ref name="defaultStack"/>          
        <result name="init">jsp/indexResults.jsp</result>
        <result name="input">jsp/indexResults.jsp</result>
</action>

我正在尝试找出哪个选项更适合我的目的.

I'm trying to figure out which option is better for my purpose.

感谢您的输入.

推荐答案

验证运行在验证文件名中指定的操作之前,而不考虑结果的 .因此,如果您有一个要同时显示和处理表单的动作,则将在显示之前和处理表单之前对该动作进行验证.

The validation runs before the action as specified in the validation file name, regardless of the result. So if you have one action to both display and process the form, then validation will run on that action before displaying and also before processing the form.

如果这不是您想要的行为,请将页面的显示/加载移动到其他操作.

If this is not the behavior you want, move the display/load of the page to a different action.

这篇关于在页面加载而不是提交时进行struts验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆