将属性从会话复制到请求范围
我想将名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在当前请求中设置请求属性,而不是在表单提交时设置的新请求。
使用填充会话值的隐藏表单字段,或者直接从会话中提取它——如果您已经拥有它,我认为没有令人信服的理由将其复制到其他地方。
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.