简体   繁体   English

方法调用的ManagedBean和xhtml设计策略

[英]ManagedBean and xhtml design strategy for method calls

Suppose that I have a class like this: 假设我有一个这样的课:

@ManagedBean
public class A {

@ManagedProperty("#{B}")
private B b;

private Object o;

public void operation() {
    b.methodCall(o);
}

public void setB(B b) {
    this.b = b;
}

public Object getO() {
    return o;
}

public void setO(Object o) {
    this.o = o;
}

}

Now when I make the method call from xhtml I can simply write: 现在,当我从xhtml进行方法调用时,我可以简单地编写:

<p:commandButton actionListener="#{a.operation()}" />

However I can also add a getter to my managed bean: 但是,我也可以向我的托管bean添加吸气剂:

public B getB() {
    return b;
}

and call the same method like this removing the need to write the operation() method in the first version of my managed bean. 并调用类似的方法,从而无需在托管bean的第一个版本中编写operation()方法。

<p:commandButton actionListener="#{a.b.methodCall(b.o)}" />

It seems to me that the first approach would be easier while refactoring because of the ide support in refactoring java classes, but at the same time it makes me write a trivial method which only serves as a mean to convey parameters. 在我看来,第一种方法在重构时会更容易,因为在重构Java类时提供了ide支持,但与此同时,它使我编写了一个琐碎的方法,该方法仅用作传达参数的手段。

Which one is the better approach? 哪种方法更好?

I think there is no straight forward answer for this question. 我认为这个问题没有直接答案。 The approach should be based on our requirement. 该方法应基于我们的要求。 Both has its own advantages and disadvantages. 两者都有其优点和缺点。

But one thing i can say is :- your first approach is more feasible. 但我可以说的是:-您的第一种方法更可行。 Because in your second approach you should have a reference of b inside a, which is not required until unless it is really required [ A need not know about B , if be is an independent entity]. 因为在第二种方法中,您应该在a内具有b的引用,除非真正需要,否则才需要b的引用[ A不必了解B ,如果be是一个独立实体]。 And you can get the instance of B from spring container (there by you can avoid the duplicate instance of B in application ) provided B is not in view scope. 如果B不在视图范围内,则可以从spring容器中获取B的实例(这样可以避免应用程序中B的重复实例)。

And second problem is when you use abmethod, we should make sure that b cannot be null at any point of time. 第二个问题是当您使用abmethod时,我们应确保b在任何时间点都不能为null。 When we have a different entity reference in one html, the application will be come tightly coupled. 当我们在一个html中具有不同的实体引用时,该应用程序将紧密耦合。 And A html cannot be used with any other entity, if you have any plan to use it as a generic html. 如果您有任何计划将html用作通用html,则html不能与任何其他实体一起使用。

hope i'am clear with my explanation. 希望我对我的解释很清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM