Reputation: 1739
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>
Upvotes: 3
Views: 2318
Reputation: 71
I had a very similar (if not exact) problem. I was using Java EE 7 on Glassfish 4.0 build 89 and Primefaces 4.0. Glassfish was reporting that it was using Mojarra 2.2.0. I was using CDI type annotations, that is, @Named and @javax.faces.view.Viewscoped. My *.xhtml file started like such
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:metadata>
<f:viewAction action="#{personController.initialise}" />
<f:viewParam name="id" value="#{personController.id}" />
</f:metadata>
...
Regardless of what I tried, it never set my view parameter. I read there was an issue with Mojarra 2.2.1 and thought it may be the same with Mojarra 2.2.0. Updatetool didn't update Mojarra any further than what I had, so I manually did it. I put a copy of the latest Mojarra implementation from http://repo1.maven.org/maven2/org/glassfish/javax.faces/2.2.5/ into $GLASSFISH_HOME/glassfish/modules directory, renamed it javax.faces.jar and backing up the existing javax.faces.jar file. Restarted Glassfish and it reported it was now using Mojarra 2.2.5. The problems went away and the code worked fine.
Upvotes: 1
Reputation: 1109542
You're managing the bean by CDI, not by JSF. The JSF @ViewScoped
annotation works on JSF @ManagedBean
only, not on CDI @Named
. On CDI @Named
you 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 @ManagedProperty
annotation. It works in JSF @ManagedBean
only, not on CDI @Named
. For CDI you should use @Inject
or a custom HTTP param annotation.
JSF issue 2266 is unrelated to this all.
Upvotes: 2