I'm doing a page with jsf 2.0 and i wanna do something like this:
<f:metadata>
<f:viewParam name="id" value="${id}" />
<f:event type="preRenderView" listener="#{controller.initPage(id)}"/>
</f:metadata>
....(Some code)....
<c:forEach items="#{bean.listLoadedByInitPage}" var="var">
#{var.something}
</c:forEach>
The method initPage(id) must load list in the bean. But seems that method is called after than c:forEach loads items not before. Any ideas?
JSTL tags runs during view build time. The <f:event type="preRenderView"> runs right before view render time. In other words, <c:forEach> runs before <f:event>. So, this behaviour is fully expected.
You have 2 options:
Use #ManagedProperty instead of <f:viewParam>, or when the bean is in the view scope or broader, grab it manually from ExternalContext#getRequestParameterMap() inside #PostConstruct. And, use #PostConstruct instead of <f:event type="preRenderView">. Yes, this makes the entire <f:metadata> obsolete. You can safely remove it.
Use a JSF component instead of <c:forEach> tag, such as <ui:repeat>.
See also:
JSTL in JSF2 Facelets... makes sense?
Related
I want to invoke an EJB method when I click a link in my JSF website. At the moment I use an h:commandLink within an h:form tag to trigger the EJB method:
<h:form>
<h:commandLink value="Logout" action="#{loginBean.logout}"/>
</h:form>
Is there a way to do the same without the need of an h:form tag?
if you don't want a form (but i don't get why) you can:
navigate to a servlet and execute your code there.
navigate to another jsf page and put your code inside a bean #PostContruct
navigate to another jsf page and put your code inside a bean method and use <f:event type="preRenderView" listener="#{bean.listener}"/>
It has to be inside a form AFAIK.
Trying to display the pages dynamically based on the dropdown list value.All the associated components are rendering properly.When I bean is in view scope the validations are not triggering whereas the samething is working fine with session scope.Could anyone please help me to resolve the issue?
Here follows my code of Main.xhtml This page contains the dropdownlist.Based on the dropdown value dynamically including the pages.
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:composition template="#{templates.standard}">
<ui:define name="contentArea">
<c:choose>
<c:when test="#{testBean.value == '1'}">
<h:panelGroup>
<ui:include src="Page1.xhtml" />
</h:panelGroup>
</c:when>
<c:when test="#{testBean.value == '2'}">
<h:panelGroup>
<ui:include src="Page2.xhtml" />
</h:panelGroup>
</c:when>
</c:choose>
</ui:define>
</ui:composition>
</html>
The below Page1.xhtml will be included dynamically in Main.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:panelGroup>
<h:inputText value="#{testBean1.date}"
id="date"
required="true" requiredMessage="Enter Valid date">
<f:validator validatorId="test.TestDateValidator" />
</h:inputText>
</h:panelGroup>
<h:panelGroup>
<h:message for="date"/>
</h:panelGroup>
View scoped beans are stored in the JSF view. The JSF view is only available when it has been built. Taghandlers like JSTL <c:xxx> run during view build time. They thus run before the JSF view is available. So, when you bind a property of a view scoped bean to a JSTL tag attribute, then it would not refer the view scoped bean instance in the JSF view, but instead refer a freshly created one, with all properties set to default.
So, basically, you end up with two different instances of a view scoped bean on a per-request basis. One which is used during restoring the view (the one which is freshly recreated on every form submit) and another one which is used during processing the form submit (the one which was actually stored in the view scope).
This chicken-egg issue is already reported as JSF issue 1492 and fixed for the upcoming JSF 2.2.
Until then, your best bet is to create a separate request scoped bean and let the include condition depend on a request parameter which is injected by #ManagedProperty, or to turn off partial state saving (which may however have memory/performance implications). Note that the <ui:include> also runs during view build time, so wrapping it in a JSF component with the rendered attribute won't help anything as it is evaluated during view render time.
See also:
JSTL in JSF2 Facelets... makes sense?
Communication in JSF 2.0 - #ViewScoped fails in taghandlers
I'm trying to pass a parameter between a JSF page to another, from a bean to another. I know it is a common question, infact I've tried several approaches before writing it down.
To do that I have put both the beans in session scope and added in the first bean the following:
<p:commandButton value="Submit" type="submit"
actionListener="#{sourceBean.save}" action="success">
<f:setPropertyActionListener
target="#{targetBean.foo}" value="#{sourceBean.foo}" />
</p:commandButton>
The problem is that I don't want these beans to be in session scope but in view scope.
So I tried to put in my first page:
<p:commandButton value="Submit" type="submit"
actionListener="#{sourceBean.save}" action="success">
<f:param name="foo" value="#{sourceBean.foo}"/>
</p:commandButton>
And in the second page:
<f:metadata>
<f:viewParam id="foo" name="foo" value="#{targetBean.foo}"
/>
</f:metadata>
The problem is that the passed String is null so, obviously, I get an error form the Converter.
I think I'm missing something in the configuration of my managed beans. Do I have to link target and source bean in someway?
At this moment I have this configuration:
<managed-bean>
<managed-bean-name>targetBean</managed-bean-name>
<managed-bean-class>guiBeans.TargetBean</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>sourceBean</managed-bean-name>
<managed-bean-class>guiBeans.SourceBean</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
</managed-bean>
Another question: In my app, the value foo.id, that I use during the conversion, is set autonatically by the database when I save the object so when I call:
actionListener="#{sourceBean.save}"
The converter gets the id and turns it into a String (and viceversa, if needed).
So, I wanted to know if in JSF is first called the actionListener or the function that sets the parameters .
Could be this the reason why I get a null String? Thanks a lot.
The <f:param> is evaluated during rendering of the form, not during submitting of the form. Your problem suggests that the #{sourceBean.foo} value is only been set during submitting the form and thus not available during rendering of the form.
You'd basically need to replace action="success" by action="#{bean.action}" with
public String action() {
return "success?foo=" + foo.getId();
}
Or, if you're using navigation cases
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/some.xhtml</to-view-id>
<redirect>
<view-param>
<name>foo</name>
<value>#{sourceBean.foo.id}</value>
</view-param>
</redirect>
</navigation-case>
Unrelated to the concrete problem, the <f:param> doesn't support the converter attribute at all. You'd have to access the desired property directly (which is id in the above example).
I want to create a composite component which adds some common meta data to views, like:
<viewController:metadata controller="orderController"/>
the component:
<composite:interface>
<composite:attribute name="controller" />
</composite:interface>
<composite:implementation>
<f:metadata>
<f:viewParam name="id" value="#{cc.attrs.controller.id}" />
<f:event type="preRenderView"
listener="#{cc.attrs.controller.initConversation}" />
</f:metadata>
</composite:implementation>
i do similar things to add a button bar to all views and it works fine, but it seems its not possible with f:metadata.
Am i right or is there something wrong with my code?
thanks
This isn't possible. The <f:metadata> has to go in the view associated with the view ID. See also the tag documentation (emphasis mine):
Declare the metadata facet for this view. This must be a child of the <f:view>. This tag must reside within the top level XHTML file for the given viewId, or in a template client, but not in a template. The implementation must insure that the direct child of the facet is a UIPanel, even if there is only one child of the facet. The implementation must set the id of the UIPanel to be the value of the UIViewRoot.METADATA_FACET_NAME symbolic constant.
I have a product.xhtml and a ProductBean. I use /product/{id} to access the products so I have a viewParam in product.xhtml with value=ProductBean.id. The problem is that inside the bean I use an init function with a PostConstruct annotation in order to fill the details of the product. To do this I need the id to call an external function. I guess though that init is called before viewParam sets the id of the bean and therefore inside init I cannot call the external function because id is not set yet. What am I doing wrong and how do I fix this?
UPDATE
I found what was wrong. I think the viewParam method works with CDI beans but the ManagedProperty method works with JSF beans..
I do have one other problem now. My CDI bean is RequestScoped and when the product.xhtml is rendered the bean is created and I guess is later discarded. The funny thing is that I have a function inside that bean which when I call, I can read the id (which I assume this happens because is connected to the view param) but not any other properties. Any ideas how to fix this?
You need a <f:event type="preRenderView"> instead.
<f:metadata>
<f:viewParam name="foo" value="#{bean.foo}" />
<f:event type="preRenderView" listener="#{bean.onload}" />
</f:metadata>
With
public void onload() {
// ...
}
Note that this is in essence a little hack. The upcoming JSF 2.2 will offer a new and more sensible tag for the sole purpose: the <f:viewAction>.
<f:metadata>
<f:viewParam name="foo" value="#{bean.foo}" />
<f:viewAction action="#{bean.onload}" />
</f:metadata>
See also:
ViewParam vs #ManagedProperty(value = "#{param.id}")
Communication in JSF 2.0 - Processing GET request parameters