ManagedBean is constructed but shouldn't? - jsf

I'working on a enterprise application that uses JSF 2.0, with Netbeans 7.0 and Glassfish 3.1
I have a managed bean that is ViewScoped. this is the declaration of the class:
#ManagedBean(name = "myBean")
#ViewScoped
public class MyMBean implements Serializable {
Inside its #PostConstruct, it has the following:
String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
if (id == null) {
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("home.xhtml");
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) { }
return;
}
if I go to the page that uses this managed bean, and the id is null, everything works fine, and I get redirected to home page.
The problem is that when I navigate to a different page that does NOT use this managed bean (lets say for example "otherpage.xhtml") the PostConstruct method is executed, and it shouldn't! And it gets worse: since the url of this other page doesn't have the "id" parameter, the bean tries to redirect to home page; and I get a IllegalStateException.
Any idea of why a viewscoped managed bean is constructed when navigating to a page that does not use it?
Edit:
If in order to navigate to "otherpage.xhtml" I use the commandlink in "home.xhtml", 6 extra beans are created.
But, if instead of using the link, I type the url in the browser; it works fine. No extra bean is created. Maybe there's something wrong in how I implemented the link. This is the code:
<h:form>
<h:commandLink value="Go to other page" action="otherPage" />
</h:form>
And this is the navigation rule in faces-config:
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>otherPage</from-outcome>
<to-view-id>/views/otherPage.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
Is there anything wrong there?
Thanks!
Damian

You surely have a #{myBean} somewhere in the view or one of its templates/include/tag/composite files, or as a #ManagedProperty of the beans referenced by the view. Putting a breakpoint in the (post)constructor and investigating the stacktrace should give enough insights who/what has triggered the bean's construction.
Unrelated to the concrete problem, the ExternalContext#redirect() already implicitly calls FacesContext#responseComplete(), you don't need to call it yourself. See also the method's javadoc.
Update: a <h:commandLink> submits its parent POST <form> to the current page (and thus creates all its related beans!) and then depending on the navigation outcome, it will forward/redirect to the result page. You shouldn't be using commandlinks/commandbuttons for plain page-to-page navigation. Use <h:link> instead.
<h:link value="Go to other page" outcome="views/otherPage" />
You can eventually also get rid of that <navigation-case>. If you really insist in keeping that navigation case, then use outcome="otherPage" instead.
See also:
When should I use h:outputLink instead of h:commandLink?
Communication in JSF 2.0 - Implicit navigation

Related

Dynamic page loading #ViewScoped beans are not destroyed [duplicate]

i am new in JSF.I wonder one point at JSF/navigation rules.i have four pages, index,p1,p2,p3.When i am trying to navigate to a page with action="#{bean.gotoP1()}", it is giving error like that ;
"Unable to find matching navigation case with from-view-id '/index.xhtml' for action '#{bean.gotoP1()}' with outcome 'success'"
My question is simple; why can not I navigate with #{bean.gotoP1()} , and i have to remove parenthesis , #{bean.gotoP1} ?
My codes are below;
index.xhtml
<h:body>
<h:form>
<h:commandButton action="#{mybean.gotoP1()}" value="P1"/>
<h:commandButton action="#{mybean.gotoP2()}" value="P2"/>
<h:commandButton action="#{mybean.gotoP3()}" value="P3"/>
</h:form>
</h:body>
mybean.java
#ManagedBean
#RequestScoped
public class Mybean implements Serializable{
private static final long serialVersionUID=1L;
public Mybean() {
}
public String gotoP1(){
return "success";
}
public String gotoP2(){
return "success";
}
public String gotoP3(){
return "positive";
}
}
faces-config.xml
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-action>#{mybean.gotoP1}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/p1.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{mybean.gotoP2}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/p2.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{mybean.gotoP3}</from-action>
<from-outcome>positive</from-outcome>
<to-view-id>/p3.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
Thanks....
My question is simple; why can not I navigate with #{bean.gotoP1()} , and i have to remove parenthesis , #{bean.gotoP1} ?
Because the EL syntax doesn't match with the navigation case. You defined #{bean.gotoP1} instead of #{bean.gotoP1()} as from-action in navigation case. Simple as that.
Those argumentless parentheses are actually unnecessary. They started to spread over JSF pages since introduction of EL 2.2, because the average EL 2.2 aware IDE thinks to be smarter than it is and unnecessarily auto-completes the method expressions with parentheses and all, confusingly making the JSF starter to think that they are actually required. I've even seen code snippets coming along from starters who actually used #{bean.getProperty()} instead of #{bean.property} to output a property, which would then later fail with a javax.el.PropertyNotWritableException when used in an input component.
Just leave out those argumentless parentheses. It's not true that this syntax is required and "normal" in JSF. Moreover, navigation rules are very JSF 1.x-ish. Also, performing navigation using POST requests is very JSF 1.x-ish. Maybe you're just learning and playing around. That's OK, but to learn about the right ways and a bit of history, carefully read below links:
Invoke direct methods or methods with arguments / variables / parameters in EL
Differences between action and actionListener
JSF implicit vs. explicit navigation
How to navigate in JSF? How to make URL reflect current page (and not previous one)
Difference between JSP EL, JSF EL and Unified EL
Last but not least, our JSF wiki page is a great place to start.

