0

我正在制作spring mvc应用程序并使用速度模板进行视图。有表格用于填充数据,在此表格中我使用#springFormSingleSelect.Form在数据有效时工作正常,但当表格由无效数据填充时,如上所述有错误消息,但#springFormSingleSelect 为空。这是我的观点:

<form action="saveEmployee" method="POST">
                #springBind("newEmployee")
            <a href="listEmployees">#springMessage("label.back")</a>
            <p>
            <table>                
                <tr>
                    <td>#springMessage("label.firstName")</td>
                    <td>#springFormInput("newEmployee.firstName" "")</td>
                    <td><font color="red">#springShowErrors("&nbsp" "")</font></td>
                </tr>
                <tr>
                    <td>#springMessage("label.lastName")</td>
                    <td>#springFormInput("newEmployee.lastName" "")</td>
                    <td><font color="red">#springShowErrors("&nbsp" "")</font></td>
                </tr>
                <tr>
                    <td>#springMessage("label.salary")</td>
                    <td>#springFormInput("newEmployee.salary" "")</td>
                    <td><font color="red">#springShowErrors("&nbsp" "")</font></td>
                </tr>
                <tr>
                    <td>#springMessage("label.birthdate")</td>
                    <td>#springFormInput("newEmployee.birthday" "")</td>
                    <td><font color="red">#springShowErrors("&nbsp" "")</font></td>
                </tr>
                <tr>
                    <td>#springMessage("label.departament")</td>
                    <td>#springFormSingleSelect("newEmployee.departamentId" $departamentsMap "")</td>
               </tr>

            </table>
                    <input type="submit" value="#springMessage("label.submit")">
        </form>

这是视图控制器的一部分:

@RequestMapping(value="/saveEmployee", method= RequestMethod.GET)
    public ModelAndView newuserForm(){
        ModelAndView model = new ModelAndView("newEmployee");
        Employee empl = new Employee();

        Map departamentsMap = new TreeMap();
        List<Departament> departamentsList = service.listDepartaments();

        //for singleselect of departaments
        for(int i=0; i<departamentsList.size(); i++){
            Departament dep = departamentsList.get(i);
            departamentsMap.put(dep.getDepartamentId(),dep.getTitle() );
        }                    
        model.addObject("departamentsMap",departamentsMap);

        model.getModelMap().put("newEmployee", empl);
        return model;
    }

    @RequestMapping(value="/saveEmployee", method=RequestMethod.POST)
    public String create(@ModelAttribute("newEmployee")Employee empl, BindingResult result){

        employeeValidator.validate(empl, result);
        if(result.hasErrors()){
            return "newEmployee";
        }
        service.saveEmployee(empl);
        return "redirect:/listEmployees";
    }

也许有人知道这种行为的原因?

4

1 回答 1

0

出错时departamentsMap 为空...所以请添加model.addObject("departamentsMap",departamentsMap); 在您的帖子详细信息中,因此当 eeror 发生时它不能为空。

//Modified this line of code..


if(result.hasErrors()){
        Map departamentsMap = new TreeMap();
        List<Departament> departamentsList = service.listDepartaments();

        //for singleselect of departaments
        for(int i=0; i<departamentsList.size(); i++){
            Departament dep = departamentsList.get(i);
            departamentsMap.put(dep.getDepartamentId(),dep.getTitle() );
        }                    
        model.addObject("departamentsMap",departamentsMap);


   return "newEmployee";
        }
于 2013-04-07T11:56:55.043 回答