I have the written the below code but its always returning user id (numeric like '40156' ,'20147' e.t.c) only not user name
Can somebody tell me how to get screen name
ThemeDisplay themeDisplay;
HttpServletRequest httpRequest;
themeDisplay = (ThemeDisplay) httpRequest.getAttribute(WebKeys.THEME_DISPLAY);
User user = null;
long userId1 = themeDisplay.getUserId();
user = UserLocalServiceUtil.getUserById(userId1);
String screenname1 = user.getScreenName();
I used the below code in java class, i can get the screen name now using the below code
now the "user" object has screen name in the variable _originalScreenName
HttpServletRequest request;
long companyID = PortalUtil.getCompanyId(request);
User user = UserLocalServiceUtil.getUserByEmailAddress(companyID, EmailID);
Related
I'm new to Spring and I want to:
1) when an user visits localhost/admin/users I want the predefined options to apply
2) On localhost/admin/users I have some buttons that perform a POST with four parameters because my boss don't want me to use get (and I think is better to use POST, too)
3) I have a controller method adminUsersPost that manages the POST request, and I want that method to be able to make my browser to reload using the adminUsersGet method, but with the information sent in the POST request.
What I'm getting now in my browser is an alert with a webpage content in some weird encoding, I hope it is correct but I don't know.
#RequestMapping(value = "/admin/users", method = RequestMethod.GET)
public ModelAndView adminUsersGet(
Integer page,
Integer items,
String sorting,
String sortingDirection)
{
// predefined options
Integer pagina = 1;
Integer itemsPorPagina = 10;
String ordenacion = "idUsuario";
String dirOrdenacion = "asc";
// end of predefined options
// Code that I want for it to use POST params from the other method
ModelAndView mv = new ModelAndView("adminUsers");
return mv;
}
#RequestMapping(value = "/admin/users", method = RequestMethod.POST)
public ModelAndView adminUsersPost(
#RequestParam(value = "pagina") Integer pagina,
#RequestParam(value = "itemsPorPagina") Integer itemsPorPagina,
#RequestParam(value = "ordenacion") String ordenacion,
#RequestParam(value = "dirOrdenacion") String dirOrdenacion)
{
// Here I try to pass the POST parameters to the GET method for reloading
// the webpage with the new content
return adminUsersGet(pagina, itemsPorPagina, ordenacion, dirOrdenacion);
}
The pattern POST params-->GET same parameters is a common one. What you need is RedirectAttributes which will store your parameters into the session and redirect to your GET method. Once the GET is hit spring will automatically remove all attributes from the session, thus none of the POST parameters will be displayed in the browser url in the GET method. Have a look here for a complete example and adjust it for your needs.
I am trying to use SpringMVC to handle a form but now I am stuck and I'm here to ask for your help.
When the user submit the form, my controller returns the jsp result page and the user can check what he filled in. In that result page I have 2 buttons, one to send definitively the form and one to modify it.
Here my problem : when I click on modify all the fields of the form are again empty and I would like to keep the value filled by the user.
My controller :
#Controller
#RequestMapping( "/adhesion" )
public class FormController {
//Send the form
#RequestMapping( method = RequestMethod.GET )
public String getFormulaire( #RequestParam( value = "user", required = false ) T_demande_adhesion User, Map<String, Object> model ) {
System.out.println( "user" + User ); //Null everytime
model.put( "user", User );
System.out.println( "Envoi formulaire !" );
return "formulaireAdhesion";
}
//Handle the form
#RequestMapping( method = RequestMethod.POST )
public String gestionAdhesion( #ModelAttribute( "formAdhesion" ) #Valid T_demande_adhesion user, BindingResult erreurForm, Map<String, Object> model )
throws ServletException, IOException {
final String CHEMIN_DOC_APP = "C:/DocumentsApp/";
// Form JSP page
final String FORMULAIRE_ADHESION = "formulaireAdhesion";
// Result page
final String AFFICHER_RESULT = "afficherResult";
// Objet de validation du formulaire
ValidationDmdAdhesion demandeAdhesion = new ValidationDmdAdhesion();
//Return AFFICHER_RESULT or FORMULAIRE_ADHESION
String resultat = demandeAdhesion.validationUser( user, erreurForm, model, FORMULAIRE_ADHESION, AFFICHER_RESULT, CHEMIN_DOC_APP );
//putting the object to the request
model.put( "user", user );
return resultat;
}
}
In JSP side I added the parameter 'User' in the request :
'
<input type="button" value="Modifier" onclick="document.location.href='<c:url value="adhesion?user=${user}"/>';" class="boutonAdhesion" />
'
But this send me a string and the url is :
/adhesion?user=com.user.entities.T_demande_adhesion#1dfe8158
When the user clicks on modifiy button (which is in the result page), I would like to get this object #ModelAttribute( "formAdhesion" ) #Valid T_demande_adhesion user on the GET request to fill in the form, in purpose to avoid the user to put again the same informations.
Any help ?
An extract from the form :
<label for="nom">Nom <span class="requis">*</span></label><input type="text" id="nom" name="identite.nom" value="<c:out value ="${user.identite.nom}"/>" size="35" maxlength="30" />
<spring-ext:errors path="identite.nom" class="erreur" firstErrorOnly="true"/>
It's here : value="<c:out value ="${user.identite.nom}"/> that I want to get the value of nom.
Sorry for the English.
Frenchy.
When I click on modify all the fields of the form are again empty and
I would like to keep the value filled by the user
Because the form data is already available on the page at client side, there is no meaning of submitting the form again to the server & show the same data, which is an unnecessary round trip from the client to the server i.e.,
To keep the data on the form, do NOT to submit the form to the server/controller itself.
How can I check the validation of the information filled in without
submit the form? How can I display them in my result page?
There is no need to validate the data that the user is anyhow going to modify. So, the validation of the form will be done only after the user completes the modifications and clicks on Submit. Once the validations are successful/failed, your can show the result on the results page.
In this case, I didn't find another way than using #SessionAttributes. In this point off my application I didn't to use a session but it seems that there isn't another way.
I need to pass one parameter (packageId) from one portlet's managed bean (I will call it beanA, with ViewScoped) where I do redirect:
LiferayPortletURL url = PortletURLFactoryUtil.create(portletRequest,
PortalUtil.getPortletId(portletRequest), themeDisplay
.getLayout().getPlid(), PortletRequest.ACTION_PHASE);
url.setParameter("_facesViewIdRender", "/views/esbDeployerView.xhtml");
url.setParameter("packageId", doc.getId());
url.setWindowState(WindowState.NORMAL);
url.setPortletMode(PortletMode.VIEW);
FacesContext.getCurrentInstance().getExternalContext().redirect(url.toString());
to another (beanB with ViewScoped) which is invoked by EL (a have a datatable there and call #{esbPackageDetail.services}) in esbDeployerView.xhtml after redirect). beanB is correctly created after redirect but when I try to set a breakpoint in on #PostConstruct method, the parameter value is null.
HttpServletRequest request = PortalUtil.getHttpServletRequest((PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
HttpServletRequest oriRequest = PortalUtil.getOriginalServletRequest(request);
String esbArchiveId = oriRequest.getParameter("packageId");
I have tried also this but the parameter is null again:
LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
String hallo = liferayFacesContext.getRequestQueryStringParameter("packageId");
EDIT:
I also tried this:
boolean parameter = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().containsKey("packageId");
boolean parametervalue = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().containsKey("cee43b8b-3719-4463-b317-e6fde80707c5");
both results are false...
I finally figured it out! The problem was in first step. It is necessary to set up PortletRequest to PortletRequest.RENDER_PHASE to keep a parameter alive during redirect. Then I was able to read it like:
HttpServletRequest request = PortalUtil.getHttpServletRequest((PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
String arumArchiveId = ParamUtil.getString(request, "packageId");
#Parkash Kumar Thank you for your help anyway.
This might work in your case:
Code sample from where you want to redirect:
String redirectURL = "public / private group" + "page_name" + "?packageId=" + doc.getId();
FacesContext.getCurrentInstance().getExternalContext().redirect(redirectURL);
Code sample to get parameter:
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest httpServletRequest = (HttpServletRequest) (externalContext.getRequestMap().get("com.liferay.portal.kernel.servlet.PortletServletRequest"));
PortletRequest portletRequest = (PortletRequest) httpServletRequest.getAttribute("javax.portlet.request");
String packageId = null;
HttpServletRequest httpRequest = null;
if(portletRequest != null){
httpRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(portletRequest));
packageId = httpRequest.getParameter("packageId");
}
I am trying to get the UUID from a user.
So far i tried
Accessing the user from a liferay portlet?
Get the current user Liferay using a simple Java code
putting String userId = renderRequest.getRemoteUser() into the view.jsp worked to get the intern ID.
However i wanted the UUID instead.
If i use the code from the links above (into the java-class doView) i only get a null-user object.
Using getUserUuid() and getUuid() returns null.
Here is my class:
ThemeDisplay td =(ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
User user = td.getUser();
String userId = user.getUuid();
renderRequest.setAttribute("myUser", userId);
and my view.jsp
<%
String userId = (String) renderRequest.getAttribute("myUser");
%>
<%= userId %>
Any help is appreciated.
On JSP, extract your parameter from implicit request object. Like:
<%
String userId = (String) request.getAttribute("myUser");
%>
How to obtain page's organization in liferay portlet correctly?
I tried:
Company. Is not an organization, but a portal instance.
Group. Is group of current user, not of current page.
UPDATE
I am in the portlet class' doView method. I know how to pass anything I can obtain here upwards to JSP to be accessible via EL/JSTL.
UPDATE 2
group.getOrganizationId() always returns zero.
UPDATE 3
This code I use to prepare variables
private void storeVariables(RenderRequest renderRequest) {
PortletPreferences prefs = renderRequest.getPreferences();
String image_id = (String) prefs.getValue("image_id", "communicator");
ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
Company company = themeDisplay.getCompany();
Group group = themeDisplay.getScopeGroup();
log.info("group = {}", group);
long organizationId = group.getOrganizationId();
log.info("organizationId = {}", organizationId);
PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
String contextPath = renderRequest.getContextPath();
if( !contextPath.endsWith("/") ) {
contextPath = contextPath + "/";
}
renderRequest.setAttribute("images", images);
renderRequest.setAttribute("image", images.get(image_id));
renderRequest.setAttribute("image_id", image_id);
renderRequest.setAttribute("themeDisplay", themeDisplay);
renderRequest.setAttribute("portletDisplay", portletDisplay);
renderRequest.setAttribute("contextPath", contextPath);
}
UPDATE 3
The following code in JSP returns empty string
<!-- scopeGroupId = <c:out value="${scopeGroupId}"/> -->
A group is a container of content and pages, and it's the internal entity for sites, layout scopes, staging and so on.
In Liferay 6.1, a site (which was called community in previous versions) can be associated to an organization. It depends on where you are (in a JSP, in the portlet class, etc.), but, if you have the entity representing the current Group, you can write something like this:
Organization organization = null;
if (group.isOrganization()) {
organization = OrganizationLocalServiceUtil.getOrganization(group.getOrganizationId());
}
Hope it helps. Ask if you need help in order to retrieve the Group object...