修改表单。将id作为隐藏值传递

时间:2014-11-01 11:37:13

标签: java spring hibernate jsp spring-mvc

应用程序:Spring MVC,Hibernate,JSP

查询:在修改表单时,我传递预先填充的RECIPE对象,但id列返回为NULL。

食谱类只有2列 - ID和NAME;

@RequestMapping(value = "/modify/{id}")
public String modifyRecipe(@PathVariable int id, Model model) {
    Recipe recipe = recipeService.findRecipe(id);
    model.addAttribute("recipe", recipe);

    return "recipes/modify";
}

@RequestMapping(value = "modify", method = RequestMethod.POST)
public String updateRecipe(@Valid Recipe recip, Model model) {
    logger.info("From JSP [" + recipe + "]");

    if (result.hasErrors()) {
        return "recipes/modify";
    }
    recipeServices.updateRecipe(recipe);
    return "redirect:/recipe/" + recipe.getId();
}

JSP页面

<body>
    <s:url value="/recipe/modify" var="recipe_modify" />
    <h2>
        Modify
        <c:out value="${fn:toUpperCase(recipe.name)}" />
    </h2>
    <sf:form modelAttribute="recipe" method="POST"
        action="${recipe_modify}">
        <fieldset>
            <table>
                <tr>
                    <th><sf:label path="name">Name of Recipe</sf:label></th>
                </tr>
                <tr>
                    <td><sf:input path="name" size="30" /> <br /> <sf:errors
                            path="name" cssClass="error"></sf:errors><br /></td>
                </tr>

提交JSP后,传递给Spring INFO 2014-11-01 22:21:32,342 com.recipe.mvc.RecipeController - 来自JSP [Recipe [id = null,name = sandwich]

为什么id没有通过这里?

1 个答案:

答案 0 :(得分:0)