如何在 Struts 2 中从更新中排除 ModelDriven Action 的某些属性 [英] How to exclude some properties of ModelDriven Action from being updated in Struts 2

查看:27
本文介绍了如何在 Struts 2 中从更新中排除 ModelDriven Action 的某些属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用带有 ModelDriven 接口的操作.该操作具有 scopedModelDriven 拦截器 并在会话中保存模型.

We are using an action with ModelDriven interface. The action has the scopedModelDriven interceptor and we save the model in session.

模型如下:

public class Fundtransfer{

private String amount;
private String sourceAccount;
private String destinationAccount;
private String sign

//setter's and getter's ommited
}

Fundtransfer bean 用作模型.JSP 表单具有数量、sourceAcountdestinationAcount 字段,它们通过 modelDriven 拦截器自动映射到 Fundtransfer bean.一切正常,直到听到!

The Fundtransfer bean is used as a model. The JSP form has amount, sourceAcount and destinationAcount fields which maps to Fundtransfer bean automatically via modelDriven interceptor. Every thing is normal till hear!

当然,用户可以简单地遍历 HTML 表单并将名为 sign 的变量传递给操作,拦截器将更新它的 bean.

Of course the user can simple tramped the HTML form and pass a variable named sign to the action and the interceptor will update it the bean.

我们是否可以配置一个模型,使得符号值无法通过 ModelDriven 拦截器更新.

Is it possible that we can configure a model in away that the sign value could not be updated via ModelDriven interceptor.

我知道我可以创建一个新 bean,然后将其复制到 Fundtransfer bean 或使用某种继承.有没有更好的方法.

I know I can create a new bean and then copy it to Fundtransfer bean or use some sort of inheritance. Are there better ways.

我们使用的真正的 bean 是巨大的,还有一些嵌套的 bean.

The real bean which we are using is huge and also some of the has nested beans.

我想也许我可以使用一些方面的技巧,但我不知道是否可行?

I thought may be I can use some aspect tricks but I don't know is it possible or not?

推荐答案

ModelDriven bean 通常用于通过 getter/setter 进行读/写.您可以在此处控制访问.但是,如果您还不知道 Struts2 通过 params<设置参数/code> 拦截器,它使用 OGNL 填充模型.你可以通过参数excludeParams来控制这个拦截器,例如

The ModelDriven bean usually used for read/write via the getters/setters. You can control access here. However, if you don't know yet Struts2 set parameters via params interceptor which uses OGNL to populate the model. You can control this interceptor via parameters excludeParams for example

<interceptor-ref name="params">
  <param name="excludeParams">
    dojo\..*,^struts\..*,sign
  </param>
</interceptor-ref>

另一种方法是使用 ParameterNameAware,您可以在其中实现参数的黑/白名单.小心这种方法可以通过第一种方法打开限制参数的大门.

Another approach is to use ParameterNameAware where you could implement black/white list of parameters. Be careful with this approach can open door to restricted parameters by the first method.

这篇关于如何在 Struts 2 中从更新中排除 ModelDriven Action 的某些属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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