sendRedirect in custom action class - liferay

I am overriding one of the actions of the calendar portlet . I want to do some functionality then redirect to another JSP after that. I tried using the sendRedirect function as follows:
1.
String redirect = ParamUtil.getString(actionRequest , "redirect");
actionResponse.sendRedirect(redirect);
2.
actionResponse.sendRedirect ("/calendar/view") ;
3.
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
PortletURL url = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest), "8", themeDisplay.getPlid(),
PortletRequest.RENDER_PHASE);
url.setParameter("struts_action", "/calendar/view");
actionResponse.sendRedirect (url.toString()) ;
However, none of the methods cause the jsp to redirect to another one.
I am doing so in the processAction function and I am extending the BaseStrutsPortletAction.
Any suggestions how to solve it?
This is my overridden file :
public class TestAction extends BaseStrutsPortletAction {
public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig,
ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
String cmdConstants = ParamUtil.getString(actionRequest, Constants.CMD);
if (cmdConstants.equals(Constants.APPROVE)) {
//some function
actionResponse.setRenderParameter("test", "test");
originalStrutsPortletAction.processAction(portletConfig, actionRequest, actionResponse);
}
}
public String render(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, RenderRequest renderRequest,
RenderResponse renderResponse) throws Exception {
String cmdConstants = ParamUtil.getString(renderRequest, Constants.CMD);
if (cmdConstants.equals(Constants.APPROVE)) {
if (renderRequest.getParameter("test").equals("test")) {
return "/portlet/calendar/view.jsp";
}
}
return originalStrutsPortletAction.render(null, portletConfig, renderRequest, renderResponse);
}
}

Related

Redirect to the same page after performing different operations in Liferay?

