我想從會話範圍中複製int []命名idNumbers以請求範圍。 下面的代碼不工作,因爲我試圖從請求範圍中檢索屬性時,在我的控制器中收到空指針異常。我覺得我既不是在理解範圍,也不是以錯誤的方式設置財產。從會話複製屬性到請求範圍
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>
控制器代碼,在這裏,我嘗試了不同的方法,但本質上是結果指出這樣的參數並沒有在要求範圍時,它應該存在。
前兩個給空值(空),然後空指針異常:
@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";
}
爲什麼要將更廣泛的範圍內的某些內容複製到更窄的範圍? – 2012-01-12 15:06:07
我忘了爲控制器添加@RequestParam(「activityId」)int [] activitiesNumbers。我想重複使用這種方法(因爲有多個頁面由此提供),而無需向方法本身添加額外的代碼。如果我有@RequestParam註釋,則參數應該作爲強制參數提供。如果我不提供它,那麼我會得到不良的Http請求。 – Aubergine 2012-01-12 15:12:49