What is difference between #{bean.function} and #{bean.function()}?

i am new in JSF.I wonder one point at JSF/navigation rules.i have four pages, index,p1,p2,p3.When i am trying to navigate to a page with action="#{bean.gotoP1()}", it is giving error like that ;
"Unable to find matching navigation case with from-view-id '/index.xhtml' for action '#{bean.gotoP1()}' with outcome 'success'"
My question is simple; why can not I navigate with #{bean.gotoP1()} , and i have to remove parenthesis , #{bean.gotoP1} ?
My codes are below;
index.xhtml
<h:body>
<h:form>
<h:commandButton action="#{mybean.gotoP1()}" value="P1"/>
<h:commandButton action="#{mybean.gotoP2()}" value="P2"/>
<h:commandButton action="#{mybean.gotoP3()}" value="P3"/>
</h:form>
</h:body>
mybean.java
#ManagedBean
#RequestScoped
public class Mybean implements Serializable{
private static final long serialVersionUID=1L;
public Mybean() {
}
public String gotoP1(){
return "success";
}
public String gotoP2(){
return "success";
}
public String gotoP3(){
return "positive";
}
}
faces-config.xml
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-action>#{mybean.gotoP1}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/p1.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{mybean.gotoP2}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/p2.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{mybean.gotoP3}</from-action>
<from-outcome>positive</from-outcome>
<to-view-id>/p3.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
Thanks....
My question is simple; why can not I navigate with #{bean.gotoP1()} , and i have to remove parenthesis , #{bean.gotoP1} ?
Because the EL syntax doesn't match with the navigation case. You defined #{bean.gotoP1} instead of #{bean.gotoP1()} as from-action in navigation case. Simple as that.
Those argumentless parentheses are actually unnecessary. They started to spread over JSF pages since introduction of EL 2.2, because the average EL 2.2 aware IDE thinks to be smarter than it is and unnecessarily auto-completes the method expressions with parentheses and all, confusingly making the JSF starter to think that they are actually required. I've even seen code snippets coming along from starters who actually used #{bean.getProperty()} instead of #{bean.property} to output a property, which would then later fail with a javax.el.PropertyNotWritableException when used in an input component.
Just leave out those argumentless parentheses. It's not true that this syntax is required and "normal" in JSF. Moreover, navigation rules are very JSF 1.x-ish. Also, performing navigation using POST requests is very JSF 1.x-ish. Maybe you're just learning and playing around. That's OK, but to learn about the right ways and a bit of history, carefully read below links:
Invoke direct methods or methods with arguments / variables / parameters in EL
Differences between action and actionListener
JSF implicit vs. explicit navigation
How to navigate in JSF? How to make URL reflect current page (and not previous one)
Difference between JSP EL, JSF EL and Unified EL
Last but not least, our JSF wiki page is a great place to start.

Bean's method that returns to the previous page