I have created a portlet in which i am doing CRUD Operations (User & Organization). But whenever I Add, Edit or Delete an Organization. I am redirect to the add user page after the operation. How can I stay on the same page after every operation ?
I tried using request dispatcher method and LastPath but couldn't make then work.
Now, I am using send redirect method which is working but whenever I signout and again signin this doesn't work (maybe because of Instance).
So how can i make this work properly please help.
Last Path Method Not Working.
HttpSession httpSession = httpServletRequest.getSession();
User user = UserLocalServiceUtil.fetchUser(UserId);
LastPath last_path = new LastPath("http://localhost:8080/web/my-site/one?p_p_id=my_registration_form_MyRegistrationFormPortlet_INSTANCE_HQMU9wIdWhH5&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&_my_registration_form_MyRegistrationFormPortlet_INSTANCE_HQMU9wIdWhH5_mvcPath=%2FaddOrganization.jsp"," ");
httpSession.setAttribute(WebKeys.LAST_PATH, last_path);
Working but have to set again after Signing out.
actionResponse.sendRedirect("http://localhost:8080/web/my-site/one?p_p_id=my_registration_form_MyRegistrationFormPortlet_INSTANCE_HQMU9wIdWhH5&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&_my_registration_form_MyRegistrationFormPortlet_INSTANCE_HQMU9wIdWhH5_mvcPath=%2FaddOrganization.jsp");
EDIT:
// For Adding Organizations
#ProcessAction(name = "addOrganization")
public void addOrganization(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException
{
long UserId = themeDisplay.getUserId();
String organizationName = ParamUtil.getString(actionRequest, "organizationName");
String country = ParamUtil.getString(actionRequest, "country");
String type = "organization";
long countryId = ParamUtil.getLong(actionRequest, "countryId");
long statusId = ParamUtil.getLong(actionRequest, "statusId");
try
{
Organization organization = OrganizationLocalServiceUtil.addOrganization(UserId, 0, organizationName, type, 0,countryId, 12017, null, false, null);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
In my view.jsp
<!-- For Displaying Add Organization Page -->
<portlet:renderURL var = "addOrganizations">
<portlet:param name = "mvcPath" value = "/addOrganization.jsp"/>
</portlet:renderURL>
In my addOrganization.jsp
<!-- For Displaying Add Organization Page -->
<portlet:renderURL var = "addOrganizations">
<portlet:param name = "mvcPath" value = "/addOrganization.jsp"/>
</portlet:renderURL>
<!-- For redirecting to the addOrganization method in MyRegistrationFormPortlet -->
<portlet:actionURL var = "addOrganization_actionURL">
<portlet:param name = "javax.portlet.action" value = "addOrganization"/>
</portlet:actionURL>
<form>
// my form
</form>
Ok I did quick checks and you can add the mvcPath as you do it for rendering directly in the actions.
#ProcessAction(name = "addOrganization")
public void addOrganization(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
long UserId = themeDisplay.getUserId();
String organizationName = ParamUtil.getString(actionRequest, "organizationName");
String country = ParamUtil.getString(actionRequest, "country");
String type = "organization";
long countryId = ParamUtil.getLong(actionRequest, "countryId");
long statusId = ParamUtil.getLong(actionRequest, "statusId");
try {
Organization organization = OrganizationLocalServiceUtil.addOrganization(UserId, 0, organizationName, type,
0, countryId, 12017, null, false, null);
} catch (Exception ex) {
ex.printStackTrace();
}
actionResponse.getRenderParameters().setValue("mvcPath", "/addOrganization.jsp");
}
Are you using MVCActionCommands for this? Example for adding a user.
You can specify a mvcRenderCommandName parameter which will redirect to a render command. In the render command you can return a String with your jsp to call.
#Component(immediate = true, property = { "javax.portlet.name=" + PortletKeys.Test,
"mvc.command.name=/addUser" }, service = MVCActionCommand.class)
public class AddUserActionCommand implements MVCActionCommand {
#Override
public boolean processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException {
//adding user to db logic
actionResponse.setRenderParameter("mvcRenderCommandName", "/users");
return false;
}
}
RenderCommand
#Component(immediate = true, property = { "javax.portlet.name=" + PortletKeys.Test,
"mvc.command.name=/users" }, service = MVCRenderCommand.class)
public class UserRenderCommand implements MVCRenderCommand {
#Override
public String render(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException {
//doing some stuff
return "/users/view.jsp";
}
}
Just be sure that the annotation "mvc.command.name" matches the render parameter which you set in "mvcRenderCommandName".
As I wrote in my first answer I would recommend to split the actions in own classes. If you try to put everything into the Portlet class you can try to include a jsp with the include() method. This is how we did that in 6.2 but I see include() is still available in 7.x.
// For Adding Organizations
#ProcessAction(name = "addOrganization")
public void addOrganization(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException
{
long UserId = themeDisplay.getUserId();
String organizationName = ParamUtil.getString(actionRequest, "organizationName");
String country = ParamUtil.getString(actionRequest, "country");
String type = "organization";
long countryId = ParamUtil.getLong(actionRequest, "countryId");
long statusId = ParamUtil.getLong(actionRequest, "statusId");
try
{
Organization organization = OrganizationLocalServiceUtil.addOrganization(UserId, 0, organizationName, type, 0,countryId, 12017, null, false, null);
}
catch (Exception ex)
{
ex.printStackTrace();
}
include('/html/myJsp.jsp',actionRequest,actionResponse);
}
Check:
https://docs.liferay.com/ce/portal/7.2-latest/javadocs/portal-kernel/com/liferay/portal/kernel/portlet/bridges/mvc/MVCPortlet.html#include-java.lang.String-javax.portlet.ActionRequest-javax.portlet.ActionResponse-

How to get default page url in liferay?

I set default page URL in portal-ext.properties.
default.landing.page.path=/group/guest/home
Then create portlet with Controller :
public class UserPortrait extends MVCPortlet {
#Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse) {
}
#Override
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
super.processAction(actionRequest, actionResponse);
}
}
Now , how to get landing page URL in processAction method.
Simply, use com.liferay.portal.kernel.util.PropsUtil.get(String propertyName) to get any property from portal-ext.properties file.

Portlet Using Other Portlet

