AJAX POST失去参考模型属性 - 春属性、模型、AJAX、POST

2023-09-11 22:35:04 作者:那班、狠势力

我提交来自通过AJAX的文章,但我失去了参照它打破了应用程序,我@ModelAttribute值。这是我的请求(使用合金UI,但要求很一般):

I'm submitting a from via an AJAX POST but am losing reference to my @ModelAttribute values which breaks the app. Here's my request (using Alloy UI but the request is very general):

 Y.io.request( launches the ajax request
    '/userEditForm/' + userID, //request string which calls the controller
    {
        method: 'GET',
        on: {
           success: function(e) {
                showEditForm();
           },
           failure: function(e) {
                alert(e.error.message);
           }
    }
});

我有一个请求设置,返回用户表单:

I have a request setup to return the user form:

@RequestMapping(value = "/userEditForm/{id}", method = RequestMethod.GET)
public ModelAndView getUserEditForm(@PathVariable Long id){
    ModelAndView mav = new ModelAndView("user");//user.html
    User user = userRepo.findOne(id);
    mav.addObject("user", user);
    return mav; 
}

在这一点上我所有的模型属性值填充是,

At this point all my Model attributes are populated with values being,

{用户=用户[的getId()= 123的getName()=鲍勃的getAddress()= 54小比利巷,getPhone()= 8457955555,getManager()=吉姆·智新]}

的形式,只有姓名,地址和电话号码字段回来,因为我不希望用户更新其他用户的ID或经理

The form is returned with only the name, address and phone number fields, as I don't want a user update another user's ID or Manager

<form action="/saveUser/{id}" method="post">
    <input type="text" name="name"><br>
    <input type="text" name="address"><br>
    <input type="text" name="phone"><br>
</form>

在提交我的saveUser @Controller

On submit my saveUser @Controller is called

@RequestMapping(value = "/saveUser/{id}", method = RequestMethod.POST)
    public String saveUser(@ModelAttribute("User") User user, @PathVariable Long id, Model model) {
        user = userRepo.findOne(id); 
        model.addAttribute("user", user);
        userRepo.save(user); 
        return "success";
    }

但在提交我的模型只持有其中更新表单上的值,不包括休息

However on submission my model only holds the values which were updated on the form and excludes the rest

{用户=用户[的getId()= 0,的getName()=鲍勃的getAddress()= 42袋鼠方式,getPhone()= 7563434444,getManager()= NULL}

尽管如此predefined字段填充,它抛出一个错误,因为该用户ID不能在提交null或0。我在寻找而去包括更新,在我的表单nonupdated各领域提交,但我不太清楚如何去了解这一点。

None of the predefined fields are populated, which throws an error as the user ID cannot be null or 0 on submission. I'm looking for away to include all fields updated and nonupdated upon my form submit, but I'm not quite sure how to go about this.

到目前为止,我读过有关Flash和重定向属性,这可能是我所需要的。有什么想法?

So far I've read about Flash and Redirect Attributes which might be what I need. Any thoughts?

编辑:尼尔·麦圭根解释说,我应该使用 @SessionAttributes 。我需要的是模型属性,将留访问整个会话,而不仅仅是一个单一的GET或POST。这可以通过会话属性来完成。

As Neil McGuigan explained, I should be using @SessionAttributes. What I needed were Model Attributes that would stay accessible through the entire session and not just a single GET or POST. This can be done with Session Attributes.

推荐答案

如果你只有部分更新记录,唯一正确和安全的方法是把它放在会话。使用 @ SessionAttributes (它会略高于控制器)。

If you are only partially updating a record, the only correct and secure way is to put it in session. Use @SessionAttributes (it goes just above your controller).

@SessionAttributes("user")

您再不需要从你的 saveUser 方法数据库获取用户。

You then don't need to fetch the user from the db in your saveUser method.