Sorry, probably another really basic question. In my ViewScoped bean, a ‘viewParam’ looks like it’s getting set, but when I come to use it, the value is null. I put a breakpoint in the setter (setEventId()) and it gets the value, but in the method specified by my preRenderView, it’s gone, so I can’t load the Event object I am trying to retrieve.
This was working fine when my bean was RequestScoped, but I found that on a POST and subsequent validation error, all my details were lost and read that ViewScoped was the way to get around this problem.
I have upgraded to Mojarra 2.1.7 because I thought it might be a bug, and indeed there is a ‘critical bug’ listed in their JIRA, fixed in 2.1.7, but I verified in the Glassfish logs that it was using the newer version, and I still get the same problem: http://java.net/jira/browse/JAVASERVERFACES-2266
Please help, here’s my bean (I have tried with and without the ‘ManagedProperty’ annotation)
@ViewScoped
@Named
public class EventController extends AbstractController {
private static final Logger logger = Logger.getLogger("EventController");
/**
* Request param managed property
*/
@ManagedProperty(value="#{param.eventId}")
private Long eventId;
private Event event = new Event();
/**
* The event dao
*/
@Inject
private EventDao eventDao;
/**
* Load the event (requires eventId has a value)
* @return
*/
public void loadEvent() {
event = eventDao.find(eventId);
}
/**
* @return the eventId
*/
public Long getEventId() {
return eventId;
}
/**
* @param eventId the eventId to set
*/
public void setEventId(Long eventId) {
this.eventId = eventId;
}
}
Here’s how I’m constructing the link in the ‘listEvents’ page
<h:link value="Full details" outcome="/calendar/viewEvent" includeViewParams="true">
<f:param name="eventId" value="#{calendarController.event.eventId}" />
</h:link>
And here’s the page that needs the eventId property
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<body>
<ui:composition template="/WEB-INF/templates/standardTemplate.xhtml">
<f:metadata>
<f:viewParam name="eventId" value="#{eventController.eventId}"/>
<f:event type="preRenderView" listener="#{eventController.loadEvent}" />
</f:metadata>
<ui:define name="content">
<h1>Event details for: #{eventController.event.title}</h1>
<h:form>
<p:messages/>
<p:panelGrid style="margin-top:20px">
<f:facet name="header">
<p:row>
<p:column colspan="4">Event details</p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
Title
</p:column>
<p:column colspan="3">
<p:inputText value="#{eventController.event.title}" size="49"/>
<h:inputHidden id="eventId" value="#{eventController.event.eventId}"/>
</p:column>
</p:row>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
You’re managing the bean by CDI, not by JSF. The JSF
@ViewScopedannotation works on JSF@ManagedBeanonly, not on CDI@Named. On CDI@Namedyou can only use the CDI scopes, not the JSF scopes. The closest what CDI offers is the@ConversationScoped. But you’ve to manage the start and end of the conversation yourself with some additional boilerplate code.The same story applies to JSF
@ManagedPropertyannotation. It works in JSF@ManagedBeanonly, not on CDI@Named. For CDI you should use@Injector a custom HTTP param annotation.JSF issue 2266 is unrelated to this all.