Struts 2中没有变量和访问器(设置器/获取器)的参数以及参数拦截器 [英] Parameters with no mutators and accessors (setters/getters) along with the parameters interceptor in Struts 2

查看:113
本文介绍了Struts 2中没有变量和访问器(设置器/获取器)的参数以及参数拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的动作类中,我正在使用参数拦截器.

In the following action class, I'm using the parameters interceptor.

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value = "struts-default")
@InterceptorRefs(@InterceptorRef(value="store", params={"operationMode", "AUTOMATIC"}))
public final class TestAction extends ActionSupport implements Serializable, ValidationAware, Preparable
{
    private static final long serialVersionUID = 1L;

    private String param1;
    private String param2;

    //Getters and setters.

    public TestAction() {}

    @Action(value = "TestMessage",
        results = {
            @Result(name=ActionSupport.SUCCESS, type="redirectAction", params={"namespace", "/admin_side", "actionName", "Test"}),
            @Result(name = ActionSupport.INPUT, location = "Test.jsp")},
        interceptorRefs={
            @InterceptorRef(value="paramsPrepareParamsStack", params={"params.acceptParamNames", "param1, param2", "params.excludeParams", "extraParam", "validation.validateAnnotatedMethodOnly", "true"})
        })
    public String insert() {
        // Do something. Add or update a row to the database (one at a time).
        addActionMessage("Action message");
        addActionError("Error message");
        return ActionSupport.SUCCESS;
    }

    @Action(value = "Test",
    results = {
        @Result(name = ActionSupport.SUCCESS, location = "Test.jsp"),
        @Result(name = ActionSupport.INPUT, location = "Test.jsp")},
    interceptorRefs = {
        @InterceptorRef(value = "paramsPrepareParamsStack", params = {"params.acceptParamNames", "param1, param2", "params.excludeParams", "extraParam", "validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
    public String load() throws Exception {
        // This method is just required to return an initial view on page load.
        return ActionSupport.SUCCESS;
    }

    @Override
    public void prepare() throws Exception {}
}

以下是<s:form>:

<s:form namespace="/admin_side" action="Test" validate="true" id="dataForm" name="dataForm">
    <s:if test="hasActionMessages()">
        <s:actionmessage theme="jquery"/>
    </s:if>

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

    <s:hidden name="param1"/>
    <s:hidden name="param2"/>
    <s:hidden name="extraParam"/>
    <s:submit value="Submit" action="TestMessage"/>
</s:form>

这里,未声明隐藏的表单域extraParam,因此在动作类中没有设置方法和获取方法.

Here, the hidden form field extraParam is not declared and consequently, has no setter and getter in the action class.

在这种情况下,提交此表单后,以下消息将显示在服务器终端上.

In this case, the following message appears on the server terminal, when this form is submitted.

严重:开发人员通知(将struts.devMode设置为false以禁用 此消息):意外的异常捕获设置上的'extraParam' 'class actions.TestAction:使用以下命令设置表达式'extraParam'时出错 值['',]

SEVERE: Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'extraParam' on 'class actions.TestAction: Error setting expression 'extraParam' with value ['', ]

params.excludeParams不像操作类中一样排除参数extraParam.

params.excludeParams does not exclude the parameter extraParam as in the action class.

我们可以在使用参数拦截器时以某种方式防止此类异常吗?如果根本不显示此类消息,则将它们不必要地添加到操作消息中并通过<s:actionmessage/>进行显示.

Can we somehow prevent such exceptions while using the parameters interceptor. Such messages are unnecessarily added to action messages and displayed through <s:actionmessage/>, if used, when they are not supposed to be shown at all.

如果在操作类中将此paramsPrepareParamsStack替换为defaultStack,则不会出现此类消息.它只是发出如下警告.

If this paramsPrepareParamsStack is replaced with defaultStack in the action class then, such messages don't appear. It just gives a warning as follows.

警告:参数[extraParam]位于以下项的excludeParams列表中: 模式!

WARNING: Parameter [extraParam] is on the excludeParams list of patterns!

请不要只是说,struts.devMode设置为false以禁用 这样的消息.

Please don't just say, set struts.devMode to false to disable such messages.

推荐答案

我已经在注释中说过,如果您定义自己的参数集来覆盖拦截器参数,则拦截器参数不会被父包的拦截器配置继承.默认设置.请参见拦截器参数重写继承.

I've already said in the comment that interceptor parameters are not inherited by the interceptor configs of the parent packages if you define your own set of parameters that override the default settings. See Interceptor Parameter Overriding Inheritance.

也有一些技术可用于获取两种不同的拦截器参数图,请参见在Struts2中获取拦截器参数.

There are also some techniques used to obtain two different maps of interceptor parameters, see Getting Interceptor Parameters in Struts2.

约定插件创建从某些父程序包继承的XWork程序包配置.请参阅 Struts 2 Convention Plugin定义多个父包的答案.

The convention plugin creates XWork package configs that inherited from some parent package. See my answer for Struts 2 Convention Plugin Define Multiple Parent Packages.

因此,如果要向集合添加自己的参数,您要做的就是覆盖父配置的默认参数集. interceptor标记或interceptor-stack标记,您应该为每个interceptor-ref标记执行此操作.

So, all you have to do is to override the default parameter set by the parent configuration if you want to add your own parameters to the set. Either interceptor tag or interceptor-stack tag and you should do it for each interceptor-ref tag.

约定插件使用 @InterceprorRef 注释出于相同的目的,但要注意的是,如果将其应用于该类,它将应用于该类的每个动作.因此,在类级别使用此批注时要小心.您将覆盖拦截器堆栈参数,因此对于每个参数名称,都应在堆栈中引用拦截器名称的前缀后加点号,但这仅在堆栈中具有interceptor-ref的唯一名称时才有效.

The convention plugin uses @InterceprorRef annotation for the same purpose but with a caveat that if applied on the class, it applies on each action of that class. So, be careful when using this annotation on the class level. You are overriding the interceptor stack parameters, so you should use a prefix followed by dot of the interceptor name referenced in the stack for each parameter name, but this only works if you have unique names of the interceptor-refs in the stack.

如果在paramsPrepareParamsStack中有两个params拦截器的引用,则无法覆盖第二个params interceptor-ref,除非您创建自己的拦截器堆栈并在拦截器的每个引用上指定参数覆盖.

If you have two references of the params interceptor in the paramsPrepareParamsStack then you can't override the second params interceptor-ref unless you create your own interceptor stack and specify parameter overrides on each reference of the interceptor.

这篇关于Struts 2中没有变量和访问器(设置器/获取器)的参数以及参数拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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