You always see Select Box (Drop down) in Biz Web Apps. Sometimes stand alone, and sometimes in a list.
And Select box usually consists of three values.
-
(A) Code that contains a key behind the screen
(B) Displaying value
(C) Selected value
(A) and (B) is sometimes together. And you always need (C) in order to the select box stays selected when the page is refreshed.
For example, if you submit a form, without doing anything, the select box will be empty and selected value will be GONE!!!! It will happen every time you refresh a page.
<form:select path="rtrnTpCd" id="tpCd" onchange="onChangeTp();" style="height:25px; width:200px"> <form:option value="" label="--- Select Type ---"/> <c:forEach items="${TP_LIST}" var="name"> <c:choose> <c:when test="${name.getRtrnTpCd()== TP_SELECTED}"> <form:option value="${name.getRtrnTpCd()}" selected="true">${name.getRtrnTpNm()}</form:option> </c:when> <c:otherwise> <form:option value="${name.getRtrnTpCd()}">${name.getRtrnTpNm()}</form:option> </c:otherwise> </c:choose> </c:forEach> </form:select>
@RequestMapping(value = "/onChangeTpCdRsnMain01") public @ResponseBody List<RtrnRsnModel> refreshRsn(@RequestParam(value = "tpCd") String rtrnTpCd, Model model, HttpServletRequest req) { List<RtrnRsnModel> hdrStsList = RsnBL.getRsnsBoundTp(rtrnTpCd); model.addAttribute("rtrnTpCd", rtrnTpCd); req.getSession().setAttribute(CONST.TP_SELECTED.getVal(), rtrnTpCd); return hdrStsList; }
“TP_SELECTED” is being set in the method “refreshRsn” in the Controller class, and the select box is being selected and stays with the value
<c:forEach items="${rmaLineListModel.rmaLineModelList}" var="rmaLnObj" varStatus="i" begin="0"> . . . <c:choose> <c:when test="${rmaLnObj.getRmaLineStsCd()== ''}"> <form:select path="rmaLineModelList[${i.index}].rmaLineStsCd" id="idLnStCd${i.index}" style="height:25px; width:130px"> <form:option value="" label=""/> <form:options items="${HDR_STS_LIST}" itemValue="rmaHdrStsCd" itemLabel="rmaHdrStsNm"/> </form:select> </c:when> <c:otherwise> <form:select path="rmaLineModelList[${i.index}].rmaLineStsCd" items="${HDR_STS_LIST}" id="idLnStCd${i.index}" itemLabel="rmaHdrStsNm" itemValue="rmaHdrStsCd" delimiter=" " readonly="true" disabled="true" style="height:25px; width:130px"/> <form:hidden path="rmaLineModelList[${i.index}].rmaLineStsCd" id="idLnStCd${i.index}" /> </c:otherwise> </c:choose> </c:forEach>
At line 7 to 13, selected value is NOT specified, whereas line 16 to 25 specified.
@RequestMapping("/cUrlValSubLine01Jsp/{rmaNum}") public ModelAndView goToSubLine01Jsp(@PathVariable("rmaNum") String rmaNum, Model model, HttpServletRequest req) { List<RmaLineModel> rmaLineModelList = RmaDtlBL.getRmaLineListWithCdTblNm(rmaNum, null); return getMVSubLine01(rmaNum, rmaLineModelList, req); }
The controller that create the list of select box.
private ModelAndView getMVSubLine01(String rmaNum, List<RmaLineModel> rmaLinsList, HttpServletRequest req) { RmaLineListModel newModel = new RmaLineListModel(rmaNum, rmaLinsList); ModelAndView modelAndView = new ModelAndView(VIEW.SUB_LINE_01.getVal()); modelAndView.addObject(CONST.FORM_KEY.getVal(), newModel); // For item detail list modelAndView.addObject(CONST.LINE_LIST_MODEL.getVal(), newModel); // Setup Status drop down line the item list modelAndView.addObject(CONST.HDR_STS_LIST.getVal(), getList(CONST.HDR_STS_LIST.getVal())); // Item list for AJAX Auto complete req.getSession().setAttribute(CONST.MDSE_LIST.getVal(), getList(CONST.MDSE_LIST.getVal())); return modelAndView; }
The line 10 specify the select box object.