速度模板中#springFormSingleSelect的工作无效

时间:2012-08-26 00:29:28

标签: spring model-view-controller spring-mvc velocity

我正在制作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";
    }

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

1 个答案:

答案 0 :(得分:0)

错误时

departamentsMap为空...所以请添加model.addObject(“departamentsMap”,departamentsMap); 在你的帖子细节中,所以当ext Occured时,它不能为空。

//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";
        }