From page to page navigation why #postconstruct init() method is called twice? - jsf-2

I have a following situation. I am trying to pass a parameter from one bean (e.g. fooBean) to another bean (e.g. barBean) when a page to a page navigation occurrs.
Both beans are ViewScoped.
The fooBean is created when a foo.xhtml page is loaded. From there I pass a parameter "id" to bar.xhtml page.
This is foo.xhtml page:
<h:form>
<p:dataTable var="item" value="#{fooBean.items}" ....>
<p:column ..>
<h:commandLink action="#{fooBean.next}">
<h:outputText value="#{item.name}"/>
<f:setPropertyActionListener value="#{item.id}" target="#{barBean.newId}" />
</h:commandLink>
</p:column>
</p:dataTable>
</h:form>
This is fooBean.next() method:
public String next() { return "bar.xhtml?faces-redirect=true" }
When I click h:commandLink (record or row) from foo.xhtml page, it is redirected to "bar.xhtml".
This is barBean:
#ManagedBean
#ViewScoped
public class BarBean implements ... {
private String newId;
#PostConstruct
public void init() { ... }
// getter and setter for newId
}
I noticed that #PostConstruct init() method of barBean is triggered twice. With first call/trigger, I was expecting that this.newId variable of the barBean would be set to passed value (e.g. id=12345) which it did by checking log statements (setter is called and this.newId = 12345).
Then init() method is triggered again but I thought this.newId would retain the passed value but it becomes null.
Can someone explain to me what is happening?

Related

#viewScoped data bean survives after navigation to another pages (views) with Spring Web Flow and JSF-2

I'm using Spring Web Flow and JSF-2.
The fact is strange and incomprehensible to me.
I have a main page, a typical index.
And I have and actionView with some filters (filtering will be made with an ajax calling) and a table for displaying the results according to filter info.
The back bean for this action is definied as #ViewScoped.
If, at first, I navigate to index after filtering some data and then, later, I return to the action view I don't expect to found the last search displayed, I expect an empty view, but the filters are not empty and data result is filtered.
Why? If I'm defining a #ViewScope is due to I expect the back bean information has to be deleted when I change the view (and index is another view in my case) but I must be making some mistake.
Here is my code:
My parent-flow (simpifying):
<view-state id="index" view="../index.xhtml" redirect="true" popup="true"/>
<view-state id="action1Flow" view="flowRedirect:action1-flow" />
<global-transitions>
<transition on="home" to="index" />
<transition on="action1" to="action1Flow" />
</global-transitions>
action1-flow: (start-state="action1View")
<view-state id="action1View" view="../views/action1View.xhtml" redirect="true" popup="true"/>
action1View.xhtml: (Simplifying, one filter -> table results)
...
<p:panel id="filter" header="Filter example">
<h:panelGrid columns="2">
<h:outputLabel for="dataFilter" value="Filter"/>
<p:inputText id="dataFilter" value="#{action1View.dataValue}"/>
<p:ajax event="keyup" listener="#{action1View.filterData()}" update="table"/>
</p:inputText>
</h:panelGrid>
</p:panel>
<p:panel id="display" header="Data filtered">
<p:dataTable id="table" var="data" value="#{action1View.resultData"
selection="#{action1View.selectedData}" rowKey="#{data.dataValue}">
<p:column headerText="#{msg.id}" sortBy="#{data.dataValue}">
<h:outputText value="#{data.dataValue}" />
</p:column>
</p:dataTable>
</p:panel>
...
<p:commandButton value="Go to index" action="home"/>
...
and action1View.java, the back bean:
#ManagedBean
#ViewScoped
#Component("action1View")
public class Action1View implements Serializable {
static final long serialVersionUID = 42L;
List<ExampleBean> resultData = null;
ExampleBean selectedData = null;
Integer dataValue;
public ExampleBean getSelectedData() {
return selectedData;
}
public void setSelectedData(ExampleBean selectedData) {
this.selectedData = selectedData;
}
public Integer getDataValue() {
return dataValue;
}
public void setDataValue(Integer dataValue) {
this.dataValue = dataValue;
}
public void filterData() {
// Some logic
resultData = xxxxx;
}
}
index.xhtml:
...
<p:commandButton value="Go to index" action="action1"/>
...
Sorry for my english and ...
Greetings!
I have found the GAP!
My bean, apart from #ViewScoped annotation, is also defining (as you can see in the code) with this annotation: #Component("action1View") without apparent reason.
The fact is by removing this annotation I got everything to work properly.
I think that is because the Component behaviur overlays ViewScoped one doing that only one bean for each defined class can be created, and, becauso of this, information stays in time (until the session ends or the application was closed).
But it would be great if someone give more and richer information about it.

