0

I would like to write my business logic after the getters and setters are called (twice), because I use their object values inside the business logic. However Construct, Post construct, actionevents,.. are called before the getters.

So how can I use the values of the getters if I don't want to write business logic inside them?

Arian
  • 3,183
  • 5
  • 30
  • 58
  • 2
    So, you basically want to run some code after render response phase? – BalusC Mar 22 '13 at 12:12
  • Yes and is it also possible to run code between getters and setters? I suppose getters are called before setters?! – Arian Mar 22 '13 at 12:39
  • 4
    You are focusing too much on getters/setters. You should not do that. That makes no sense. They are just public access points for bean's properties. You should focus on **when** you want to run the business code which manipulates those properties. Thus, again, you want to know how to run some business code after render response phase? In what scope is your backing bean? – BalusC Mar 22 '13 at 12:44
  • My Bean is in RequestScoped, my business logic includes database queries – Arian Mar 25 '13 at 09:02
  • I googled render response phase but didn't found a solution so my guess is that this means after the constructor (?). Basically my case is that I want to retrieve some values of textfields in the bean and then perform business logic. I read that because of security reasons its better to do it with getter and setters and not 'FacesContext' . – Arian Mar 25 '13 at 09:07
  • You have still not told/confirmed **when** in the HTTP/HTML/JSF lifecycle you want to run the business code based on those properties (it's perhaps better if you tell in detail what the concrete functional requirement is for which you thought that this is the right solution). – BalusC Mar 25 '13 at 09:30
  • I want to navigate to the site and get data from a database displayed into outputText. When I change a (primefaces)selectOneMenu value the bean gets the selectOneMenu's value and performs a query in the database for this value, and writes the query result inside the outputText. – Arian Mar 25 '13 at 09:40

3 Answers3

2

You are making a basic mistake in your thinking.

There is no such phase as "The Getters". Getters are just a convention to read a property of a bean.

Those properties can be read individually throughout the entire request. Some may be consulted as early as during "create/restore view", while others may be consulted during "render response".

There is no such thing as that JSF in one particular phase does a sweep through your code and for the fun of it calls every getter it finds.

The solution for you is to let this thinking go. I know it might be hard to let go of something you think is true, but inhale, clear you mind, say goodbye to your current understanding of how things work, and just re-learn from scratch.

You'll then find the answer yourself in no-time. Good luck!

Mike Braun
  • 3,729
  • 17
  • 15
2

I want to navigate to the site and get data from a database displayed into outputText.

Do the job in (post)constructor of the bean.

@ManagedBean
@RequestScoped
public class Bean {

    private String data;

    @EJB
    private SomeService service;

    @PostConstruct
    public void init() {
        data = service.load();
    }

    // Getter.
}

with

<h:outputText value="#{bean.data}" />

When I change a (primefaces)selectOneMenu value the bean gets the selectOneMenu's value and performs a query in the database for this value, and writes the query result inside the outputText.

Do the job in the ajax listener method of the bean which is attached to input component's change event.

@ManagedBean
@ViewScoped
public class Bean {

    private String selectedItem;
    private String result;

    @EJB
    private SomeService service;

    public void changeSelectedItem(AjaxBehaviorEvent event) {
        result = service.find(selectedItem);
    }

    // Getters+setter.
}

with

<p:selectOneMenu value="#{bean.selectedItem}">
    <f:selectItems ... />
    <p:ajax listener="#{bean.changeSelectedItem}" update="result" />
</p:selectOneMenu>
<h:outputText id="result" value="#{bean.result}" />

Doing it after the getters are called would be too late. JSF would at that point already be finished with rendering the HTML output. You can't change the HTML output afterwards.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
-4

Did not understand your question fully, but obvious way is to put logic in getters and setters.

popfalushi
  • 1,332
  • 9
  • 15
  • 3
    Best practice is to not but business logic inside getters: http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times/2090062#2090062 – Arian Mar 22 '13 at 12:01
  • 1
    It is not entirely true - getters and setters are there because there is possibility that there will be some business logic while setting values. Otherwise all vars which have both getters and setters would be public and without g/s. Of course, there is certain boundary after which you can say 'there is too much logic in it'. – popfalushi Mar 22 '13 at 12:35
  • @popfalushi it looks that you have never worked with JSF. If you have done it, then you were doing it in the wrong way. Also, as another proof that you're wrong, BalusC doesn't recommend it and he's the most experienced with JSF in StackOverflow. – Luiggi Mendoza Mar 22 '13 at 15:48
  • 1
    @popfalushi there is a difference between some simple logic to correct some matters and "business logic". Getters are there to, if really needed, do some simple checks (if value is null then ...) or delegate to another getter. More or less by convention, it should work only with the instance variables of the class itself and most of all be "simple". Business logic however means calling an EJB service, which calls a Database and does long running queries etc. THAT stuff should never ever be put into a getter. – Arjan Tijms Mar 23 '13 at 11:05