Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Jonathan Mattox
My Sites Portlet
May 29, 2014 1:15 PM

Jonathan Mattox

Rank: New Member

Posts: 13

Join Date: November 5, 2013

Recent Posts

Hello, I am developing a hook for the "My Sites" portlet. My question is regarding how to edit the functionality of what happens when the clicks the "Save" button after requesting access to a site and filling in a comment. What I'm trying to accomplish is having an email go out after the user fills in a comment and clicks the "Save" button. I have been able to do this as soon as the user clicks the "Request Membership" hyperlink, but I also want to capture the user's comment and sent it in the email. Plus, if the user clicked the "Cancel" button instead of "Save" I do not want the email to go out. For these two reasons I need the email to go out after the "Save" button is clicked.

I'm having a hard time understanding how to handle this action. I am working in the following customized jsp in my hook: html/portlet/sites_admin/post_membership_request.jsp

I checked the liferay-portlet.xml file for any relevant information and here is the section for the "My Sites" portlet:


Then I looked at the struts-config.xml file for help. Here is the relevant section for the My Sites Portlet from the struts-config.xml file which can be found on github at the following link:

<!-- My Sites -->

<action path="/my_sites/edit_layouts" type="com.liferay.portlet.myplaces.action.EditPagesAction" />

<action path="/my_sites/view" type="com.liferay.portlet.myplaces.action.ViewAction">
<forward name="portlet.my_sites.view" path="portlet.my_sites.view" />

I then went and looked at the com.liferay.portlet.myplaces.action.ViewAction class file to see if I could determine what code is being executed when the "Save" button is clicked. Here is the link to that file:

If someone could please explain to me how this kind of thing works I would be greatly appreciative. Here is the code of the html/portlet/sites_admin/post_membership_request.jsp that I have been working with and the email code I added highlighted in bold.

* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
* The contents of this file are subject to the terms of the Liferay Enterprise
* Subscription License ("License"). You may not use this file except in
* compliance with the License. You can obtain a copy of the License by
* contacting Liferay, Inc. See the License for the specific language governing
* permissions and limitations under the License, including but not limited to
* distribution rights of the Software.

<%@ include file="/html/portlet/sites_admin/init.jsp" %>

<%@ page import="*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ page import="com.liferay.mail.service.MailServiceUtil"%>
<%@ page import="com.liferay.portal.kernel.mail.MailMessage"%>
String redirect = ParamUtil.getString(request, "redirect");


Group group = (Group)request.getAttribute(WebKeys.GROUP);

long groupId = BeanParamUtil.getLong(group, request, "groupId");

MembershipRequest membershipRequest = (MembershipRequest)request.getAttribute(WebKeys.MEMBERSHIP_REQUEST);

InternetAddress from = new InternetAddress("", "Admin admin");
InternetAddress to = new InternetAddress("", "Jonathan");

MailMessage mailMessage = new MailMessage();


mailMessage.setBody("email body text");
mailMessage.setSubject("email subject text");




<portlet:actionURL var="postMembershipRequestURL">
<portlet:param name="struts_action" value="/sites_admin/post_membership_request" />

<aui:form action="<%= postMembershipRequestURL %>" method="post" name="fm">
<aui:input name="<%= Constants.CMD %>" type="hidden" />
<aui:input name="redirect" type="hidden" value="<%= redirect %>" />
<aui:input name="groupId" type="hidden" value="<%= groupId %>" />

backURL="<%= redirect %>"
escapeXml="<%= false %>"
localizeTitle="<%= false %>"
title='<%= LanguageUtil.format(pageContext, "request-membership-for-x", HtmlUtil.escape(group.getDescriptiveName(locale))) %>'

<liferay-ui:error exception="<%= MembershipRequestCommentsException.class %>" message="please-enter-valid-comments" />

<aui:model-context bean="<%= membershipRequest %>" model="<%= MembershipRequest.class %>" />

<c:if test="<%= Validator.isNotNull(group.getDescription()) %>">
<aui:field-wrapper label="description">
<%= HtmlUtil.escape(group.getDescription()) %>

<aui:input name="comments" />

<aui:button type="submit" />

<aui:button href="<%= redirect %>" type="cancel" />

<c:if test="<%= windowState.equals(WindowState.MAXIMIZED) %>">
Liferay.Util.focusFormField(document.<portlet:namespace />fm.<portlet:namespace />comments);
Andrew Jardine
RE: My Sites Portlet
May 29, 2014 3:02 PM

Andrew Jardine

Community Moderator

Rank: Liferay Legend

Posts: 1699

Join Date: December 22, 2010

Recent Posts

Hey Jonathan,

I think what you are looking for is a struts portlet action. Since you want to maintain the existing functionality (unless I misunderstood) and simply augment it with "more stuff" you could try thing.

Step #1: Create a class --

2public class CustomEditPagesAction extends BaseStrutsPortletAction

Step #2: Override the method you want to augment -- in your case the processAction

2public class CustomEditPagesAction extends BaseStrutsPortletAction
4    public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception
5    {
7        }

Step #3: Make a call to the existing action, and then add your stuff...

 2public class CustomEditPagesAction extends BaseStrutsPortletAction
 4    public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception
 5    {
 6               // execute the core functionality and save the content editing.
 7        originalStrutsPortletAction.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
 9               // .. add your email stuff here
10        }

Step #4: Add the configuration for the hook in your liferay-hook.xml file
2  <struts-action>
3        <struts-action-path>/my_sites/view</struts-action-path>
4        <struts-action-impl></struts-action-impl>
5    </struts-action>

and deploy. Hope this helps.

PS> Forgot to add.. the struts action is stored in a parameter called "cmd". Check the page source to see what the command the save button says is. Probably either "save" or "add" You can use the Command class with it's contants to check for the save and only send the email in that case.

e.g. if ( ParamUtil.getString("cmd").equals(Command.ADD)) { ... do your stuff ... }

Participate in the State of Liferay Community 2017. Help the community and even win some prizes!