I am having two portlets with following action methods
A-Portlet
public void actionMethodA(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
System.out.println("Portlet A");
}
B-Portlet
public void actionMethodB(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
System.out.println("Portlet B");
}
Can we call actionMethodB in B-Portlet ?
Use PortletClassInvoker class to call a plugin method from another plugin.
Example:
try {
methodKey = new MethodKey(
ClassResolverUtil.resolveByPortletClassLoader(
"com.acme.SyncFactoryRegistry",
"custom-portlet"), "register", String.class,
Object.class);
}
catch (RuntimeException re) {
return;
}
PortletClassInvoker.invoke(
false, "1_WAR_customportlet",
_methodKey , arg1, arg2);

Get keyword of the search with struts Liferay

I want get the keywords of the search with the portlet of Search. I saw that in the url appear: "struts_action=/search/seach" then I looked for in strut-config.xml and find:
action path="/search/search" forward="portlet.search.search"
I am trying to do a strut but I don't know very much about struts and it doesn't work. This is the code of 'liferay-hook.xml':
<portal-properties>portal.properties</portal-properties>
<struts-action>
<struts-action-path>/search/search</struts-action-path>
<struts-action-impl>com.segmentationProject.searchAction.struts.SearchAction</struts-action-impl>
</struts-action>
portal.properties:
auth.public.paths=/search/search
SearchAction.java:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.struts.BaseStrutsAction;
public class SearchAction extends BaseStrutsPortletAction {
#Override
public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
System.out.println("inside the process ");
super.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
}
#Override
public String render(StrutsPortletAction originalStrutsPortletAction,PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {
// TODO Auto-generated method stub
System.out.println("inside the render");
return super.render(portletConfig, renderRequest, renderResponse);
}
}
Any idea about I missing or do wrong? I only want get the keywords and then do the search normally.
Thanks!
I think that this one will do the job :
String keywords = ParamUtil.getString(actionRequest, "keywords");
ParamUtil use the portlet namespace to retrieve parameters.
I think you must use originalStrutsPortletAction instead of super.
super.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
}
just replace super by originalStrutsPortletAction
originalStrutsPortletAction .processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
}

Liferay Hook calling extension method

I have new action in a liferay hook which ideally will be calling methods created in an extension. But at run time when executing the action, it throws the exception
java.lang.ClassNotFoundException
for the methods created in the extension.
Has anybody created similar action in liferay hook? if so, what was the solution for this problem if encountered?
Here is my code:
public class ExampleStrutsAction extends BaseStrutsAction {
public String execute( HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = ParamUtil.get(request, "name", "World");
ThemeDisplay themeDisplay= (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
BSCDynamicDataListLocalServiceUtil.cloneDynamicDataListPageInSuborganization(the‌​meDisplay, name);
return "/portal/sample.jsp";
}
}
Try below code,
hook.xml
<hook>
<portal-properties>portal.properties</portal-properties>
<language-properties>
content/Language.properties
</language-properties>
<custom-jsp-dir>/META-INF/custom_jsps</custom-jsp-dir>
<struts-action>
<struts-action-path>/my_account/edit_user</struts-action-path>
<struts-action-impl>com.test.hook.action.EditUserAction</struts-action-impl>
</struts-action>
<struts-action>
<struts-action-path>/users_admin/edit_user</struts-action-path>
<struts-action-impl>com.test.hook.action.EditUserAction</struts-action-impl>
</struts-action>
</hook>
EditUserAction class
public class EditUserAction extends BaseStrutsPortletAction
{
#Override
public void processAction(final StrutsPortletAction originalStrutsPortletAction, final PortletConfig portletConfig,
final ActionRequest actionRequest, final ActionResponse actionResponse) throws Exception
{
// add your custom code
originalStrutsPortletAction.processAction(portletConfig, actionRequest, actionResponse);
}
#Override
public String render(final StrutsPortletAction originalStrutsPortletAction, final PortletConfig portletConfig, final RenderRequest renderRequest,
final RenderResponse renderResponse) throws Exception
{
//add your custom code
return originalStrutsPortletAction.render(portletConfig, renderRequest, renderResponse);
}
}
I have created above hook for EditUserAction in Control Panel
Hope it helps you!!

Resources