Search code examples
liferayliferay-7

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>


Solution

  • 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");
        }