在 JSF2 托管 Bean 中实现 MVC 的最佳实践

发布于 2024-10-04 03:21:08 字数 955 浏览 5 评论 0原文

随着我正在处理的 Web 项目的复杂性不断增加,包含 MVC 结构的需求变得更加迫切。我的模型类定义良好,但视图和控制器代码往往会混在一起。我也在网站上使用了大量的 AJAX(主要是 RichFaces jsFunctions),这使得事情变得稍微复杂一些。

有没有人找到使用 JSF2 实现 MVC 的好策略?我不想在项目中引入另一个框架(例如Spring MVC)。

到目前为止,我还没有开始做一些想法

  • 对于具有大量ajax的页面,有一个“视图”bean来记住选定的选项卡,选定的项目,提供过滤的数据列表等...
  • 有一个“控制器”bean处理诸如模型更改之类的操作
  • 在 JSF 页面和控制器之间使用“命令”bean。 jsFunction 使用参数填充命令 bean,调用command.execute() 会导致命令 bean 调用控制器 bean 上的正确方法来执行操作。 “命令”bean 可能包含一些在完成时调用的 JavaScript。它还可以指定要重新呈现的页面区域。

有什么想法吗?

编辑

我经常看到的是托管bean往往可以做所有事情:跟踪用户的选择,更新模型,获取过滤列表等......

我们'目前正在使用 JSF 1.2,因此我们不能使用带参数的 actions/actionlisteners。例如,我们的托管 bean 包含诸如 m_selectedDate 之类的变量,其唯一目的是在调用 updateFilteredItemsBasedOnDate() 时将所选日期提供给后端。如果额外的变量能够消失那就太好了,因为它们只是暂时的。 JSF 2 的 EL 带参数应该会有所帮助,但我很好奇是否有可用的替代解决方案。

我很好奇是否有一种方法可以将 MVC 应用于托管 bean,或者有某种分离关注点的方法,这样我们就不会得到试图做所有事情的大型 bean。

As the complexity of the web projects that I am working on increases, the need to include a MVC structure is becoming more urgent. My model classes are well defined, but view and controller code tends to get munged together. I've been using pretty heavy AJAX on the site also (RichFaces jsFunctions mostly) which makes things slightly more complicated.

Has anyone found good strategies for implementing MVC using JSF2? I do not want to introduce another framework to the project (e.g. Spring MVC).

Some ideas thus far, which I haven't started doing yet

  • For a page with heavy ajax, have a 'view' bean for remembering selected tabs, selected items, providing filtered lists of data, etc...
  • Have a 'controller' bean to handle actions such as changes to the model
  • Have 'command' beans which go between the JSF page and controller. A jsFunction populates the command bean with parameters, and calling command.execute() causes the command bean to call the correct method on the controller bean to perform the action. The 'command' bean may include some javascript to be called on completion. It may also specify regions of the page to be re-rendered.

Any thoughts?

Edit

What I see pretty often are managed beans that tend to do everything: keep track of users' selections, update the model, get filtered lists, etc...

We're using JSF 1.2 at the moment and so we can't use actions/actionlisteners with parameters. So for example, our managed beans contain variables such as m_selectedDate whose only purpose is to feed the selected date to the back-end on the call to updateFilteredItemsBasedOnDate(). It would be nice if the extra variables could go away since they are just temporary. JSF 2's EL with parameters should help but I'm curious if there is an alternate solution available.

I am curious if there is a way to apply MVC to managed beans, or some method of separating concerns so that we don't end up with large beans that try to do everything.

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

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

发布评论

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

评论(3

无声无音无过去 2024-10-11 03:21:08

您在寻找设计模式吗?如果您正在寻找 MVC,那么 BalusC 答案就足够了,否则请检查此

https://web.archive.org/web/20171228073050/https://www.allapplabs.com/j2ee_design_patterns/j2ee_design_patterns.htm

另请查看 这个

Are you looking for design patterns? If you are looking for MVC then BalusC answer is sufficient, else check this

https://web.archive.org/web/20171228073050/https://www.allapplabs.com/j2ee_design_patterns/j2ee_design_patterns.htm

Also check out this

原谅过去的我 2024-10-11 03:21:08

好吧,也许您需要的是分层架构。迄今为止的MVC架构,还有更多的担忧。
例如,您可以将架构分层到视图层、业务逻辑层和数据访问层,在每一层中实现大多数公共设计和企业模式。

您已经开始其中一个实现 MVC,但是,采用“关注点分离”是一种很好的做法,

通过这种方式,您可以实现组织良好且解耦的代码。

我希望这个简短的解释有用。

问候。

Well, maybe what you need is a layered architecture. So far a MVC architecure, there are more concerns.
For example, you could layer you architecure in View, Business Logic and DataAccess layers, implemeting most of commons design and enterprise patterns in each layer.

You h've started whit one of them implementing MVC, but, is a good practice take in count "separation of concerns"

In this way you could achieve a well organized and decoupled code.

I hoppe this brief explanation was useful.

Regards.

高冷爸爸 2024-10-11 03:21:08

您必须了解管理 bean 的更好方法,而不是研究如何更好地在 JSF 中使用 MVC。因为JSF本身就带有MVC更好的实现。我不确定您所说的实现 MVC 的更好方法到底是什么意思。

正如 Balusc 在评论中所说,JSF 本身有足够的 MVC 来满足您的应用程序的需要。如果您想分离关注点,最好将业务逻辑分离到 Helper 类中并只在 bean 中编写表示逻辑。

关于是否将该 bean 用于业务逻辑或者是否应该将其与托管 bean 分开存在争论。这完全取决于您的应用程序和特定领域。

谢谢,
克里希纳

You have to see the better way of managing the beans rather look into how better use the MVC in JSF. Because JSF itself come with the MVC in better implementation. I am not sure what exactly here you mean as the better way of implementing the MVC.

As Balusc told in the comments, JSF itself has the sufficient MVC for your application. May be if you want to separate the concerns, it is good idea to separate the business logic into Helper classes and write only the presentation logic in the beans.

There is debate on whether to use the bean for business logic or should separate it from the managed bean. That is all depends on the your application and domain specific.

Thanks,
Krishna

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