Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Gaston Artemski
[SOLVED] ThemeDisplay within portlet
June 8, 2009 4:17 AM
Answer

Gaston Artemski

Rank: Junior Member

Posts: 73

Join Date: April 27, 2009

Recent Posts

Hello everybody,

I want to use ThemeDisplay in my portlet.
But if I want to get this in this way I only get null for ThemeDisplay.

 1
 2public class LoginPortlet extends GenericPortlet {
 3    PrintWriter writer;
 4    PortletDisplay pd;
 5    ThemeDisplay td;
 6   
 7    public void doView(RenderRequest req, RenderResponse resp)
 8            throws IOException, PortletException {
 9        render(req,resp);
10        resp.setContentType("text/html");
11        writer=resp.getWriter();
12        execute();
13    }
14
15    private void execute() {
16        pd=new PortletDisplay();
17        td=pd.getThemeDisplay();
18       
19        if(pd==null)
20        {
21            System.out.println("pd is null");
22            return;
23        }
24       
25        if(td==null)
26        {
27            System.out.println("td is null");
28            return;
29        }
30        writer.write(td.getCompanyId()+"");
31        writer.write(td.getUser().getFirstName());
32   
33    }
34}


I read in another forum that ThemeDisplay can only be accessed / initialized during the render phase, but this did not work too.
Perhaps somebody faced the same problem and has a solution for my problem

Greetings

Gaston
Victor Zorin
RE: ThemeDisplay within portlet
June 8, 2009 3:57 AM
Answer

Victor Zorin

Rank: Liferay Legend

Posts: 1177

Join Date: April 14, 2008

Recent Posts

themeDisplay is inserted into the request as attribute, so it is probably easier to get access to it using the following code:
 1
 2    import com.liferay.portal.theme.ThemeDisplay;
 3    import com.liferay.portal.util.WebKeys;
 4
 5// ..
 6    public ActionForward render(ActionMapping mapping, ActionForm form,
 7            PortletConfig config, RenderRequest req, RenderResponse res)
 8            throws Exception {
 9       
10        ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
11                WebKeys.THEME_DISPLAY);
12        logger.info("themeDisplay.companyId=" + themeDisplay.getCompanyId());
13
14// ..
Tarun S. Kayasth
RE: ThemeDisplay within portlet
June 8, 2009 4:01 AM
Answer

Tarun S. Kayasth

Rank: Regular Member

Posts: 160

Join Date: June 8, 2007

Recent Posts

Hi Gaston,

Use the following code:

import com.liferay.portal.theme.ThemeDisplay;
.
.

ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
WebKeys.THEME_DISPLAY);


by using theme display you can use

themeDisplay.getUser().getFirstName());


Thanks,

Tarun Kayasth
Gaston Artemski
RE: ThemeDisplay within portlet
June 8, 2009 4:16 AM
Answer

Gaston Artemski

Rank: Junior Member

Posts: 73

Join Date: April 27, 2009

Recent Posts

Thank you for your both answers...it works now.

Fantastic


Greetings
Arek Martin Maltanski
RE: ThemeDisplay within portlet
August 5, 2009 11:03 AM
Answer

Arek Martin Maltanski

Rank: New Member

Posts: 10

Join Date: July 10, 2009

Recent Posts

Hi Gaston,

Sorry to ask, but could you give me a little bigger explanation of how to build a class that will get userID?? I try to do this for some time with no effect. Whatever I do allways get null point exception.

I would really apreceate some more tips or a pice of code emoticon

Thanks
Gaston Artemski
RE: ThemeDisplay within portlet
August 6, 2009 5:28 AM
Answer

Gaston Artemski

Rank: Junior Member

Posts: 73

Join Date: April 27, 2009

Recent Posts

Hello Arek,

you need a bean with this method to get the user-object:

 1
 2public User getUser()
 3    {
 4        if(user==null)
 5        {
 6            FacesContext context = FacesContext.getCurrentInstance();
 7            PortletRequest portletRequest = (PortletRequest)context.getExternalContext().getRequest();      
 8            ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
 9            user=themeDisplay.getUser();
10        }
11        return user;
12    }

Then you can get the id with
1
2user.getUserId()

