1
votes

Here i am submitting the form using submit action. In Controller the model object is showing null value. Suppose i am using request.getParameter("mcodeName") - then i am getting value. Whey value is null when i using getMcode() method. Please help me to resolve this issue.

@RequestMapping(value="/masterCode.do", method=RequestMethod.POST)
    public ModelAndView addMasterCode(HttpServletRequest req, 
            HttpServletResponse res, @ModelAttribute("masterCodeBean") MasterCodeBean mCodeBeanObj) { 
        log.info("");
        String mCodeName    = mCodeBeanObj.getMaster_code_name(); //getting null
        String mCodeStatus  = mCodeBeanObj.isActive(); //getting null
        System.out.println("Req code des " + req.getParameter("mCodeName"));
        System.out.println("Req active : " + req.getParameter("active"));
        ModelAndView model = null;
        try {
            model = new ModelAndView("masterCode");
            codeDelegate.addMasterCodeDetails(mCodeBeanObj);
            model.addObject("masterCodeBean", mCodeBeanObj);
        }catch(Exception e) {
            System.out.println(EXCEP_ERR);
        }
        return model;
    }

my form bind model attribute like

1
<pre> <form:form id="mCodeForm" method="post" action="masterCode.do" modelAttribute="masterCodeBean"> Enter Product Name : <form:input id="mCodeName" name="mCodeName" path="" /> <br></div> Select Status : <form:select id="active" name="active" path=""> <form:option value="0" label="Active" /> <form:option value="1" label="De-Active" /> </form:select> <span> <input type="submit" value="Save" id="save_mstCdeMgr"/> </span> </form:form> </pre>saravanan

1 Answers

2
votes

Sorry, I don't have enough reputations to comment on. So I am writing the answer here.

<form:form id="mCodeForm" method="post" action="masterCode.do" modelAttribute="masterCodeBean"> 
Enter Product Name : <form:input id="mCodeName" path="mCodeName" /> <br>
Select Status : <form:select id="active" path="isActive"> 
<form:option value="0" label="Active" /> 
<form:option value="1" label="De-Active" /> 
</form:select> 
<span> <input type="submit" value="Save" id="save_mstCdeMgr"/> </span> </form:form>
  1. The setters and getters should follow some code conventions. Suppose if you have mCodeName as class variable then getMCodeName() and setMCodeName(String mCodeName).

  2. As you used form tag, the path is nothing but the modelAttribute field name. No need of writing id and name attributes as it generates them at run-time. The id is needed for some client-side validation or javascript related purposes.

    <form:input id="mCodeName" path="mCodeName" /> is converted into the html code at run-time as <input type="text" id="mCodeName" name="mCodeName" />

Try this and let me know.