43

From what I understand, when a JSF action returns "" (empty String) the user stays on the current page but the view is refreshed. However, when the action returns null the user still stays on the current page but the old view is reused. My question is:

  1. Is the above statement correct (accurate)?
  2. If yes, then what are the implications of this? Specifically, what effect does using one versus the other have on data on the page (values in JSF UI components, or data stored in a request-scope bean in a DataTable, for example)?
  3. In what situations should one be used over the other?
Perception
  • 79,279
  • 19
  • 185
  • 195
Brent C
  • 833
  • 1
  • 9
  • 15

1 Answers1

35

Is the above statement correct (accurate)?

Yes. Instead of returning null you can also just return void.


If yes, then what are the implications of this? Specifically, what effect does using one versus the other have on data on the page (values in JSF UI components, or data stored in a request-scope bean in a DataTable, for example)?

Nothing on request scoped beans. It has only effect on JSF2 view scoped beans. On returning null or void, the view scoped bean instance will be retained in the next request, else it will be recreated.


In what situations should one be used over the other?

If you want to retain the JSF2 view scoped bean in the subsequent request.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So if I am using JSF 1.2 without a view-scope, then it is safe to say there is really no difference? – Brent C Jan 05 '12 at 14:42
  • 1
    Functionally, no. But technically there's a difference which you already found out yourself. Creating a new view has a minor cost. Just always return `null` or `void` on postbacks to the same view. – BalusC Jan 05 '12 at 14:49
  • FYI: _Application Actions_ have the following constraints in the JSF2 spec: _1. The method must be public. 2. The method must take no parameters. 3. The method must return Object._ I recall at least one navigation API implementation blowing up if you tried to use a `void` return type, but can't remember which vendor/version. – McDowell Jan 05 '12 at 16:13
  • @McDowell: in reality, anything which can be represented by a [`MethodExpression`](http://docs.oracle.com/javaee/6/api/javax/el/MethodExpression.html) is a legal action method, even the ones taking parameters as possible in EL 2.2. That spec cite is perhaps a leftover from JSF 1.0. – BalusC Jan 05 '12 at 16:27
  • Is this difference in treating of response value covered in spec or it is just implementation thing? – Tibor Blenessy Sep 30 '14 at 11:38