如何有效地重用 JSF (1.2) 页面及其支持 bean?

发布于 2025-01-02 03:01:52 字数 1117 浏览 3 评论 0原文

我知道这个问题更多的是一个有待讨论的问题而不是有待回答的问题,但我无法重新表述这个问题以使其更加具体。

我目前正在开发基于 JSF 1.2 的页面,并且正在考虑如何有效地重用部分页面。我提出了以下想法(示例场景):

第 1 页由三部分组成,第 2 页也是如此。 我将这三个部分提取到单独的文件中,并将它们包含在第 1 页和第 2 页上(使用 )。

因此,第 1 页包括第 1、2 和 3 部分,第 2 页包括第 1、2 和 3 部分(它们是相同的)。

此时,我正在考虑如何重用支持 bean,并想到了以下想法:

每个页面都有自己的支持 bean,我们将其称为 Bean 1(对于页面 1)和 Bean 2(对于页面 2)。

我为每个部分定义了一个接口。因此,我为第 1 部分设置了一个接口(称为接口 1),为第 2 部分设置了第二个接口(称为接口 2),依此类推。这些接口定义了其部分所需的所有方法。 F. 前。如果第 1 部分需要 JSF 站点中的属性“test”(String)( ),接口 1 定义了一个名为 public String getTest(); 的方法。

所以我的基本想法是,如果 Page 1 包含 Part 1,那么它的支持 bean (Bean 1) 必须实现接口 1。 在我的场景中,Page 1 的支持 bean 将实现接口 1、2 和 3。Page 2 也将实现接口 1、2 和 3。

所以到目前为止,支持 bean 中的代码仍然是多余的,这是我想避免的。 为了解决这个问题,我为接口 1、2 和 3 创建了抽象实现,称为抽象部分 1 等。

这个抽象类将由 Bean 1 和 2 扩展。Bean 1 和 2 将仅重写与抽象实现不同的方法(例如负责加载数据的方法 - 在上面的示例中 public String getTest( ); 可以被覆盖以为第 1 页和第 2 页提供不同的数据)。

所以我的最终解决方案非常复杂。现在我的“问题”:这是重用支持 bean 的好方法吗?有人有更好的解决方案吗?

亲切的问候

笨羊

I'm aware that the question is more a question to-be-discussed than to-be-answered, but I wasn't able to rephrase the question to make it more concrete.

I'm currently developing JSF 1.2 based pages and I was thinking about how to efficiently reuse parts of the pages. I came up with the following idea (example scenario):

Page 1 is composed of three parts, the same is true for Page 2.
I extracted the three parts in separate files and I include them on Page 1 and 2 (using <ui:include />).

So Page 1 includes Part 1, 2 and 3 and Page 2 includes Part 1, 2 and 3 (they are identical).

At this point I was thinking how to reuse the backing beans and I came up with the following idea:

Each page has its own backing bean, let's call it Bean 1 (for Page 1) and Bean 2 (for Page 2).

I define an interface for each Part. So I have one interface (called Interface 1) for Part 1, a second interface (called Interface 2) for Part 2 and so on. These interfaces define all methods required by its part. F.ex. if Part 1 requires the attribute "test" (a String) in the JSF site (<h:outputText value="#{bean.test}" />), Interface 1 defines a method called public String getTest();.

So my basic idea is, if Page 1 includes Part 1, its backing bean (Bean 1) must implement Interface 1.
In my scenario, the backing bean of Page 1 would implement Interface 1, 2 and 3. Page 2 would implement Interface 1, 2 and 3 as well.

So up to this point the code in the backing bean's are still redundant, which I want to avoid.
To solve this, I create abstract implementations for Interface 1, 2 and 3, called Abstract Part 1 and so on.

This abstract classes will be extended by Bean 1 and 2. Bean 1 and 2 will only override methods which differ from the abstract implementation (f.ex. methods which are responsible for loading the data - in above sample public String getTest(); can be overridden to provide different data for Page 1 and Page 2).

So my final solution is quiet complex. Now my "question": Is this a good way to reuse backing beans? Has somebody better solutions?

Kind regards

stupidSheep

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪少女 2025-01-09 03:01:52

如果我正在创建一个页面片段*,我打算在多个页面中重复使用,我通常会为该片段创建一个支持 bean,并通过 params(和 Seam DI)将所需的任何上下文信息传递给该片段,但这可能不可行为您):

<ui:include scr="/common/myfragment.xhtml">
    <ui:param name="property" value="#{pageBean.property}" />
</ui:include>

这样您的页面 bean 不需要实现任何片段的功能。

*很抱歉使用术语“片段”,我指的不是 标签,而是页面的任意部分。

If I am creating a page fragment* that I intend to re-use in multiple pages, I usually create a backing bean just for the fragment and pass any contextual information required to the fragment via params (and Seam DI but that might not be viable for you):

<ui:include scr="/common/myfragment.xhtml">
    <ui:param name="property" value="#{pageBean.property}" />
</ui:include>

That way your page beans don't need to implement any of the fragment's functionality.

*Sorry for using the term fragment, by which I do not mean a <ui:fragment> tag but an arbitrary portion of the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文