gpt4 book ai didi

spring-mvc - Spring MVC : ModelAttribute method is guaranteed to run before GET and POST method?

转载 作者:行者123 更新时间:2023-12-02 21:26:18 24 4
gpt4 key购买 nike

我正在做一个 Spring Web 应用程序。我有一个具有以下方法的 Controller :

@RequestMapping(value="my", method = RequestMethod.GET)
public String get() {...}

@RequestMapping(value="my", method = RequestMethod.POST)
public String post() {...}

@ModelAttribute
public void mydata() {...}

该 Controller 用于显示表单并在提交表单时处理数据。

我的问题是:ModelAttribute 方法是否保证在 GET 方法(在 GET 请求中)和 POST 方法(在 POST 请求中)之前运行?

问候和感谢!

最佳答案

以下内容摘自 Spring documentation Spring 3

@ModelAttribute annotated methods are executed before the chosen @RequestMapping annotated handler method. They effectively pre-populate the implicit model with specific attributes, often loaded from a database. Such an attribute can then already be accessed through @ModelAttribute annotated handler method parameters in the chosen handler method, potentially with binding and validation applied to it.

当前文档(针对 Spring 4)是 here并陈述了几乎相同的事情

Instead @ModelAttribute methods in a controller are invoked before @RequestMapping methods, within the same controller

关于spring-mvc - Spring MVC : ModelAttribute method is guaranteed to run before GET and POST method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23987792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com