开发者

Best practice for implementing MVC in JSF2 managed beans [duplicate]

This question already has answers here: What components are MVC in JSF MVC framework? (4 answers) Contradictory explanations of MVC in JSF (1 answer) JSF Controller, Service and DAO (2 answers) How to choose the right bean scope? (2 answers) Closed 4 months ago.

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 开发者_StackOverflow社区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.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