CDI bean List in datatable is null to send from JSF
Please note that this question is about CDI scopes as we are using CDI scopes in the application, not JSF scopes.
1) The controller Bean (TestController.java) which is in RequestScoped (enterprise context) is called index_cut.xhtml when we come to this page for the first time.
2) On the Load button, we load the following method to fill in the sapFinancialPeriodList, which works great and displays the data
3) After changing the content on the page and submitting, it is sapFinancialPeriodList
displayed as NULL in the following method -
Any suggestions?
source to share
Your bean is a request scope and you are loading the data model only into the activity, not into the post (post). When the HTTP response after completing the action that loaded the data, the bean is flushed. A subsequent request will receive a new instance of the bean with all of the default options. However, since the same data model is not saved during (post) construction, it remains empty.
In JSF2, you solve this with @ViewScoped
. This way the bean will work as long as you interact with the same view using postbacks (which return null
or void
).
In CDI, you will need to solve this problem using @ConversationScoped
, which in turn requires an additional template @Inject Conversation
, complete with calls begin()
and end()
at the right points. For a specific example, see also What scoping to use in JSF 2.0 for the wizard pattern? ...
An alternative is to pass the parameters responsible for creating the data model for subsequent request via <f:param>
the command line / button as follows
<h:commandButton value="save" ...>
<f:param name="period" value="#{bean.period}" />
</h:commandButton>
and then recreate exactly the same data model in the constructor (post) of the object with the bean request like this
String period = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("period");
List<SapFinancialPeriod> sapFinancialPeriodList = someservice.list(period);
(the above way is better solved with @ManagedProperty
if you are using standard JSF as far as I know CDI does not have an annotation that allows you to set the HTTP request parameter as a bean property)
See also:
Unrelated to a specific issue, the upcoming JSF 2.2 better addresses this functional requirement by using the new "Faces Flow" with new annotation @FlowScoped
and new tags xmlns:j="http://java.sun.com/jsf/flow"
.
source to share