Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between returning null and "" from a JSF action

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?
like image 839
Brent C Avatar asked Jan 05 '12 14:01

Brent C


People also ask

What does returning NULL do?

Returning null Creates More Work An ideal function, like an assistant cook, will encapsulate work and produce something useful. A function that returns a null reference achieves neither goal. Returning null is like throwing a time bomb into the software. Other code must a guard against null with if and else statements.

Is returning NULL good practice?

Returning Null is Bad Practice The FirstOrDefault method silently returns null if no order is found in the database. There are a couple of problems here: Callers of GetOrder method must implement null reference checking to avoid getting a NullReferenceException when accessing Order class members.


1 Answers

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:

  • Communication in JSF 2.0 - Managed bean scopes
like image 149
BalusC Avatar answered Sep 27 '22 20:09

BalusC