Struts 2验证xml中的特定操作 [英] Struts 2 validate specific action in xml

查看:96
本文介绍了Struts 2验证xml中的特定操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想仅在动作类中验证特定方法。
操作方法就是这样。

I want to validate specific method only in action class. Action method is this.

public String add() throws Exception {

// aflag = true;

org.setAoName(aoName);
orgBo.addOrg(org);
orglist = orgBo.searchOrg(organisationSearch);
setAoName("");
setAflag("viewaddorgs");

return "add";
}

问题是目前我在动作类中有很多动作方法。当我调用这些方法时,它会验证此方法。请帮我用xml验证这个方法。

Problem is currently I have many action methods in action class. When I call those methods it validate this method. Please help me to validate only this method by xml.

这是我的 OraganisationAction-validation.xml

This is my OraganisationAction-validation.xml:

    <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

<validators>

    <field name="aoName">
        <field-validator type="requiredstring">
            <message>
                The name is required!
            </message>
        </field-validator>

        <field-validator type="requiredfield">
            <message>
                The name is required!
            </message>
        </field-validator>
    </field>


</validators>

这是我的jsp:

    <s:form id="post" action="addorgs" validate="true" >

    <s:textfield name="aoName" label="Add Organisations"  />

</s:form>

struts xml

struts xml :

<package name="default" extends="struts-default">


        <result-types>
            <result-type name="tiles"
                class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>

        <action name="*security" class="com.ast.action.admin.SecurityAction"
            method="{1}">
            <result name="second" type="tiles">secondTemplate</result>
            <result name="pass" type="tiles">orgTemplate</result>
        </action>


        <action name="*orgs" class="com.ast.action.admin.OraganisationAction"
            method="{1}">

            <result name="viewadd" type="tiles">orgAddTemplate</result>
            <result name="input" type="tiles">orgAddTemplate</result>
            <result name="add" type="tiles">orgAddTemplate</result>
            <result name="addtbl" type="tiles">orgAddTemplate</result>
            <result name="search" type="tiles">orgTemplate</result>
            <result name="delete" type="tiles">orgTemplate</result>
            <result name="viewedit" type="tiles">orgEditTemplate</result>
            <result name="edit" type="tiles">orgEditTemplate</result>
            <result name="edittbl" type="tiles">orgEditTemplate</result>
            <result name="orgmenu" type="tiles">orgTemplate</result>
            <result name="view" type="tiles">orgViewTemplate</result>
        </action>

它适用于注释但xml尚未成功。

it works with annotations but with xml no success yet.

推荐答案

使用操作别名:将不同的Action方法映射到struts.xml中的不同操作别名,而不是单个 yourAction-validation.xml 文件,为要执行的验证器添加 yourAction-yourAlias-validation.xml 仅限该方法。

Use an action alias: map different methods of an Action to different action aliases in struts.xml, and instead of the single yourAction-validation.xml file, add a yourAction-yourAlias-validation.xml for the validators you want to perform for that method only.

在此处阅读更多内容

编辑


现在没有任何验证。

No its not validation anything now.

<validators> 
    <field name="aoName">
        <field-validator type="required"> 
            <message>You cannot leave the aoName address field empty.</message>
        </field-validator> 
    </field>
</validators>

名称是 OraganisationAction-add-validation.xml 并将其与动作类包一起使用。有什么要在struts.xml中启用验证吗?

name is OraganisationAction-add-validation.xml and put it with action class package. Is there anything to enable validation in struts.xml ?

如果你有一个正确配置的(例如默认的)拦截器堆栈,验证已启用。如果您使用名为 OraganisationAction-add-validation.xml 的文件,则表示:

If you have a correctly configured (for example the default) Interceptor Stack, validation is already enabled. If you are using a file named OraganisationAction-add-validation.xml, that means that:


  • 您的操作CLASS必须命名为 OraganisationAction.java ;

  • 您在struts.xml中的操作映射必须是以下内容:

  • your action CLASS must be named OraganisationAction.java;
  • your action mapping in struts.xml must be the following:

<action name="add" class="your.package.OraganisationAction" method="add">
    <result>yourResult.jsp</result>
</action>


查找拼写错误,检查拦截器堆栈,它会自动运作。

Look for typos, check your Interceptor Stack, and it will work automatically.

这篇关于Struts 2验证xml中的特定操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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