Fórumok

[SOLVED] ThemeDisplay within portlet

Gaston Artemski, módosítva 14 év-val korábban

[SOLVED] ThemeDisplay within portlet

Junior Member Bejegyzések: 70 Csatlakozás dátuma: 2009.04.27. Legújabb bejegyzések
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.


public class LoginPortlet extends GenericPortlet {
	PrintWriter writer;
	PortletDisplay pd;
	ThemeDisplay td;
	
	public void doView(RenderRequest req, RenderResponse resp)
			throws IOException, PortletException {
		render(req,resp);
		resp.setContentType("text/html");
		writer=resp.getWriter();
		execute();
	}

	private void execute() {
		pd=new PortletDisplay();
		td=pd.getThemeDisplay();
		
		if(pd==null)
		{
			System.out.println("pd is null");
			return;
		}
		
		if(td==null)
		{
			System.out.println("td is null");
			return;
		}
		writer.write(td.getCompanyId()+"");
		writer.write(td.getUser().getFirstName());
	
	}
}



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
thumbnail
Victor Zorin, módosítva 14 év-val korábban

RE: ThemeDisplay within portlet

Liferay Legend Bejegyzések: 1228 Csatlakozás dátuma: 2008.04.14. Legújabb bejegyzések
themeDisplay is inserted into the request as attribute, so it is probably easier to get access to it using the following code:

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

// ..
	public ActionForward render(ActionMapping mapping, ActionForm form,
			PortletConfig config, RenderRequest req, RenderResponse res)
			throws Exception {
		
		ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
				WebKeys.THEME_DISPLAY);
		logger.info("themeDisplay.companyId=" + themeDisplay.getCompanyId());

// ..
thumbnail
Tarun S. Kayasth, módosítva 14 év-val korábban

RE: ThemeDisplay within portlet

Regular Member Bejegyzések: 162 Csatlakozás dátuma: 2007.06.08. Legújabb bejegyzések
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, módosítva 14 év-val korábban

RE: ThemeDisplay within portlet

Junior Member Bejegyzések: 70 Csatlakozás dátuma: 2009.04.27. Legújabb bejegyzések
Thank you for your both answers...it works now.

Fantastic


Greetings
Arek Martin Maltanski, módosítva 14 év-val korábban

RE: ThemeDisplay within portlet

New Member Bejegyzések: 10 Csatlakozás dátuma: 2009.07.10. Legújabb bejegyzések
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, módosítva 14 év-val korábban

RE: ThemeDisplay within portlet

Junior Member Bejegyzések: 70 Csatlakozás dátuma: 2009.04.27. Legújabb bejegyzések
Hello Arek,

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


public User getUser()
	{
		if(user==null)
		{
			FacesContext context = FacesContext.getCurrentInstance();
			PortletRequest portletRequest = (PortletRequest)context.getExternalContext().getRequest();       
			ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
			user=themeDisplay.getUser();
		}
		return user;
	}


Then you can get the id with

user.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, módosítva 14 év-val korábban

RE: ThemeDisplay within portlet

New Member Bejegyzések: 10 Csatlakozás dátuma: 2009.07.10. Legújabb bejegyzések
Hi Gaston,

Thansk for reply but, Unfortunately i still got error

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


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


I add library`s you mentioned and upgrade my web.xml with
  <context-param>
        <param-name>com.icesoft.faces.hiddenPortletAttributes</param-name>
        <param-value>THEME_DISPLAY</param-value>
    </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, módosítva 14 év-val korábban

RE: ThemeDisplay within portlet

Junior Member Bejegyzések: 70 Csatlakozás dátuma: 2009.04.27. Legújabb bejegyzések
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, módosítva 14 év-val korábban

RE: ThemeDisplay within portlet

New Member Bejegyzések: 10 Csatlakozás dátuma: 2009.07.10. Legújabb bejegyzések
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


 principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        id= Integer.parseInt(principal.getName());
        return id;


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

Greetings
Gaston Artemski, módosítva 14 év-val korábban

RE: ThemeDisplay within portlet

Junior Member Bejegyzések: 70 Csatlakozás dátuma: 2009.04.27. Legújabb bejegyzések
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


<managed-bean>
		<managed-bean-name>ThemeDisplay</managed-bean-name>
		<managed-bean-class>com.liferay.util.bridges.jsf.common.ThemeDisplayManagedBean</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>


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


<ice:outputtext value="#{ThemeDisplay.user.id}" />


Good luck and I am happy that it works for you...
asif aftab, módosítva 10 év-val korábban

RE: ThemeDisplay within portlet

Regular Member Bejegyzések: 123 Csatlakozás dátuma: 2013.09.02. Legújabb bejegyzések
We can also get User by this way and hence all the values related with this
user = PortalUtil.getUser(request);