掲示板

correspondence jsp code in java

11年前 に franco33 albert によって更新されました。

correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
hello,

can someone help me retrieve the current user in a java class

in other words I want to make the correspondence of this JSP code in java

<liferay-ui:user-display userId="<%= user.getUserId() %>" />



So replace the jsp code in java code

best regards

franco
thumbnail
11年前 に David H Nebinger によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
Well the key is getting the current user.

That you can use com.liferay.portal.util.PortalUtil.getUser() to get the user or PortalUtil.getUserId() for the id. Just pass in the current portlet request and you're done.
11年前 に franco33 albert によって更新されました。

RE: correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
thank you for your answer
but I did not understand this trick

Just pass in the current portlet request


I try with com.liferay.portal.util.PortalUtil.getUser (.........);

but the problem is to know the parameter

this parameter is portlet request
thumbnail
11年前 に David H Nebinger によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
Right, and you're getting that as part of any action or render request coming to your portlet...
11年前 に franco33 albert によって更新されました。

RE: correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
I tried with this code



import com.liferay.portal.model.User;
public class Test
{
private User user;



	public User getUser() {
		user=com.liferay.portal.util.PortalUtil.getUser(null);

// here I have this error under null  :  The method getUser(HttpServletRequest) is ambiguous for the type PortalUtil
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}

I write this code in my portlet called TestProject

so tried with

public User getUser() {
		user=com.liferay.portal.util.PortalUtil.getUser(TestProject);


but this error is displayed :
TestProject cannot be resolved to a variable


I'm sorry if my questions are juniors
11年前 に franco33 albert によって更新されました。

RE: correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
even if I treid with this code to retreive givenName of current User


import javax.portlet.PortletRequest;

public class Test
{

private PortletRequest renderRequest;
Map userInfo = (Map)renderRequest.getAttribute(PortletRequest.USER_INFO);
String givenName ;

public String getGivenName()
{
givenName=(userInfo != null) ? (String)userInfo.get("user.name.given") : "";
return givenName;
}

public void setGivenName(String givenName)
{
this.givenName=givenName;
}


}

but I can't retreive the givenName of current user




but I think that the solution is as you say :
com.liferay.portal.util.PortalUtil.getUser
thumbnail
11年前 に David H Nebinger によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
Right, so if you have a full-blown portlet (not some weird test case thing that you seem to be trying), you'll have code that handles ActionRequests and RenderRequests. These are both instances of PortletRequest.

So, once you have your full portlet code bundled up into a war file and deployed to a Liferay instance, drop your portlet on a page and, lo and behold, behind the scenes it will now be working.
thumbnail
11年前 に Hitoshi Ozawa によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 7942 参加年月日: 10/03/24 最新の投稿
Check the "How to determine which User Group the logged in user belongs to" code sample in the following page:

http://songcuulong.com/public/html/liferay/serverside/quicknote.html
11年前 に franco33 albert によって更新されました。

RE: correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
thank you again for your reply

but until now I can not retrieve a user connected through java code

I can make it through a jsp page
&lt;%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %&gt;

<liferay-theme:defineobjects />
<portlet:defineobjects />

<liferay-ui:user-display userId="<%= user.getUserId() %>" />


but how to do the same thing but this time through java code

currently without involving the notion of role
thumbnail
11年前 に Hitoshi Ozawa によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 7942 参加年月日: 10/03/24 最新の投稿
If you can't comprehend the java code in the link I've given you, I think you'll better start off by learning java before trying to code liferay.
Liferay has some developer's education courses which you can take. You can ask you questions there.

public class MyGreetingPortlet extends MVCPortlet {
@Override
public void processAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
User user = PortalUtil.getUser(actionRequest);
11年前 に franco33 albert によって更新されました。

RE: correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
thank you again for your reply

I'm not beginner with java

but with liferya we should use same methods to retrieve the current user

I give you the test that I made

Note my portlet is done with struts

I try to show FirstName of the user connected

the java code:


public class testAction extends ActionSupport {


	private String FirstName;
public void processAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
try {
	User user = PortalUtil.getUser(actionRequest);
	setFirstName(user.getFirstName());
} catch (PortalException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (SystemException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} 
}


	public String getFirstName() {
FirstName=user.getFirstName();
		return FirstName;
	}


	public void setFirstName(String firstName) {
		FirstName = firstName;
	}




}


and the jsp code


&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;

the FirstName is :<s:property value="FirstName" />




but when I deploy the application

only this message is displayed
the FirstName is :
without retrieve the value of the firstName of the user connected
thumbnail
11年前 に David H Nebinger によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
Your code is a mess. Who knows if your action class is ever getting invoked? Where's your form/action demarcation? Where is there at least one comment so your coworkers will know what you're doing?

Long story short, your crappy code aside, if you output or log the result of the setFirstName(user.getFirstName()) line, I think you'll see that the underlying code is working it's just your crap that isn't.
11年前 に franco33 albert によって更新されました。

RE: correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
you ask : Who knows if your action class is ever getting invoked?


but I make


    public String getFirstName() {
FirstName="franco";
        return FirstName;
    }


my portlet when it is deployed displys this result :

the FirstName is : franco


the problem is how to fill firstName

with its getter

here we don't need to make action in jsp page and we don't need to moify something in strust.xml


Note that I did deploy a portlet that contains several things with the concepts that you addressed in your response but I give you a party java code which normally can help me retrieve firstName of the user connected
thumbnail
11年前 に David H Nebinger によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
So all you've proven is that your action class is not getting invoked. Portlets have two phases - action and render. The action phase is invoked when you do something in the portlet. The render phase is invoked when the portlet has to render it's content. When first dropped on a page, only the render phase is processed; only after you do something in the portlet will the action phase get invoked.

Your code relies on the action phase getting invoked. The fake getter that you're using sets the value that is used only by the render phase, thus proving the problem is your code.
11年前 に franco33 albert によって更新されました。

RE: correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
Assuming my portlet contains a single jsp page
the code for this jsp page


&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;

the FirstName is :<s:property value="FirstName" />



I want after portlet deployment in liferay

that the value of FirstName is filled without doing anything else on the page to inject the filler

all I want is the same principle of this code


&lt;%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %&gt;

<liferay-theme:defineobjects />
<portlet:defineobjects />

<liferay-ui:user-display userId="<%= user.getUserId() %>" />




since this code after deployement portlet displays the user logged on this page jsp of portlet
but this time in java



therefore


My problem is with this java code


public class testAction extends ActionSupport {


    private String FirstName;
public void processAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
try {
    User user = PortalUtil.getUser(actionRequest);
  
} catch (PortalException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SystemException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}


    public String getFirstName() {
FirstName=user.getFirstName();
        return FirstName;
    }


    public void setFirstName(String firstName) {
        FirstName = firstName;
    }




}




can please help me correct it



I do not like asking java code correction

but this time I'm stuck and frankly I overcome this problem and do something else


best regards

franco
thumbnail
11年前 に David H Nebinger によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
franco33 albert:
Assuming my portlet contains a single jsp page
the code for this jsp page


&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;

the FirstName is :<s:property value="FirstName" />



Right, that's the part that your not understanding Franco.

The JSP that you've shown above is called during the render phase of portlet handling. Effectively on the server side the portlet is processing the render request and therefore can call PortalUtil.getUser(renderReq) to get the current user and display the first name.

Your java code, however, is all based on responding to an action request, i.e. a user clicks a button in your struts app and the struts action is invoked, but again it is only in the action request handling.

If you structured your code such that you display a button that says 'click me' that in turn calls your struts action, your action handler would have access to the user object and would set the first name. It would then be passed to the render phase where the value would exist (just as you had stuck in the "Franco" before as a hard-coded string).

These things are the basics of portlet development. There's lots of stuff online to help you learn and understand the portlet phases (action, render, etc.) and there are plenty of books on portlet development too.

Your biggest problem at this point is not understanding the lifecycle of a portlet request. Until you learn the different phases, you're going to have a difficult time putting any portlet together.
thumbnail
11年前 に Hitoshi Ozawa によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 7942 参加年月日: 10/03/24 最新の投稿
What David is trying to explain is written in Liferay's Development manual. You're better off taking Liferay's courses to learn portlet basics because you're not understanding the basics.

http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/understanding-the-two-phases-of-portlet-executi-4
11年前 に franco33 albert によって更新されました。

RE: correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
thank you again for your response

I show you why I want to retrieve the logged in user after authentication through liferay

my JEE application is developed with struts2 and contains two profiles (or roles)
: Consultant and Director

and in this application I do not create jsp page for authentication because I use liferay authentication (the concept of SSO)

and I work with the user table that is generated automatically by liferay

So in my JEE application I haveto retrieve the connected user and I have to test its role:
if it is Director so I have only seen the director jsp pages and in the same way for the consultant role


So I must do this retreive in the java code : the user logged on without any action in my portlet deployed


the user object which is declared in my Java class must be filled across the page

login.jsp of liferay (this jsp page is for authentication)


So the final test senario:

I have two users in my database:

Name: Franco, email: franco@test.org, login: Franco, Role: Director
Name: adam Email: f@test.org, login: Adamm, Role: Consultant

and after deployment of my JEE apllication in liferay

if I made the authentication with franco@test.org and for login: franco

I need to find in the application deployed only the jsp pages for the Director



to make this senarion I do in a java class of my apllication



import com.liferay.portal.model.User;
public class extends testAction ActionSupport {


      private User user;
public void processAction (
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
try {
     user = PortalUtil.getUser (actionRequest);
   
} Catch (e PortalException) {
    / / TODO Auto-generated catch block
    e.printStackTrace ();
} Catch (SystemException e) {
    / / TODO Auto-generated catch block
    e.printStackTrace ();
}
}



 public String initial() throws Exception {

user = PortalUtil.getUser (actionRequest);
if (user.getRole (). equals ("director"))
return "AuthDirector";

return "AuthoConsultant";
    }


}



for the file struts.xml


<!--? Xml version = "1.0" encoding = "UTF-8"?-->
<!-- DOCTYPE struts PUBLIC
    "- / / Apache Software Foundation / / DTD Struts Configuration 2.0 / / EN"
    "Http://struts.apache.org/dtds/struts-2.0.dtd"-->

<struts>
<package name="view" extends="struts-portlet-default" namespace="/view">

<action name="initial class=" com.test.action.testaction" method="">

<result name="AuthDirector"> / WEB-INF/view/HomeDirector.jsp <!-- result-->

<result name="AuthoConsultantr"> / WEB-INF/view/HomeConsultant.jsp <!-- result-->
<!-- Action-->

<!-- Package-->
<!-- Struts-->

</result></result></action></package></struts>



and I do another test without success


import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import com.liferay.portal.model.User;
public class extends testAction ActionSupport {


  private User user;

FacesContext fc = FacesContext.getCurrentInstance();
	     ExternalContext externalContext = fc.getExternalContext();
	     Long id = Long.parseLong(externalContext.getUserPrincipal().getName());

 public String initial() throws Exception {

user = UserLocalServiceUtil.getUserById(id);
if (user.getRole (). equals ("director"))
return "AuthDirector";

return "AuthoConsultant";
    }


}
thumbnail
11年前 に Hitoshi Ozawa によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 7942 参加年月日: 10/03/24 最新の投稿
I recommend that you read Liferay's manual and the book "Liferay in Action" before posting any more questions here.
You need to learn the basics.
11年前 に franco33 albert によって更新されました。

RE: correspondence jsp code in java

Junior Member 投稿: 35 参加年月日: 12/04/28 最新の投稿
frankly I read several papers on this problem

but I am surprised because I can't find an answer to a basic issue of liferay in this forum


the goal is how to retrieve the user logged in liferay

I need to retrieve a user after authentication in the authentication page of liferay

because the goal is to do the redirect of the jsp page of my application deployed in liferya according to the profiles of user who is connected


Note I need to do the recovery of the user is logged in the Java code

So the code should be in the controller since I work with struts

I think I need to work with this type of test


import com.liferay.portal.model.User;
public class testAction  extends ActionSupport {
 
 
      private User user;
public void processAction (
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
try {
     user = PortalUtil.getUser (actionRequest);
 
} Catch (e PortalException) {
    / / TODO Auto-generated catch block
    e.printStackTrace ();
} Catch (SystemException e) {
    / / TODO Auto-generated catch block
    e.printStackTrace ();
}
}
 
 
 
public String initial() throws Exception {
 
user = PortalUtil.getUser (actionRequest);
if (user.getRole (). equals ("director"))
return "AuthDirector";
 
return "AuthoConsultant";
    }
 
 
}
thumbnail
11年前 に David H Nebinger によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
Look, Franco, we're not trying to be mean or anything about this. The basic code

public void processAction (ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
    User user = PortalUtil.getUser(actionRequest);
}


will work to get the current user, but it is only called as a result of an incoming action invoked by a user.

JSP is a different beast in that it is used for both the action and render handling. When you see the code in the JSP page, that code is actually used twice in the portlet lifecycle. The first is during the response to any user activity (i.e. clicking a button or whatever) which turns into an action request for the portlet to handle. The second time it is used is when the portlet needs to render it's content.

At both times it is being used, a portlet request is available and the PortalUtil.getUser() call works.

When you're moving code from JSP into Java, you have to be aware of the portlet lifecycle to understand where and when your java code is being called.

In the portlet world, the first time the portlet is ever shown to a user, only the render request is received. Your code is only in an action handler, so it will not be executed yet. If you have a button that the user clicks on to invoke your action handler, and the action handler gets you back to the page that you were on, you will actually have a value where you didn't before.
thumbnail
11年前 に Hitoshi Ozawa によって更新されました。

RE: correspondence jsp code in java

Liferay Legend 投稿: 7942 参加年月日: 10/03/24 最新の投稿
but I am surprised because I can't find an answer to a basic issue of liferay in this forum


I suggest you take one of the Developer's educational course offered by Liferay.