JSF component no longer rendered, but FacesMessage remains

Here is the situation:
I have a component, whose value is a property of my ViewScoped bean, which gets a FacesMessage generated for it do to some error when the form is first submitted
On a subsequent user action, a h:commandlink is clicked, which uses f:ajax, and its actionlistener sets the component to no longer be rendered
Upon receiving the reponse, the component is no longer rendered (as expected), but the FacesMessage for it still remains and is displayed
I am using Mojarra JSF 2.1, and IceFaces 3.0 - although this example and the problematic behavior are seen using only standard JSF components. Here is a small example that demonstrates the behavior:
test.xhtml
<h:form id="testform">
<h:message for="tv"/>
<h:inputText id="tv" value="#{testBean.testVal}" rendered="#{testBean.testValRendered}" required="true" />
<h:commandButton value="Submit"/>
<h:commandLink actionListener="#{testBean.toggleTestVal}" value="Toggle input">
<f:ajax execute="#this" render="#form"/>
</h:commandLink>
</h:form>
Bean code
#ViewScoped
#ManagedBean(name = "testBean")
public class TestBean {
private String testVal;
private boolean testValRendered;
public TestBean() {
testValRendered = true;
}
public String getTestVal() {
return testVal;
}
public void setTestVal(String testVal) {
this.testVal = testVal;
}
public boolean isTestValRendered() {
return testValRendered;
}
public void setTestValRendered(boolean testValRendered) {
this.testValRendered = testValRendered;
}
public void toggleTestVal(ActionEvent ae) {
testValRendered = !testValRendered;
}
}
If I change the h:commandLink so that it does not use f:ajax, and set immediate='true' on it instead, then the message is removed properly. While this may work in some cases, I would like to understand why using f:ajax to render things as I have in my example does not work. Thanks.
The input component is submitted, processed and validated before its rendering condition has been toggled. The <h:message> isn't aware that the associated input component is not rendered. You'd like to check the very same rendering condition on the <h:message> as well:
<h:message for="tv" rendered="#{testBean.testValRendered}" />
<h:inputText id="tv" ... rendered="#{testBean.testValRendered}" />

Passing parameters to a bean method from a datatable within a view/page in JSF 2.0

