javajspportletgatein

Set data into ActionResponse Portlet GateIn


I am working on portlet for GateIn 3.6. Currently i have created a page that list the records in table. When user click on any row then it takes to description page.

Right now when user click on row it loads the details page but values are not available. What i am doing details are here..

    @RenderMode(name = "view")
    public void display(RenderRequest request, RenderResponse response) throws PortletException, IOException, NamingException {

        RecordsDAO recordsDAO = new RecordsDAOImpl();

        // Records listing available on listing page
        request.setAttribute("recordsList", recordsDAO.getAllRecords());

        // tried to load record detail page when user click one row
        if(actionJsp == null || actionJsp.equals("")){
            getPortletContext().getRequestDispatcher("/jsp/ListRecords.jsp").
            include(request, response);
        } else {
            getPortletContext().getRequestDispatcher("/jsp/DetailsBoxRecord.jsp").
            include(request, response);
        }   
        actionJsp = "";
    }

and the process action is

    @ProcessAction(name = "details")
    public void details(ActionRequest request, ActionResponse response) throws PortletException, IOException {

         RecordsDAO recordsDAO = new RecordsDAOImpl();

         int id = Integer.parseInt(request.getParameter("id"));

         RecordsForm recordsForm = recordsDAO.getRecord(id);

         // way 1 set request attribute
         request.setAttribute("details", recordsForm);


         // way 2 set response attribute -- display error that cannot set Form type values
         response.setRenderParameter("details", recordsForm);
         actionJsp = "values";
    }

Option1: I have tried to set RecordsForm type values in request.setAttribute, its done but the values in request are not available in jsp page.

Option2: Using response.setRenderParameter I am not able to set RecordsForm type values in response.setRenderParameter to access these values in jsp page.

Can anyone please guide me which one is correct way in my case and how these values will be available in jsp page so i can load detail page.


Solution

  • This line of code

    response.setRenderParameter("details", recordsForm);
    

    will only pass the recordsForm object to the doView method as parameter. Your JSP is likely trying to use attributes on the RenderRequest. This means in your doView you will need to do something like the following

    RecordsForm recordsForm = request.getParameter("details");
    request.setAttribute("details", recordsForm);
    

    There is also the option to use the portlet container runtime option javax.portlet.actionScopedRequestAttributes. You set this in your portlet.xml and it should take attributes you set in an action method and provide them as attributes in your render method, eliminating the need to move your objects from the parameters map to the attributes map yourself.

    Unrelated note: It looks like you're using actionJsp as an object variable in your portlet. That's not recommended because portlets should be coded to be thread safe. Object variables are shared between users and threads, so saving user specific state data in them will cause errors when you get multiple users.