SnIpEr ReLoAdEd
SnIpEr ReLoAdEd

Reputation: 72

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>

Upvotes: 1

Views: 988

Answers (3)

ratzownal
ratzownal

Reputation: 294

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

Upvotes: 2

ratzownal
ratzownal

Reputation: 294

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-

Upvotes: 2

ratzownal
ratzownal

Reputation: 294

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".

Upvotes: 2

Related Questions