Search code examples
jspliferayliferay-6portletliferay-theme

how to pass selected userId to action class?


I have created a portlet where I am adding users programatically. I am able to add and view the users now. In order to edit the user I have created <liferay-ui:icon-menu> and added the two options (Edit and Deactivate). Now On clicking or selecting the respective users action buttons (Edit / Deactivate) I need to send the selected user ID to my action class. Once I get the userID in my action class I will call the UserLocalServiceUtil.updateUser() method with updated values. Can anyone please tell me how can I send the selected user id to my action class.

My code is as follows,

View.jsp

<liferay-ui:search-container delta="10" emptyResultsMessage="no-users-were-found">
        <liferay-ui:search-container-results
                results="<%=UserLocalServiceUtil.getUsers(searchContainer.getStart(), searchContainer.getEnd())%>"
        total="<%=UserLocalServiceUtil.getUsersCount()%>" />
        <liferay-ui:search-container-row className="com.liferay.portal.model.User" keyProperty="userId"        modelVar="user">

                <liferay-ui:search-container-column-text name="name" value="<%= user.getFullName() %>"/>
                <liferay-ui:search-container-column-text name="first-name" property="firstName"        />
                <liferay-ui:search-container-column-text name="last-name" property="lastName" />
                <liferay-ui:search-container-column-text name="screen-name"        property="screenName"/>
                <liferay-ui:search-container-column-jsp align="right" path="/html/users/custom_user_actions.jsp" />

        </liferay-ui:search-container-row>
        <liferay-ui:search-iterator />
</liferay-ui:search-container>
<liferay-ui:search-container delta="10" emptyResultsMessage="no-users-were-found" />

custom_user_actions.jsp

<liferay-ui:icon-menu>  
  <portlet:actionURL name="edit" var="edit">
          <portletaram name="action" value="edit" />
  </portlet:actionURL>
  <liferay-ui:icon image="edit" message="Edit" url="<%=edit.toString() %>" />

  <portlet:actionURL name="deactivate" var="deactivate">
          <portletaram name="action" value="deactivate" />
  </portlet:actionURL>
  <liferay-ui:icon image="deactivate" message="deactivate" url="<%= deactivate.toString() %>" />
</liferay-ui:icon-menu>

Action class

public void edit(ActionRequest request, ActionResponse response) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException{
                  System.out.println("<<<< Controller add() method has been called >>>>>>>>>");

          }


        public void deactivate(ActionRequest request, ActionResponse response) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException{
                  System.out.println("<<<< Controller delete() method has been called >>>>>>>>>");

          }

Can any one please suggest me how can I pass the selected row user ID to my action class.

Thanks in advance.


Solution

  • Get the respective userId in in custom_user_actions.jsp and pass it as a parameter to your action class.

    Add the following code in custom_user_actions.jsp

    <%
     ResultRow resultRow = (ResultRow)request.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW);
     User userObj = (User)resultRow.getObject();
     %>
    

    And pas the parameter as follows,

    <portlet:actionURL name="edit" var="edit">
              <portletaram name="selectedId" value="<%String.valueOf(user.getUserId())%>" />
      </portlet:actionURL>
      <liferay-ui:icon image="edit" message="Edit" url="<%=edit.toString() %>" />
    

    Get the parameter in your action class as,

    public void edit(ActionRequest request, ActionResponse response) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException{
    
    String usrID = ParamUtil.getString(request, "selectedId")
                      System.out.println("<<<< Controller add() method has been called >>>>>>>>>");
    
              }
    

    Hope it may help you :)