After performing the appropriate action, the method of my bean must ensure that the browser back to the previous page. How can I handle it?
I am using the CDI Conversation.
Pass the current URI as request parameter along during navigation to the page containing that action.
<h:link value="Go to page containing that action" outcome="pageContainingThatAction.xhtml">
<f:param name="from" value="#{request.requestURI}" />
</h:link>
(use #{view.viewId} instead if you want to pass the view ID)
Set and remember that parameter representing the URI in the view/conversation scoped managed bean.
<f:metadata>
<f:viewParam name="from" value="#{bean.from}" />
</f:metadata>
Finally redirect to that URI in that action method.
public void thatActionMethod() throws IOException {
// ...
externalContext.redirect(from);
}
After asking you a couple of question in comments, I think what you want to do is :
On your button, specify the attribute action to a function in your bean that return a String.
The String returned need to be the navigation path to the page you want to be redirected.
If the validation is wrong on button click, then simply return null and it will stay on same page.
Make sure you define the proper navigation rule in faces-config.xml
See this tutorial for how to configure navigation rules.
See Primefaces commandButton doc for info on action tag.

Omnifaces Faces.redirect loses conversation scope

I have problem with org.omnifaces.util.Faces#redirect and conversation scoped bean:
there is a button
<p:commandButton action="#{navigationHandler.gotoCreateCar}"
actionListener="#{createHandler.init(searchHandler.search())}
value="#{msg.search}" update=":articleSearchForm">
<f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
</p:commandButton>
which must do a navigation to createCar page within the same conversation scope after init of my conversation scoped bean: createHandler.
In the NavigationHandler#gotoCreateCar is just a call of Faces.redirect(createCarPage).
If I do like this the parameter cid is not transfered and I lose my conversation.
If I define a navigation rule in faces-config.xml:
<navigation-case>
<from-outcome>createCar</from-outcome>
<to-view-id>/portal/createCar.xhtml</to-view-id>
<redirect />
</navigation-case>
and in the NavigationHandler#gotoCreateCar just return the needed outcome - then it works fine.
Maybe I do not understand every detail in the difference between this two navigation approaches. I would be appreciated if somebody could help me to understand the problem.
Thanks!
The conversation propagation is handled by the navigation handler. The Faces#redirect() delegates to ExternalContext#redirect() which does not use the navigation handler. You'd better use Faces#navigate() instead which delegates to NavigationHandler#handleNavigation().
public void gotoCreateCar() {
// ...
Faces.navigate("/portal/createCar.xhtml?faces-redirect=true");
}
(note: no <navigation-case> is needed in this case)
Alternatively, just return exactly that string from the action method.
public String gotoCreateCar() {
// ...
return "/portal/createCar.xhtml?faces-redirect=true";
}
The Faces#navigate() is only useful when you're inside a (listener) method which doesn't support returning a navigation case outcome, such as #PostConstruct or preRenderView.

View-scoped bean recreated on POST when URL parameters not used

I have a view-scoped JSF-managed bean that's backing an xhtml view where I read one parameter from the URL using f:viewParam.
The view presents a form to the user. However, when the user submits the form by pressing the p:commandButton it seems that the view-scoped bean is recreated (I added a #PostConstruct annotation to verify this) and so doesn't remember the instance variable read from the f:viewParam (invId in the code below).
I originally navigate to the view with a GET that includes a URL parameter but the POST message that's send when the user presses the p:commandButton doesn't include the URL parameter. So I am thinking that when the JSF runtime doesn't see the URL parameter on the POST it considers this to be a different view and is recreating the JSF-managed bean. When I change the view scope to session-scoped the code works.
Here's the code:
view
<f:metadata>
<f:viewParam name="invId" value="#{registerBean.invId}"/>
</f:metadata>
<h:form id="registrationForm">
....
<p:commandButton value="register" action="#{registerBean.register}"
icon="ui-icon ui-icon-newwin" ajax="false"/>
</h:form>
backing bean
#ManagedBean
#ViewScoped
public class RegisterBean implements Serializable {
#ManagedProperty(value="#{invId}")
private String invId;
...
update
It turns out that this wasn't related to the URL parameters at all. Following BalusC advice below I removed the c:when tags my view was using (relying on rendered attributes instead for the same effect), and now the view-scoped bean is no longer recreated and the invId field is properly retained.
The problem is not visible in the code posted so far, but it's for JSF 2.0/2.1 a known issue that a view scoped bean will be recreated when one of its properties is been bound to an attribute of a taghandler like JSTL <c:if> or Facelets <ui:include> or a view build time attribute of JSF component, such as id and binding, while partial state saving is enabled (as by default).
The background explanation is that any EL expressions in those attributes are executed during building and restoring the view. As view scoped beans are stored in the view and thus only available after restoring the view, such an EL expression evaluation would cause a brand new and separate view scoped bean to be created. This is basically a chicken-egg issue. It's fixed in the upcoming JSF 2.2.
There are basically 3 solutions:
Change the view accordingly so that those EL expressions are only evaluated during view render time. E.g. replace <c:if>/<c:choose> by rendered.
Or bind those attributes to a request scoped bean (design notice: you can just inject a view scoped bean as a managed property of a request scoped bean).
Turn off partial state saving, if necessary only for the particular view.
See also:
JSTL in JSF2 Facelets... makes sense?
#ViewScoped fails in taghandlers

Resources