Unlike updating a part of screen by Ajax, this is a traditional(?), and a basic way: to Submit an entire form.
Summarized as three steps:
- SUBMIT button is pressed, and passes an Forms (Object) to the controller method.
- The Controller method takes the Object and Register (Insert/Update) to the data base
- Return a String as JSP name
@RequestMapping(value = "/cUrlValAttrbMain01Jsp", params = "submitHdr", method = RequestMethod.POST) public String submitHdr(@ModelAttribute("RmaHdrModel") RmaHdrModel rma, HttpServletRequest req) { RmaBL.saveHdr(rma, req); return VIEW.REDIRECT_HOME.getVal(); }
1.1. “cUrlValAttrbMain01Jsp” is defined in main_01.jsp.
<c:url var="addAction" value="/cUrlValAttrbMain01Jsp.html" ></c:url> <form:form action="${addAction}" commandName="rmaMapKey">
1.2. “submitHdr” is “name” attribute in “Submit” button in main_01.jsp.
<input type="submit" value="Submit" name="submitHdr" style="height:25px; width:150px; color: #F6FDA4; background-color: #CC0000; font-face: 'Comic Sans MS';"/>
1.3. @ModelAttribute specifies an argument in the Controller class RmaController.java.
1.4. The parameter “RmaHdrModel rma” takes the form object from JSP.
@RequestMapping(value = "/cUrlValAttrbMain01Jsp", params = "submitHdr", method = RequestMethod.POST) public String submitHdr(@ModelAttribute("RmaHdrModel") RmaHdrModel rma, HttpServletRequest req) { RmaBL.saveHdr(rma, req); return VIEW.REDIRECT_HOME.getVal(); }
2.1. RmaBL.saveHdr does insert/update to the Database
public static RMA_HDR saveHdr(RmaHdrModel rma, HttpServletRequest req) { RMA_HDR obj = new RMA_HDR(rma); if (rma.getId() == 0) { hdrSvc.save(obj); // Set RMA# with formatted DecimalFormat df = new DecimalFormat("000000"); obj.setRmaNum("RMA" + df.format((double) obj.getId())); } hdrSvc.update(obj); req.getSession().removeAttribute(CONST.HDR_LIST.getVal()); return obj; }
3.1. “VIEW.REDIRECT_HOME.getVal()” returns a string “redirect:/ns-home” from a constant class and redirect to the method initialize the screen
enum VIEW { MAIN_01("main_01"), SUB_LINE_01("sub_line_01"), SUB_TP_01("sub_tp_01"), SUB_RSN_01("sub_rsn_01"), SUB_TP_RSN_01("sub_tp_rsn_01"), REDIRECT_HOME("redirect:/ns-home") ; private String code; VIEW(String code) { this.code = code; } public String getVal() { return code; } }