Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Celil Uzunel
Passing Parameters through JSF Pages Inside a Portlet
December 9, 2012 11:03 AM
Answer

Celil Uzunel

Rank: New Member

Posts: 4

Join Date: December 9, 2012

Recent Posts

HI Guys,


I'm new in developing Liferay Portlets with JSF 2.0. Before that i developed JSF 2 Webapplication and now I'm trying to develop it as a Portlet, but i have some problems:

I want to pass parameters from one JSF page to another JSF page using <f:param> and <f:viewParam>. As pure JSF Webapplication its working great, but as Portlet, its not working anymore. I tried to find solutions in the net, but i hadnt success. To be simple, i created a simple small portlet, where i can type a text and this text should be shown in the next JSF page. Here is my code:

BackingBean:

 1@SuppressWarnings("serial")
 2@ManagedBean
 3@ViewScoped
 4public class BackingBean implements Serializable {
 5   
 6    private String text;
 7
 8    public String getText() {
 9        return text;
10    }
11
12    public void setText(String text) {
13        this.text = text;
14    }
15   
16    public String navigate(){
17       
18        Map<String, String> param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
19        this.text = param.get("text");
20        return "/views/view2.xhtml";
21    }
22
23}


view.xhtml:

 1<?xml version="1.0"?>
 2
 3<f:view
 4    xmlns="http://www.w3.org/1999/xhtml"
 5    xmlns:c="http://java.sun.com/jsp/jstl/core"
 6    xmlns:f="http://java.sun.com/jsf/core"
 7    xmlns:h="http://java.sun.com/jsf/html"
 8    xmlns:ui="http://java.sun.com/jsf/facelets"
 9>
10    <h:head />
11    <h:body>
12        <h:outputText value="Type something:" />
13        <h:form>
14            <h:inputText value="#{backingBean.text}" />
15            <h:commandButton action="#{backingBean.navigate()}" value="Send" >
16                <f:param name="text" value="#{backingBean.text}"></f:param>
17            </h:commandButton>
18        </h:form>
19    </h:body>


view2.xhtml:

 1
 2<?xml version="1.0"?>
 3
 4<f:view
 5    xmlns="http://www.w3.org/1999/xhtml"
 6    xmlns:c="http://java.sun.com/jsp/jstl/core"
 7    xmlns:f="http://java.sun.com/jsf/core"
 8    xmlns:h="http://java.sun.com/jsf/html"
 9    xmlns:ui="http://java.sun.com/jsf/facelets"
10>
11<f:metadata>
12    <f:viewParam name="text" value="#{backingBean.text}"></f:viewParam>
13</f:metadata>
14    <h:head />
15   
16    <h:body>
17        <h:outputText value="Given text: #{backingBean.text}" />
18    </h:body>
19</f:view>


I would be glad about every help!


Celil
Andreas Feldmann
RE: Passing Parameters through JSF Pages Inside a Portlet
December 10, 2012 5:31 AM
Answer

Andreas Feldmann

Rank: New Member

Posts: 3

Join Date: December 10, 2012

Recent Posts

Interesting! I am trying to do the same thing:


I want to pass parameters from one JSF page to another JSF page using <f:param> and <f:viewParam>. As pure JSF Webapplication its working great, but as Portlet, its not working anymore.


I would be glad if anybody is able to help!

Thanks in advance!

Andi
Jan Geißler
RE: Passing Parameters through JSF Pages Inside a Portlet
December 10, 2012 6:54 AM
Answer

Jan Geißler

Community Moderator

Rank: Liferay Master

Posts: 526

Join Date: July 5, 2011

Recent Posts

I haven't used JSF at all, but my guess is, that the action method doesn't pass the parameter to the render method..
The difference from Servlet to portlet is, that a portlet has 2 phases. An Action and a render Phase to encapsulate the Action (DataManipulation) from the Rendering of a page. If you want to pass something from the Action Method to the RenderMEthod you should set it as attribute or as renderParameter in your action method.
Hope this helps, even if I am completely out of my comfort zone ;)

So long
David H Nebinger
RE: Passing Parameters through JSF Pages Inside a Portlet
December 10, 2012 7:06 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 8431

Join Date: September 1, 2006

Recent Posts

Celil Uzunel:
1    public String navigate(){
2       
3        Map<String, String> param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
4        this.text = param.get("text");
5        return "/views/view2.xhtml";
6    }



Parameters are namespaced. You will not have a request parameter named "text", but based on your code you really don't need it.

