JSF backing bean structure (best practices)

后端 未结 6 784
谎友^
谎友^ 2020-11-28 00:21

I hope that in this post, I can get people\'s opinions on best practices for the interface between JSF pages and backing beans.

One thing that I never can settle on

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 00:40

    I think the most important thing with your backing beans is to seperate their logics. If you have a front page for a CMS system I would see it as bad practice to put every piece of code into one bean because:

    1. The bean would turn very large eventually
    2. Its easier for other people to find what their looking for if they are troubleshooting the login page, if they then can easily just look up the loginBean.java file.
    3. Sometimes you have small pieces of functionality that is clearly distinct from the rest of your code, by separating this I would imagine you would make it easier on yourself to redevelop/expand this code into something bigger, when you already have a nice bean with good structure.
    4. Having 1 big bean, to do it all, will make it more memory dependant if/when you have to do declarations like this MyBigBean bigBean = new MyBigBean(); instead of using the funksjonality you actually needed by doing LoginBean loginBean = new LoginBean(); (Correct me if Im wrong here???)
    5. In my opinion, separating your beans is like separating your methods. You dont want 1 big method which runs over 100's of lines, but rather split it up with new methods that handles their specific task.
    6. Remember, most likely someone other than you will have to work on your JSF projects aswell.


    As for the coupling, I dont see it as a troublesome issue to allow your JSF pages access too the properties in objects in your backingbean. This is support which is built into JSF, and really just makes it easier to read and build imo. Your allready separating the MVC logic strictly. By doing this your saving yourself tons of lines with getters and setters in your backingbean. For example I have a really huge object given to me by the web services, where I need to use some properties in my presentation. If I were to make a getter/setter for each property my bean would expand with atleast 100 more lines of variables and methods for getting the propeties. By using the built in JSF functionality my time and precious code lines are spared.

    Just my 2 cents regarding this even with the question already marked as answered.

提交回复
热议问题