I have a page with a datatable that populates from a list. The list has many ids, which I need to pass to a bean method and perform a database operation.I am using a commandButton's rendered attribute to make a call to the bean method (findInfo()). I need to pass the ids (myBean.testList.id) one at a time from the list to this bean method so that I get the boolean back. First of, I am not sure how to pass the id (to the findInfo method in my bean), secondly if I use tag, I end up sending just a single ID. Could anyone please suggest me a solution. I tried doing it with the codes as below.
My Bean
#ManagedBean(name="myBean")
public class MyBean(){
public boolean findInfo(Integer indId){
boolean var = dataProvider.isIdPresent(indId); // database operation
if (var)
return true;
else
return false;
}
}
xhtml page
<h:dataTable value="#{myBean.testList}" var="t" >
<h:column>
<f:facet name="header">
<h:outputText value="Actions" />
</f:facet>
<h:commandButton value = "Add"
action = "#{someBean.loadReport}"
rendered = "#{myBean.findInfo() == false}"/>
<h:commandButton value = "View"
action = "#{someBean.loadReport}"
rendered = "#{myBean.findInfo() == true}"/>
</h:column>
<h:column>
...
</h:column>
</h:dataTable>
UPDATE:
I tried to follow the answer that BalusC's explained (that kolossus posted), I end up passing the id as null in f:param. Any suggestions. My updated code:
<h:dataTable value="#{myBean.testList}" var="t" >
<h:column>
<f:facet name="header">
<h:outputText value="Actions" />
</f:facet>
<h:commandButton value = "Add"
action = "#{someBean.loadReport}"
rendered = "#{myBean.findInfo() == false}">
<f:param name="id" value="#{t.id}" />
</h:commandButton>
<h:commandButton value = "View"
action = "#{someBean.loadReport}"
rendered = "#{myBean.findInfo() == true}">
<f:param name="id" value="#{t.id}" />
</h:commandButton>
</h:column>
<h:column>
...
</h:column>
My Updated Bean
#ManagedBean(name="myBean")
#RequestScoped
public class MyBean(){
#ManagedProperty("#{param.id}")
private Integer indId;
public boolean findInfo(){
boolean var = dataProvider.isIndIdPresent(indId);
if (var)
return true;
else
return false;
}
}
Also, I think I cannot use f:ViewParam as I am calling the bean from the data table within the same view.
First of all, you should declare your bean as #ViewScoped. Then, you can keep your findInfo method as following:
#ManagedBean(name="myBean")
#ViewScoped
public class MyBean(){
private List testList;
public boolean findInfo(Integer indId){ ... }
// Getters and Setters
}
The following snippet should do the job:
<h:dataTable value="#{myBean.testList}" var="t" >
<h:column>
<h:commandButton value="Add" action="#{someBean.loadReport}"
rendered="#{not myBean.findInfo(t.id)}" />
<h:commandButton value="View" action="#{someBean.loadReport}"
rendered="#{myBean.findInfo(t.id)}" />
</h:column>
</h:dataTable>
UPDATE: Since you're not using EL 2.2, you may need process the boolean value for each row before rendering the table. Wrapping the objects in the testList inside a dummy object containing a boolean value should do the job :). Something like this:
#ManagedBean(name="myBean")
#ViewScoped
public class MyBean(){
private List<DummyObject> testList;
#PostConstruct
public boolean prepareTestList(){
// Initiate test list
List<AnObject> oriList = ....;
// Process oriList to create a testList
this.testList = new LinkedList();
for (AnObject o : oriList) {
DummyObject do = new DummyObject();
do.setObject(o);
do.setIdPresent(findInfo(o.getId()));
this.testList.add(do);
}
}
public boolean findInfo(Integer indId) { ... }
public class DummyObject {
private boolean idPresent;
private AnObject object;
// Getters and Setters
}
// Getters and Setters
}
Then your rendered attribute should look like this:
<h:commandButton value="View" action="#{someBean.loadReport}"
rendered="#{t.idPresent}" />

Panel not rendering on command button click in primefaces

I was playing around with primefaces components, when I stumbled upon this issue...
I have a sample page which contains the following,
<h:form>
<p:panel id="p1">
<p:commandButton value="display" actionListener="#{myTestBean.display}" update="p2"> </p:commandButton>
</p:panel>
<p:panel id="p2" rendered="#{myTestBean.show}">
<p:inputText value="#{myTestBean.val1}" id="t1"></p:inputText>
<p:inputText value="#{myTestBean.val2}" id="t2"></p:inputText>
<p:commandButton value="click me" update="#form" action="#{myTestBean.disply}"></p:commandButton>
</p:panel>
</h:form>
My backing bean contains the following,
public class Testbean implements Serializable {
private String val1;
private String val2;
private boolean show;
//getters and setters for the above...
public void display(){
if(show == false){
setShow(true);
}
}
public void disply(){
System.out.println("I am here");
val1 = "hi";
val2 = "hello";
if(show == false){
setShow(true);
}
}
}
Now, the catch here is, when I click my first button(display) the below panel(p2) gets rendered correctly. But when I click on the second button(click me) the panel gets hidden and also on click of second button it never goes to the backing bean method 'disply()'.
I have used action listener in the second button but again the same behavior. Also, I have given the update = "p2" in the second button, even that didn't yield what I wanted.
Could anyone help me out with this and let me know where and what I am doing wrong?
Thank you.
The bean needs to be placed in the view scope in order to remember all previous (ajax) actions on the same view.
#ManagedBean
#ViewScoped
public class TestBean {
// ...
}
When the bean is placed in the request scope, then the boolean show would be reinitialized to default false and thus the rendered attribute would evaluate false while JSF is looking for the action method to be invoked and hence the action method would be never found and invoked.
See also:
How to choose the right bean scope?
commandButton/commandLink/ajax action/listener method not invoked or input value not updated
When using actionListener="#{myTestBean.display}", the backinbean method should be
public void display(**ActionEvent event**){
if(show == false){
setShow(true);
}
}
and an ActionEvent parameter must be declared.
I surrounded the button as shown below
<h:form>
<h:commandButton styleClass="toggle_btn" action="#{buttonNavBean.show()}" style=" font-family: sans-serif; color: black;" value="Sign In/Sign Up"></h:commandButton>
</h:form>
I faced the same challenge too. I tried to search online but all in vain. But after several trials, I came to figure out that since I was using the commandButton to set the value of the rendered panel, it was not possible to work out of UIForm, Therefore, I surrounded the commandButton with UIForm and it worked correctly as I wanted.

