SpringMVC提交表单行为

时间:2014-06-06 15:10:54

标签: forms spring jsp spring-mvc

我是SpringMVC的新手,我需要你的帮助来解决这个POST请求行为。

这里是表格(型号):

    public class ReferenteForm {

       private Integer id;
       private String descrizione;

       .. getter/setter

    }

控制器:

    @RequestMapping("/referente")
    public class ReferenteController {

        @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
        public String setupUpdate(@PathVariable("id") Integer id, Model uiModel) {

            logger.info("Setup ..");

            // get data
            Referente referente = referenteService.getById(id);

            // assign to form 
            ReferenteForm referenteForm = toReferenteForm(referente);

            //
            uiModel.addAttribute("referenteForm", referenteForm);

            return "referente/edit";
        }    


        @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
        public String update(@Valid ReferenteForm refForm,
                            BindingResult bindingResult, 
                            Model uiModel,
                            HttpServletRequest httpServletRequest,
                            RedirectAttributes redirectAttributes, 
                            Locale locale) {

            logger.info("id.............."+refForm.getId());
            logger.info("descrizione....."+refForm.getDescrizione());

        }    

    }

此处的视图对应于"参考/编辑":

        <form:form modelAttribute="referenteForm" method="post">

            <table>
                <tr>
                    <td>
                        Descrizione:
                    </td>
                    <td>
                        <form:input path="descrizione" />
                    </td>
                </tr>

            <button type="submit">
                <span>Save</span>
            </button> 
            <button type="reset">
                <span>Reset</span>
            </button>           

        </form:form>   

我开始通过GET请求触发setupUpdate()方法,例如: / MyApp的/ referente /更新/ 30 该方法从底层服务(未示出)获取数据构建一个简单的ReferenteForm对象,将其设置为edit.jsp(referente / edit)查看的模型。 然后,我可以看到由edit.jsp显示的id = 30的参考数据并准备编辑。 我提交表单(POST请求),因此将触发update()方法。 我很惊讶看到ID,它来自哪里? 谢谢!

0 个答案:

没有答案