Aubergine
Aubergine

Reputation: 6042

copy attribute from session to request scope

I want to copy int[] named idNumbers from session scope to request scope. The code below doesnt work as I get null pointer exception in my controller whenever trying to retrieve attribute from request scope. I am feeling I am not either understanding the scopes or I either set the property in wrong way.

the jsp code

<c:if test="${sessionScope.idNumbers!=null}"> // this line is OK and session scope contains the idNumbers(checked)
  <% request.setAttribute("activityId", session.getAttribute("idNumbers")); %>
  <c:set var="activityId" scope="request" value="${sessionScope.idNumbers}"/>
  <form class="spanFormat" method="post" action="/blablabla">
    <p>
      <input value="Attach" type="submit" style="color: green;" />
      <input name="programId" style="display: none;" value="${blabla.programId}" />
    </p>
  </form>
</c:if>

the controller code, here I tried different approaches, but essentially the result tells that such parameter doesn't exist in request scope when it should.

the first two give nulls(empty) and then null pointer exception:

@RequestMapping(value = "/program/set", method = RequestMethod.POST)
public String setActivitiesForProgram(@RequestParam("activityId") int[] activitiesNumbers,
        Model model, HttpSession hs, HttpServletRequest hr)
        throws ServletRequestBindingException {
    String activityNumber1 = (String) hr.getParameter("activityId");
    logger.info(activityNumber1);

    String activityNumber = (String) hr.getAttribute("activityId");
    logger.info(activityNumber);

    String[] activitiesNumbersss = (String[])    hr.getAttribute("activityId");
    logger.info(activitiesNumbersss[0]);

    String[] activitiesNumberss = hr.getParameterValues("activityId");
    logger.info(activitiesNumberss[0]);

    return "someView";
 }

Upvotes: 0

Views: 1360

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

You're setting the request attribute in the current request--not the new one made on form submission.

Use a hidden form field filled with the session value, or just pull it from the session directly--I don't see a compelling reason to copy it somewhere else if you already have it.

Upvotes: 1

Related Questions