【问题标题】:Spring MVC @ModelAttribute zero ID from formSpring MVC @ModelAttribute 来自表单的零 ID
【发布时间】:2015-05-07 07:03:58
【问题描述】:

我将现有的 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";

    }

}

【问题讨论】:

    标签: java html spring-mvc


    【解决方案1】:

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

    为什么不改用 readOnly=true?

    【讨论】:

      猜你喜欢
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2012-10-25
      • 2014-09-20
      • 1970-01-01
      相关资源
      最近更新 更多