Tribune

Home » Liferay Portal » English » 3. Development

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Jay Sellers
How to ref a bean defined in faces-config.xml, in a portlet
14 febbraio 2009 11.40
Risposta

Jay Sellers

Punteggio: New Member

Messaggi: 21

Data di Iscrizione: 12 aprile 2008

Messaggi recenti

I am trying to figure out how to get access to a request-scoped bean, inside my FacesPortlet implementation class. My ultimate goal is to set values on a request-scoped bean inside my portlet class. That request-scoped bean will then be referenced by my jsp to display values, such as:
<h:outputText value="#{MyJSFBean.name}" id="Name"/>

In my render method, FacesContext is always null. I'm attempting to get the FacesContext because I assume that from the FC I can get a handle to my request-scoped bean, defined in faces-config.xml. The ultimate question is, how do I get a handle to a bean defined in faces-config.xml, from inside a portlet?


My environment:
MyPortlet.java extends FacesPortlet
MyJSF.jsp is my view
MyJSF.java is the backing bean for MyJSF.jsp (request scoped bean)
MyJSFBean.java is the model that MyJSF.java uses and that MyJSF.jsp references. (request scoped bean)


portlet.xml:
<portlet>
....
<portlet-class>com.my.portlet.MyPortlet</portlet-class>
<init-param>
<name>com.sun.faces.portlet.SAVE_REQUEST_SCOPE</name>
<value>true</value>
</init-param>
<init-param>
<description>Portlet Init View Page</description>
<name>com.sun.faces.portlet.INIT_VIEW</name>
<value>/MyJSF.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
...
</portlet>


In MyPortlet I override the render method:
@Override
public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException {
FacesContext fc = FacesContext.getCurrentInstance();
System.out.println("fc: " + fc);
super.render(request, response);
}

fc is always null
Jay Sellers
RE: How to ref a bean defined in faces-config.xml, in a portlet
14 febbraio 2009 12.50
Risposta

Jay Sellers

Punteggio: New Member

Messaggi: 21

Data di Iscrizione: 12 aprile 2008

Messaggi recenti

I tried adding MyPortlet to the faces-config.xml as a request bean, just for kicks. Didn't work.
Tomas Polesovsky
RE: How to ref a bean defined in faces-config.xml, in a portlet
15 febbraio 2009 15.00
Risposta

Tomas Polesovsky

LIFERAY STAFF

Punteggio: Liferay Master

Messaggi: 574

Data di Iscrizione: 13 febbraio 2009

Messaggi recenti

Hi,

I assume that you use Sun JSF portlet bridge in version 1.1.5 (which I have sources of ;) )

If you override the render method, there is no JSF logic that can be taken into action. If you want to override the render method take a look in the FacesPortlet's renderFaces method. There you can see how is the JSF environment initialized and you can see the code:
1
2        FacesContext context =
3            getFacesContextFactory().getFacesContext
4            (portletConfig.getPortletContext(),
5             request, response, getLifecycle());


I think that you can use this construction to access the FacesContext in your portlet (if the portlet extends FacesPortlet).
Jay Sellers
RE: How to ref a bean defined in faces-config.xml, in a portlet
15 febbraio 2009 15.36
Risposta

Jay Sellers

Punteggio: New Member

Messaggi: 21

Data di Iscrizione: 12 aprile 2008

Messaggi recenti

Thanks Tomas, that seems to have worked! At least it gave me back an instance of FacesContext. emoticon

Much appreciated!
Jay Sellers
RE: How to ref a bean defined in faces-config.xml, in a portlet
15 febbraio 2009 16.14
Risposta

Jay Sellers

Punteggio: New Member

Messaggi: 21

Data di Iscrizione: 12 aprile 2008

Messaggi recenti

Tomas, you wouldn't happen to know why my javascript isn't firing, would you? It seems to me like I remember coming across a discussion somewhere in the LR forums about javascript execution, but I can't seem to find it.

I'm simply trying to call a js function defined inside my jsf, in an onClick of a button. The usual jsf manor does not seem to be working.
Tomas Polesovsky
RE: How to ref a bean defined in faces-config.xml, in a portlet
16 febbraio 2009 13.16
Risposta

Tomas Polesovsky

LIFERAY STAFF

Punteggio: Liferay Master

Messaggi: 574

Data di Iscrizione: 13 febbraio 2009

Messaggi recenti

Hi,

I don't know your javascript and I cannot read your thoughts so far emoticon

The common mistake is that you should prefix your elements' IDs with <portlet:namespace /> and use that IDs in your javascript also.
Jay Sellers
RE: How to ref a bean defined in faces-config.xml, in a portlet
16 febbraio 2009 13.45
Risposta

Jay Sellers

Punteggio: New Member

Messaggi: 21

Data di Iscrizione: 12 aprile 2008

Messaggi recenti

lol. I just thought I'd give you a challenge!

I think it has to do with an attempt to point to an external js file. I'm trying to incorporate some yahoo-ui styles for a tabset components, w/out adding them to a theme, and they require some js. When I remove the link, my other js works just fine. So... yup

I will start a new thread for this issue though.

Thanks again.
Jay Sellers
RE: How to ref a bean defined in faces-config.xml, in a portlet
22 febbraio 2009 9.39
Risposta

Jay Sellers

Punteggio: New Member

Messaggi: 21

Data di Iscrizione: 12 aprile 2008

Messaggi recenti

So I can get a reference to FacesContext, now how can I grab a request-scoped bean defined in faces-config?
Jay Sellers
RE: How to ref a bean defined in faces-config.xml, in a portlet
22 febbraio 2009 9.54
Risposta

Jay Sellers

Punteggio: New Member

Messaggi: 21

Data di Iscrizione: 12 aprile 2008

Messaggi recenti

All I had to do was pose the question, and the answer showed up.

This code does get me my request-scoped bean, however "createValueBinding" appears to be deprecated. So...

FacesContext fc = getFacesContextFactory().getFacesContext(getPortletConfig().getPortletContext(),request, response, getLifecycle());

MyRequestBean myBean = (MyRequestBean) fc.getApplication().createValueBinding("#{MyRequestBean}").getValue(fc);
Jay Sellers
RE: How to ref a bean defined in faces-config.xml, in a portlet
22 febbraio 2009 16.56
Risposta

Jay Sellers

Punteggio: New Member

Messaggi: 21

Data di Iscrizione: 12 aprile 2008

Messaggi recenti

non-deprecated solution:

ExpressionFactory ef = fc.getApplication().getExpressionFactory();
ValueExpression ve = ef.createValueExpression(fc.getELContext(), "#{MyRequestBean}", MyRequestBean.class);
MyRequestBean myBean = (MyRequestBean)ve.getValue(fc.getELContext());