java - 使用 Stripes,显示/更新/等操作 Bean 的最佳模式是什么?

标签 java stripes

我已经为这个问题苦苦挣扎了一段时间。我想使用相同的条纹 ActionBean对于 showupdate行动。但是,我无法弄清楚如何以一种干净的方式实现这一点,从而允许当前用户对对象所有权进行可靠的绑定(bind)、验证和验证。

例如,假设我们的操作 bean 采用 postingId 。该帖子属于已登录的用户。我们可能有这样的内容:

@UrlBinding("/posting/{postingId}")
@RolesAllowed({ "USER" })
public class PostingActionBean extends BaseActionBean

现在,对于show Action ,我们可以定义:

    private int postingId; // assume the parameter in @UrlBinding above was renamed
    private Posting posting;

现在使用@After(stages = LifecycleStage.BindingAndValidation)获取 Posting 。我们的@After函数可以验证当前登录的用户是否拥有该帖子。我们必须使用@After ,不是@Before ,因为postingId不会事先绑定(bind)到参数。

但是,对于 update函数,你想绑定(bind)Posting使用 @Before 对象到 Posting 变量,不是@After ,以便将返回的表单条目应用到现有 Posting 对象之上,而不是应用到空 stub 上。

定制TypeConverter<T>在这里可以很好地工作,但是因为该 session 无法从 TypeConverter 获得接口(interface),在绑定(bind)过程中很难验证对象的所有权。

我能看到的唯一解决方案是使用两个单独的操作 bean,一个用于显示,一个用于更新。但是,如果您这样做,<stripes:form>标签及其下游标签将无法正确填充表单的值,因为 beanclassaction标签必须映射回相同的 ActionBean .

据我所知,Stripes 模型仅在操作简单(非 POJO)参数时才保持在一起。在任何其他情况下,您似乎都会遇到从数据存储绑定(bind)对象并用客户端发送的更新覆盖它的第 22 条军规。

我一定错过了一些东西。经验丰富的 Stripes 用户的最佳实践是什么?

最佳答案

在我看来,授权与对象水合作用是正交的。我的意思是,您应该将对象水合的关注点(在本例中,使用 postingId 并将其转换为 Posting )与确定用户是否有权对该对象执行操作(例如 show 、更新、删除等)。

对于物体水合作用,我使用 TypeConverter<T> ,并且我对对象进行水合,而不考虑 session 用户。然后在我的ActionBean里面我在二传手周围有一个守卫,因此...

public void setPosting(Posting posting) {
    if (accessible(posting)) this.posting = posting;
}

哪里accessible(posting)看起来像这样...

private boolean accessible(Posting posting) {
    return authorisationChecker.isAuthorised(whoAmI(), posting);
}

然后你的show()事件方法看起来像这样...

public Resolution show() {
    if (posting == null) return NOT_FOUND; 
    return new ForwardResolution("/WEB-INF/jsp/posting.jsp");
}

另外,当我使用 Stripes 时,我经常在同一个 Stripes 中发生多个事件(例如“显示”或“更新”)ActionBean 。对我来说,围绕相关名词对操作(动词)进行分组是有意义的。

使用干净的 URL,您的 ActionBean注释看起来像这样...

@UrlBinding("/posting/{$event}/{posting}")
@RolesAllowed({ "USER" })
public class PostingActionBean extends BaseActionBean

...哪里{$event}是事件方法的名称(即“显示”或“更新”)。请注意,我正在使用 {posting} ,而不是{postingId}

为了完整起见,这是您的 update()事件方法可能看起来像...

public Resolution update() {
    if (posting == null) throw new UnauthorisedAccessException();
    postingService.saveOrUpdate(posting);
    message("posting.save.confirmation");
    return new RedirectResolution(PostingsAction.class);
}

关于java - 使用 Stripes,显示/更新/等操作 Bean 的最佳模式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977413/

相关文章:

java - 从 RGB 绘制 CMYK 颜色

Java Scanner vs Matcher——正则表达式,Matcher 有效,Scanner 无效

java - Apache POI 货币数据格式

java - Arquillian TomEE 测试性能

eclipse - Stripes 看不到我的 ActionBean

java - App Engine 本地主机 x 在线 session

stripes - Shiro 总是将我重定向到 login.jsp

java - 条纹中默认的另一个枚举值

java - AtomicBoolean 与同步块(synchronized block)

java - Stripes 中的隐藏字段值(对象而不是数值)