将属性从会话复制到请求范围

发布于 2024-12-26 14:31:05 字数 1592 浏览 0 评论 0原文

我想将名为 idNumbers 的 int[] 从会话范围复制到请求范围。 下面的代码不起作用,因为每当尝试从请求范围检索属性时,我的控制器中都会出现空指针异常。我觉得我要么不理解范围,要么以错误的方式设置属性。

jsp代码

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

控制器代码,在这里我尝试了不同的方法,但本质上结果告诉我们这样的参数在请求范围内不存在,而它应该存在。

前两个给出 nulls(empty),然后给出空指针异常:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2025-01-02 14:31:05

您正在当前请求中设置请求属性,而不是在表单提交时设置的请求。

使用填充会话值的隐藏表单字段,或者直接从会话中提取它——如果您已经拥有它,我认为没有令人信服的理由将其复制到其他地方。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文