Java – get request parameters in @ viewscoped bean
I have a list from which the user can select "PQ" (link list) When clicking or otherwise entering the browser, the main page of each PQ shall be displayed Each PQ page is a table
Http: / / local host: 8080 / project name / main jsf ID = 2
This is the first PQ bean:
@Named @ViewScoped public class PqHome implements Serializable { @PersistenceContext(unitName="...") private EntityManager em; private Integer id; private PQ instance; @postconstruct public void init() { System.out.println("ID is " + id); // ID from URL param instance = em.find(PQ.class,id); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public PQ getInstance() { return instance; } }
This is main xhtml:
<ui:composition xmlns="http://www.w3.org/1999/xhtml" ...> <ui:define name="Metadata"> <f:Metadata> <f:viewParam name="id" value="#{pqHome.id}"> <f:convertNumber integerOnly="#{true}" /> </f:viewParam> <!--f:event type="preRenderView" listener="#{pqHome.init}" /--> </f:Metadata> </ui:define> <ui:define name="title"> <h:outputText value="Main" /> </ui:define> ... </ui:composition>
Whenever I choose or otherwise refresh a page / url, I get a NullPointerException from the entitymanager:
org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke [method] @postconstruct public de.mycomp.myproj.beans.PqHome.init() on de.mycomp.myproj.beans.PqHome@4f0ea68f at org.jboss.weld.bean.AbstractClassBean.defaultpostconstruct(AbstractClassBean.java:595) ... Caused by: java.lang.IllegalArgumentException: id to load is required for loading at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:87) at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:59) at org.hibernate.internal.SessionImpl.get(SessionImpl.java:961) at org.hibernate.internal.SessionImpl.get(SessionImpl.java:957) at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:787) at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:762) at org.jboss.as.jpa.container.AbstractEntityManager.find(AbstractEntityManager.java:221) at de.mycomp.myproj.beans.PqHome.init(PqHome.java:47) ... 56 more
[line 47 is em.find (...)]
This line
<f:event type="preRenderView" listener="#{pqHome.init}" />
It won't make things better I'm depressed now
How to pass URL get request parameters into @ viewscoped bean?
Note: I bet it's not a trivial matter I may have made a conceptual mistake, so any tips on how to improve are welcome I think I need to select @ viewscoped because there will be a more complex Ajax based GUI on this page. I really want to access it through the URL get parameter
thank you
Solution
Call @ postconstruct directly after bean construction and all dependency injection (for example, @ persistencecontext, @ EJB, @ managedproperty, @ inject, etc.)
< F: viewparam > set its value in the update model value stage, which is the long-term (post) construction of the bean Therefore, the < F: viewparam > value has not been set in @ postconstruct It was still empty then
You are close to < F: event type = "prerenderview" >, but you must delete the @ postconstruct comment
So:
<f:viewParam name="pq" value="#{pqHome.id}"> <f:convertNumber integerOnly="#{true}" /> </f:viewParam> <f:event type="preRenderView" listener="#{pqHome.init}" />
with
private Integer id; public void init() { instance = em.find(PQ.class,id); }
It has nothing to do with the specific problem. I suggest using a converter instead See also communication in JSF 2.0 – converting and validating get request parameters
@The named @ viewscoped combination also does not work as expected JSF specific @ viewscoped is only used with JSF specific @ managedbean Your CDI specific @ named will appear as @ requestscoped. In this way Use @ managedbean instead of @ named or CDI specific @ conversationscoped instead of @ viewscoped