JSF Best Practices: Model, Actions, Getters, Navigation, Phaselists

I have a project for re-factoring a JSF implementation. Existing code does not comply with the relevant JSF standards. To do this, I study all the concepts in JSF (I already have experience with JSF). To be specific, I would like to ask what I mean.

  • In the MVC pattern, what is a model component in JSF? Is it a driven bean?
  • Is it good to write business logic in action methods? I have seen hundreds of lines written in action methods.
  • Do you think that we can write any logic in getter methods ?. How many times a getter or setter was called in the JSF life cycle.
  • What is the usual way to write faces-config.xml. In one document, I read that it is good practice to write a managed bean declaration and a navigation case for this bean together. It will be more readable.
  • Using a phase listener will affect the response time. For example, I am writing logic to analyze a query parameter in a PhaseListener and execute some logic. Are there any tips on this?

Please answer the above questions. If I agree with the answer, I will come up with a few more questions.

+7
source share
2 answers

Note that even if you checked [icefaces] , this answer applies to JSF in general, not IceFaces.

In the MVC pattern, what is a model component in JSF? Is it a driven bean?

It is right. View is the JSP / Facelets page. The controller is a FacesServlet . For more on how this works "under the covers", see also this answer .

Is it good to write business logic in action methods? I have seen hundreds of lines written in action methods.

You can also delegate a call to a business service, such as EJB. This way you can distract business details. In "simple" applications, it is usually not harmful to leave this part and do everything in a managed bean. However, as soon as you get to the point, you would like to change the business logic (for example, for another client or for demonstration purposes, etc.), then the provision of the service would be more convenient, so you do not need to change managed beans, but you just need to write another implementation class for a particular business interface.

Do you think that we can write any logic in getter methods ?. How many times a getter or setter was called in the JSF life cycle.

If business logic needs to be executed every time getter is called, then do it (this, however, is very unlikely in the real world, expect crazy logging or special lazy (repeated) load cases). But if business logic needs to be executed only once per action, event, request, session, or application area, it should definitely be executed elsewhere. See also this answer .

How many times a getter is called, you should be the least concerned. A getter must do nothing but return the property in question. When called in the output value, it may be once per request. When the input value is called, this can be two times per request. When inside the datatable / repeat component, multiply the calls by the number of rows. When inside the displayed attribute, multiply calls with 6 ~ 8 times.

What is the usual way to write faces-config.xml. I read in one document that he says that it is good practice to write a managed bean declaration and a navigation case for this bean together. It will be more readable.

I myself use very rarely navigation cases, usually not in my faces-config.xml . I always return back to the same view (return null or void ), and then render / enable the result conditionally. For page navigation, I do not use POST requests (for which navigation cases are mandatory) simply because the user interface does not work properly and the URL in the address bar of the browser is always at one step, because it is by default forward, not redirected) and SEO (search engine optimization, search engines don’t “post index POST). I just use the output links or even simple HTML <a> elements to navigate the pages.

Also, in JSF 2.0, technically there is no need for w812 managed definitions and navigation in faces-config.xml . See also this answer .

Using a phase listener will affect the response time. For example, I am writing logic to analyze a query parameter in a PhaseListener and execute some logic. Are there any tips on this?

This applies to servlet filters in the premature optimization category. Concern about their execution usually does not make sense. This balance is usually only one or two lines of code. There is really nothing to worry about. You would have much more trouble copying this piece of code into all classes. If you really think it is worth the performance, first a profile, and then we can talk about it.

+15
source

In the MVC pattern, what is a model component in JSF? Is it a driven bean?

I suggest the following:

LAYER VIEW (composed of mini MVC):

userForm.xhtml <- view; webpage

UserController <-controller; driven bean

UserController.get / setUser <- model; model for presentation

CONTROLLER LAYER:

UserService <-controller; a controller containing business logic related to CRUD

UserDAO <- saves objects in the database

User <is the domain object that is being saved.

MODEL LAYER:

Database

Is it good to write business logic in action methods? I have seen hundreds of lines written in action methods.

In my example, put the business logic in the UserService methods. The UserController action method is not much more than calling the method in the UserService, catching any exceptions and formatting the response for the next displayed web page.

Do you think that we can write any logic in getter methods ?. How many times a getter or setter was called in the JSF life cycle.

It is preferable for getter / seters to just do setup / setup. There is no logic.

What is the usual way to write faces-config.xml. I read in one document that he says that it is good practice to write a managed bean declaration and a navigation case for this bean together. It will be more readable.

I declare all my managed beans together. And announce all my navigation rules together.

Using a phase listener will affect the response time. For example, I am writing logic to analyze a query parameter in a PhaseListener and execute some logic. Are there any tips on this?

Not sure what you are doing exactly, but you don’t need to manually analyze the query parameters. JSF should enter the values ​​of your form directly into the view model for you. Perhaps I need more information on what you are trying to do.

Hope this helps.

+4
source

All Articles