在控制器中处理完 @ModelAttribute 后,如何在 Spring MVC 中重置 @ModelAttribute?

发布于 2024-11-26 22:11:12 字数 669 浏览 0 评论 0原文

我定义了一个 @ModelAttribute("mymodel")

@ModelAttribute("mymodel")
MyModel mymodel() {
  MyModel mymodel = new MyModel();
  return mymodel;
 }


@RequestMapping(value = "/save", method = RequestMethod.POST)
public final void save(@ModelAttribute("mymodel") MyModel mymodel, 
                           final BindingResult binding,
    final HttpServletRequest request, 
    final ModelMap modelMap) throws Exception {
    modelService.save(mymodel);

            // try to reset the model --> doesn't work!!!
    myModel = new MyModel();
}

问题是,即使我在保存方法中重置模型,如果我在保存操作后重新加载页面并再次保存,该模型包含的所有值之前的myModel

处理完成后如何重置?

I have defined a @ModelAttribute("mymodel")

@ModelAttribute("mymodel")
MyModel mymodel() {
  MyModel mymodel = new MyModel();
  return mymodel;
 }


@RequestMapping(value = "/save", method = RequestMethod.POST)
public final void save(@ModelAttribute("mymodel") MyModel mymodel, 
                           final BindingResult binding,
    final HttpServletRequest request, 
    final ModelMap modelMap) throws Exception {
    modelService.save(mymodel);

            // try to reset the model --> doesn't work!!!
    myModel = new MyModel();
}

The problem is, even though I reset the model in the save method, if I reload the page after a save operation and save a second time, the model contains all of the values of the previous myModel.

How do I reset it after it has been processed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

任性一次 2024-12-03 22:11:12

除非我错过了我的猜测,否则这是因为

myModel = new MyModel();

只会重置方法内的引用,就像从中获取 MyModel 一样List 然后调用 myModel = new MyModel(); 不会更改列表中的元素,只会更改本地引用。

您很可能需要将新的 MyModel() 放入模型或 modelMap 中。

发布后重定向模式可能对您也有用。让您的 POST 方法

返回“redirect:originalpage.htm”

这应该重新加载原始页面,并且还意味着如果您点击刷新,您将不会重新提交 POST,从而保存您的对象两次。

Unless I miss my guess, this is because

myModel = new MyModel();

is only going to reset the reference within the method, in the same way that getting a MyModel from a List<MyModel> and then calling myModel = new MyModel(); would not change the element in the List, only your local reference.

You most likely need to put the new MyModel() into the model or modelMap.

The redirect after post pattern may also be useful to you here. Have your POST method

return "redirect:originalpage.htm"

This should reload the original page fresh, and will also mean that if you hit refresh you won't resubmit the POST, saving your object twice.

極樂鬼 2024-12-03 22:11:12

我不会这样做。

尝试在 get 请求中设置您的模型,例如:

@RequestMapping(value = "/save", method = RequestMethod.GET)
public ModelAndView getSaveForm(ModelMap model) {
   model.addAttribute("mymodel", new MyModel());
   return new ModelAndView("newView", model);
}

I wouldn't do it like this.

Try setting your model in the get request like :

@RequestMapping(value = "/save", method = RequestMethod.GET)
public ModelAndView getSaveForm(ModelMap model) {
   model.addAttribute("mymodel", new MyModel());
   return new ModelAndView("newView", model);
}
橘虞初梦 2024-12-03 22:11:12

我在处理 Portlet in Action 中的 BookCatalog 时遇到了类似的问题。我的解决方案是使用 Model.addAttribute() 手动重置它。例如:

@RequestMapping(value = "/save", method = RequestMethod.POST)
public final void save(@ModelAttribute("mymodel") MyModel mymodel, 
   final BindingResult binding,
   Model model
   final HttpServletRequest request, 
   final ModelMap modelMap) throws Exception 
{
   modelService.save(mymodel);
   model.addAttrubute("mymodel", new MyModel());
}

I was running into a similar same issue while messing with the BookCatalog from Portlets in Action. My solution was to manually reset it using Model.addAttribute(). For example:

@RequestMapping(value = "/save", method = RequestMethod.POST)
public final void save(@ModelAttribute("mymodel") MyModel mymodel, 
   final BindingResult binding,
   Model model
   final HttpServletRequest request, 
   final ModelMap modelMap) throws Exception 
{
   modelService.save(mymodel);
   model.addAttrubute("mymodel", new MyModel());
}
戴着白色围巾的女孩 2024-12-03 22:11:12

当控制器完成处理后我们想要添加响应标头时,下面的示例方法有效。 @ModelAttribute("mymodel") 在控制器方法处理之前调用,因此我们不能根据处理结果放置任何值。
//在请求方法中添加一个参数“HttpServletResponse”

@RequestMapping(method = RequestMethod.POST, value = APIConstants.Request_URL, headers = APIConstants.Request_HEADER, Produces = APIConstants.PRODUCES)
公共@ResponseBody PojoRS autheticateByPassword(@RequestBody()PojoRQ apiRequest,HttpServletResponse响应)
抛出异常{

    try
    {
        // All code related to processing

    }
    catch(Exception e)
    {
        throw e;
    }
    //Just before sending back the response add below line
    response.setHeader("Response_Key","Response_Header_value");
    return PojoRS;
}

Below sample method works when we want to add the response header once the controller has finished its processing. @ModelAttribute("mymodel") is invoked before controller method processing so we can not put any values based on processing results.
//Add a parameter "HttpServletResponse"in the request method

@RequestMapping(method = RequestMethod.POST, value = APIConstants.Request_URL, headers = APIConstants.Request_HEADER, produces = APIConstants.PRODUCES)
public @ResponseBody PojoRS autheticateByPassword(@RequestBody() PojoRQ apiRequest, HttpServletResponse response)
throws Exception {

    try
    {
        // All code related to processing

    }
    catch(Exception e)
    {
        throw e;
    }
    //Just before sending back the response add below line
    response.setHeader("Response_Key","Response_Header_value");
    return PojoRS;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文