没有修改器和访问器(setter/getter)的参数以及 Struts 2 中的参数拦截器 [英] Parameters with no mutators and accessors (setters/getters) along with the parameters interceptor in Struts 2

查看:17
本文介绍了没有修改器和访问器(setter/getter)的参数以及 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 没有声明,因此在动作类中没有 setter 和 getter.

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 Define Multiple Parent Packages 的回答.

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-refs 在堆栈中.

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.

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

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