Neither BindingResult nor plain target object for bean name when running application

55 views Asked by At

index.jsp

<f:form action="emp_change.htm" method="POST" commandName="index">
                <div id="login_box">



                    <div id="form_val">
                        <div class="label">Username:</div>
                        <div class="control"><f:input path="emailId"  /></div>
                        <div style="clear:both;height:0px;"></div>
                        <div class="label">Password:</div>
                        <div class="control"><f:input path="password" type="password" /></div>
                        <div style="clear:both;height:0px;"></div>
                        <div id="msgbox"></div>
                        <div id="login_foot">
                            <input type="submit" name="action" id="login" value="Login" class="send_button" />
                        </div>                    
                    </div>
                </div>
            </f:form>

AdminInfoControl.java

@Controller
public class AdminInfoControl {

        private AdminInfoService service;

    @RequestMapping(value = "/emp_change", method = RequestMethod.POST)
    public String doActions(@ModelAttribute JtAdminInfo emp, BindingResult result, @RequestParam String action, Map<String, Object> map) {
        service = new AdminInfoService();
        JtAdminInfo empResult = new JtAdminInfo();
        switch (action.toLowerCase()) {

            case "login":
                JtAdminInfo search = service.getFindAdmin(emp.getEmailId());

                empResult = search != null ? search : new JtAdminInfo();
                break;
        }
        map.put("index", empResult);
        map.put("empList", service.getAll());
        return "index";
    }
}

I'm getting following error :

Neither BindingResult nor plain target object for bean name 'emp_change' available as request attribute

Anyone please help me to correct this. Please let me know, if information is not sufficient.

2

There are 2 answers

0
Ravi On BEST ANSWER

I have added following method in my controller and works fine.

    @RequestMapping("/index")
    public String setupForm(Map<String, Object> map) {
        map.put("index", new JtAdminInfo());
        return "index";
    }
3
Branislav Lazic On

Just answered on almost the same question.. You are obtaining emp_change as a model attribute in doActions method but you didn't set it as a model attribute previously. So, set your emp_change as a model attribute in some method that e.g. shows your index.jsp page. Something like this:

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView showIndex() {
    ModelAndView mav = new ModelAndView("index");
    mav.addObject("emp_change", new JtAdminInfo ());
    return mav;
}