Basically how you defined your form, the backing bean text field will be populated by the input field during form submit. However, you're trying to access the parameter directly via the request parameter map. The value of text will be null and you're overriding the value of text sent in during the submit process.

Take out the first two lines (the Map and the this.text lines) and your code should work fine.
Jan Geißler
RE: Passing Parameters through JSF Pages Inside a Portlet
December 10, 2012 7:22 AM
Answer

Jan Geißler

Community Moderator

Rank: Liferay Master

Posts: 526

Join Date: July 5, 2011

Recent Posts

I stand corrected! emoticon
Celil Uzunel
RE: Passing Parameters through JSF Pages Inside a Portlet
December 11, 2012 1:29 AM
Answer

Celil Uzunel

Rank: New Member

Posts: 4

Join Date: December 9, 2012

Recent Posts

Hi guys,

thanks for your repliesemoticon. But deleting the two lines didn't help me, its still the same.
I checked the sample portlets of liferay and i saw, that they used there the Flash Scope, which is also working for me. Would you recommend to use it? If not, what can i do to pass parameters?
Jan Geißler
RE: Passing Parameters through JSF Pages Inside a Portlet
December 11, 2012 2:50 AM
Answer

Jan Geißler

Community Moderator

Rank: Liferay Master

Posts: 526

Join Date: July 5, 2011

Recent Posts

Maybe it DOES have something to do with my post, that parameters which get send to an action method are not available in the Render Method....
David H Nebinger
RE: Passing Parameters through JSF Pages Inside a Portlet
December 11, 2012 6:16 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 8431

Join Date: September 1, 2006

Recent Posts

JSF lifecycle has some similarities to the portlet lifecycle, but you shouldn't necessarily consider the two the same, Jan.

Flash scope is used when you're doing redirects as a result of your navigation. Normal request scope would keep the backing bean alive during the request processing, but if you're doing redirects (or perhaps the bridge is doing redirects on your behalf), you may need to use flash scope to keep the backing bean alive during the redirect...
Andreas Feldmann
RE: Passing Parameters through JSF Pages Inside a Portlet
December 11, 2012 6:22 AM
Answer

Andreas Feldmann

Rank: New Member

Posts: 3

Join Date: December 10, 2012

Recent Posts

Ah, thanks for the solution. I have found an example of this flash scope in this sample project:

https://github.com/liferay/liferay-faces/blob/master/demos/bridge/jsf2-portlet/src/main/webapp/views/confirmation.xhtml

Have a look :-)
Neil Griffin
RE: Passing Parameters through JSF Pages Inside a Portlet
December 11, 2012 3:00 PM
Answer

Neil Griffin

LIFERAY STAFF

Rank: Liferay Legend

Posts: 2227

Join Date: July 26, 2005

Recent Posts

Just wanted to mention that the JSF 2.0 f:viewParam feature isn't working yet in Liferay Faces Bridge. For more info, see: FACES-224.
Celil Uzunel
RE: Passing Parameters through JSF Pages Inside a Portlet
December 12, 2012 12:20 AM
Answer

Celil Uzunel

Rank: New Member

Posts: 4

Join Date: December 9, 2012

Recent Posts

Thanks for your infos emoticon...

I think, for now i will use the Flash-Scope.
Thanks for your helps!
Cristhian Camilo Lopez
RE: Passing Parameters through JSF Pages Inside a Portlet
May 5, 2013 6:57 PM
Answer

Cristhian Camilo Lopez

Rank: New Member

Posts: 15

Join Date: April 10, 2008

Recent Posts

Neil Griffin:
Just wanted to mention that the JSF 2.0 f:viewParam feature isn't working yet in Liferay Faces Bridge. For more info, see: FACES-224.



Hi Neil,

I'm trying to use f:viewParam on Liferay Faces Bridge 3.1.2-ga3 builded from git source today but I couldn't get it working.. any advice on this ?? since FACES-224 is fixed now...
Neil Griffin
RE: Passing Parameters through JSF Pages Inside a Portlet
May 6, 2013 1:20 PM
Answer

Neil Griffin

LIFERAY STAFF

Rank: Liferay Legend

Posts: 2227

Join Date: July 26, 2005

Recent Posts

The FACES-224 issue has an issue test portlet that you can find here.

Similarly, FACES-257 has an issue test portlet here.

Both of them make use of f:viewParam. The one for FACES-257 is really fun because it takes advantage of Liferay Friendly URLs. emoticon