an many more attributes with other methods of the User-object.

When you compile this you need the jars portal-impl.jar,portal-kernel.jar and perhaps - I am not sure - portal-service.jar. You will find this jars within your liferay distrubution.
Important: You only need this jars by compiletime. Make sure that they are not in your war because you will get then errors - a class cast exception because this classes then would be two times, one time within the portal itself and a second time in your application.

If you have any further question please ask me.

Hope this helps..

Greetings
Arek Martin Maltanski
RE: ThemeDisplay within portlet
August 6, 2009 6:56 AM
Answer

Arek Martin Maltanski

Rank: New Member

Posts: 10

Join Date: July 10, 2009

Recent Posts

Hi Gaston,

Thansk for reply but, Unfortunately i still got error

1java.lang.ClassCastException: com.icesoft.faces.webapp.http.servlet.ServletExternalContext$1
2    com.icesoft.icefaces.tutorial.component.table.basic.TableBean.getUser(TableBean.java:153)
3    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
4    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
5.....


It points at the line with
1            PortletRequest portletRequest = (PortletRequest)context.getExternalContext().getRequest();


I add library`s you mentioned and upgrade my web.xml with
1  <context-param>
2        <param-name>com.icesoft.faces.hiddenPortletAttributes</param-name>
3        <param-value>THEME_DISPLAY</param-value>
4    </context-param>

It still takes me nowhere, and if you have any idea what can I do to get rid of this error I would be very greatful

Thanksss
Gaston Artemski
RE: ThemeDisplay within portlet
August 7, 2009 4:40 AM
Answer

Gaston Artemski

Rank: Junior Member

Posts: 73

Join Date: April 27, 2009

Recent Posts

Class castException often signalizes that there is one jar within your war that is allready in

/liferay-portal-5.2.2/tomcat-6.0.18/lib or
/liferay-portal-5.2.2/tomcat-6.0.18/ib/ext or
liferay-portal-5.2.2/tomcat-6.0.18/webapps/ROOT/WEB-INF/ib

Perhaps you have portlet.jar in your war that is allready in the ext folder. Check your jars please, that could be the key for the solution of your problem.

Yes I have this entry in web.xml too.

I use IceFaces too - version 1.8.1 -and it will work for you too

Look after jars and if you sure you don't have any jar twice - within your war and the mentioned - directories I will help you further.

Greetings

Gaston
Arek Martin Maltanski
RE: ThemeDisplay within portlet
August 9, 2009 1:32 PM
Answer

Arek Martin Maltanski

Rank: New Member

Posts: 10

Join Date: July 10, 2009

Recent Posts

Hi Gaston,

Thanks very much for all your effort to help me emoticon. I didn`t found any duplicate jar`s but I founded other solution. I wanted only user Id and I can get it witch this pice of code

1
2 principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
3        id= Integer.parseInt(principal.getName());
4        return id;


Maybe this isn`t the best solution but it works for me emoticon
Once again thanks for help

Greetings
Gaston Artemski
RE: ThemeDisplay within portlet
August 10, 2009 7:53 AM
Answer

Gaston Artemski

Rank: Junior Member

Posts: 73

Join Date: April 27, 2009

Recent Posts

Hi Arek,

I am happy that it works for you.
But if you have some time you should try it again with ThemeDisplay because this is the recommended way.
You can try this way if you like:

add to faces-config.xml the entry

1
2<managed-bean>
3        <managed-bean-name>ThemeDisplay</managed-bean-name>
4        <managed-bean-class>com.liferay.util.bridges.jsf.common.ThemeDisplayManagedBean</managed-bean-class>
5        <managed-bean-scope>request</managed-bean-scope>
6    </managed-bean>


and then you can get in your icefaces xhtml site the id and another attributes in this way:

1
2<ice:outputText  value="#{ThemeDisplay.user.id}"/>


Good luck and I am happy that it works for you...
asif aftab
RE: ThemeDisplay within portlet
April 23, 2014 12:03 AM
Answer

asif aftab

Rank: Junior Member

Posts: 95

Join Date: September 2, 2013

Recent Posts

We can also get User by this way and hence all the values related with this
user = PortalUtil.getUser(request);