dynamic adding radio and InputText in jsf

i am getting problem in editing in textBox which are generated dynamically.
see my code.
<h:form>
<h:panelGroup>
<h:panelGrid columns="2">
<h:panelGrid columns="1">
<h:selectOneRadio id="radio1" value="#{dynamicBean.radiovalue}" layout="pageDirection" >
<f:selectItems value="#{dynamicBean.objectList}" var="k1" itemValue="#{k1.value}" itemLabel="" />
</h:selectOneRadio>
</h:panelGrid>
<h:panelGrid columns="1" rowClasses="raw1">
<c:forEach items="#{dynamicBean.objectList}" var="k3">
<p:inputText value="#{k3.textvalue}" valueChangeListener="#{dynamicBean.ajaxEvent}" >
</p:inputText>
</c:forEach>
</h:panelGrid>
<h:commandButton value="add new" action="#{dynamicBean.addNew}"/>
</h:panelGrid>
</h:panelGroup>
</h:form>
and this is my bean.
#ManagedBean
#ViewScoped
public class DynamicBean implements Serializable{
private String radiovalue;
private List<Pojo> objectList=new ArrayList<Pojo>();
int i=0;
private Pojo single=new Pojo();
public DynamicBean() {
System.out.println("In Cons");
if(objectList.isEmpty())
{
Pojo p1=new Pojo();
p1.setName("Name-"+i);
p1.setValue("Value-"+i);
p1.setTextvalue("Text-"+i);
objectList.add(p1);
i++;
setRadiovalue(p1.getValue());
}
}
public void addNew()
{
Pojo p1=new Pojo();
p1.setName("Name-"+i);
p1.setValue("Value-"+i);
p1.setTextvalue("Text-"+i);
objectList.add(p1);
i++;
setRadiovalue(p1.getValue());
}
public void ajaxEvent(ValueChangeEvent e)
{
System.out.println("New:"+e.getNewValue());
System.out.print("Old:"+e.getOldValue());
}
following are three variable in Pojo with getter and setter
private String name;
private String value;
private String textvalue;
biggest confusion is i can change first object value on in text box but i cant change the value of new generated objects.
Thanks.
Your concrete problem is caused because you're using JSTL <c:forEach> tag which runs during view build time while you're using a view scoped bean. View scoped beans are stored in the view state. When you submit the form to the server, the view state will be restored, but the original view scoped bean isn't available yet at that moment and thus a new one will be created (thus, with all properties set to default!). After restoring the view, the original view scoped bean will be put back in scope, overriding the temporary one.
You need a fullworthy JSF UI component instead of a JSTL tag. For this particular purpose, you need the <h:dataTable>.
Replace
<h:panelGrid columns="1" rowClasses="raw1">
<c:forEach items="#{dynamicBean.objectList}" var="k3">
<p:inputText value="#{k3.textvalue}" valueChangeListener="#{dynamicBean.ajaxEvent}" >
</p:inputText>
</c:forEach>
</h:panelGrid>
by
<h:dataTable value="#{dynamicBean.objectList}" var="k3" rowClasses="raw1">
<h:column>
<p:inputText value="#{k3.textvalue}" valueChangeListener="#{dynamicBean.ajaxEvent}" />
</h:column>
</h:dataTable>
See also:
JSTL in JSF2 Facelets... makes sense?
Unrelated to the concrete problem, you've some code duplication there in the constructor. Just call addNew() method in there. I'd also remove the instance variable i as that makes no sense. Just use a local one instead which get initialized with List#size().
public DynamicBean() {
addNew();
}
public void addNew() {
Pojo p1 = new Pojo();
int i = objectList.size();
p1.setName("Name-" + i);
p1.setValue("Value-" + i);
p1.setTextvalue("Text-" + i);
objectList.add(p1);
setRadiovalue(p1.getValue());
}

Resources