Spring MVC @ModelAttribute来自表单的零ID

时间:2015-03-05 19:15:24

标签: java html spring-mvc

我将现有的User对象作为@ModelAttribute加载到Spring Form中(并且它正确显示相应的ID和Name),但是当我修改Name并尝试在提交时读取它时,我在processEditSubmit中得到零ID(名称已正确更改。)

User.java

@Entity
@Table(name = "\"user\"")
public class User {

    @Id
    // @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private int id;

    @Column
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

EditUser.jsp

<table>
    <form:form modelAttribute="userCmd" action="update" method="post" >
        <tr><td>ID:</td><td><form:input disabled="true" path="id" /></td><td><form:errors path="id" cssClass="error"/></td></tr>
        <tr><td>Name:</td><td><form:input path="name"/></td><td><form:errors path="name" cssClass="error"/></td></tr>
        <tr><td colspan="3"><input type="submit" value="Save"/></td></tr>
    </form:form>
</table>

ManageUsers.java

@Controller
@RequestMapping("/users")
public class ManageUsers {

    @Autowired
    private UserValidator userValidator;

    @Autowired
    private UserManager userManager;

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        // binder.setValidator(userValidator);
        // binder.registerCustomEditor(User.class, new UserEditor());
    }

    @RequestMapping(method = RequestMethod.GET)
    public String viewUsers(ModelMap model) {
        UserList ul = new UserList();
        List<User> users = userManager.getUsers();
        ul.setUserList(users);
        model.addAttribute("userListCmd", ul);
        return "manageUsers";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editUser(@RequestParam int id, ModelMap model) {
        model.addAttribute("userCmd", userManager.getUserById(id));
        return "editUser";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addUser(ModelMap model) {
        model.addAttribute("userCmd", new User());
        return "addUser";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String processAddSubmit(@ModelAttribute("userCmd") User user,
            BindingResult result, SessionStatus status,
            HttpServletResponse response) throws IOException {

        userValidator.validate(user, result);

        if (result.hasErrors())
            return "addUser";

        userManager.insertUser(user);
        status.setComplete();

        return "redirect:/users";

    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String processEditSubmit(@ModelAttribute("userCmd") User user,
            BindingResult result, SessionStatus status,
            HttpServletResponse response) throws IOException {

        userValidator.validate(user, result);

        if (result.hasErrors())
            return "editUser";

        // user contains zero ID and operation fails with org.hibernate.StaleStateException: 
        // Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
        userManager.updateUser(user);

        status.setComplete();

        return "redirect:/users";

    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public String processDelete(@PathVariable int id,
            HttpServletResponse response) throws IOException {

        userManager.deleteUser(id);

        return "redirect:/users";

    }

}

1 个答案:

答案 0 :(得分:0)

在你的jsp中,你提到disabled = true。执行此操作时,浏览器不会将此字段值发送回服务器(您可以通过Chrome Dev工具或FF上的Firebug插件验证)。所以在你的控制器中,id字段值为零,因为它是一个整数基元类型,所有实例基元都被初始化为默认值。

为什么不使用readOnly = true?