Java @方法级别的ModelAttribute

Java @方法级别的ModelAttribute,java,spring,jakarta-ee,spring-mvc,Java,Spring,Jakarta Ee,Spring Mvc,我指的是来自 其中@ModelAttribute位于方法级别 /** * Retrieves all addresses and allows them to be used as a model * @return a model attribute containing addresses */ @ModelAttribute("addresses") public List<Address> getAllAddresses() { // Delegate

我指的是来自

其中@ModelAttribute位于方法级别

 /**
  * Retrieves all addresses and allows them to be used as a model
  * @return a model attribute containing addresses
  */
 @ModelAttribute("addresses")
 public List<Address> getAllAddresses() {
  // Delegate to service
  return addressService.getAll();
 }



@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
    dateFormat.setLenient(false);
    webDataBinder.registerCustomEditor(Date.class, new             CustomDateEditor(dateFormat, true));
}

 /**
  * Handles and retrieves a JSP page containing all addresses.
  * We use the @ModelAttribute to pass the data to the view
  * 
  * @return the name of the JSP page
  */
    @RequestMapping(value="list1", method = RequestMethod.GET)
    public String getAllUsingModelAttribute() {
     logger.debug("Received request to show all addresses page");

     // No need to add the model here
     // It has been automatically added when we used the @ModelAttribute annotation earlier
     // The name of the ModelAttribute is "addresses". Your JSP should reference "addresses"

     // This will resolve to /WEB-INF/jsp/addressespage.jsp
     return "addressespage";
 }
/**
*检索所有地址并允许它们用作模型
*@返回包含地址的模型属性
*/
@ModelAttribute(“地址”)
公共列表getAllAddresses(){
//委派服务
return addressService.getAll();
}
@InitBinder
public void initBinder(WebDataBinder WebDataBinder){
SimpleDataFormat dateFormat=新SimpleDataFormat(“yyyy-mm-dd”);
dateFormat.setLenient(false);
webDataBinder.registerCustomEditor(Date.class,新的CustomDateEditor(dateFormat,true));
}
/**
*处理并检索包含所有地址的JSP页面。
*我们使用@ModelAttribute将数据传递给视图
* 
*@返回JSP页面的名称
*/
@RequestMapping(value=“list1”,method=RequestMethod.GET)
公共字符串getAllUsingModelAttribute(){
调试(“收到显示所有地址页面的请求”);
//无需在此添加模型
//在前面使用@ModelAttribute注释时,它已自动添加
//ModelAttribute的名称是“addresses”。JSP应该引用“addresses”
//这将解析为/WEB-INF/jsp/AddressPage.jsp
返回“地址页”;
}
我的问题是,当请求到达控制器时,带@modeldattribute注释的方法是否自动激发?在本例中,方法getAllAddresses()。在这个例子中,我没有看到这个方法在任何地方被显式调用

或者,当请求到达包含@RequestMapping的方法后立即放置的方法时,会触发它


@initBinder的同样问题是,在表单提交后,带注释的方法是否会自动触发?

尝试调试控制器并放置一些日志语句

您首先看到的是对
@modeldattribute
注释方法的调用

如果将自定义对象放入模型中,或者如果处理程序方法将自定义对象声明为参数,则将调用您的
initBinder

@ModelAttribute("obj")
 public List<Address> getAllAddresses() {
  // Delegate to service
  return new CustomObject();
 }

@RequestMapping(value="list1", method = RequestMethod.GET)
    public String getAllUsingModelAttribute(CustomObject customObject) {
@modeldattribute(“obj”)
公共列表getAllAddresses(){
//委派服务
返回新的CustomObject();
}
@RequestMapping(value=“list1”,method=RequestMethod.GET)
公共字符串getAllUsingModelAttribute(CustomObject CustomObject){

如果页面具有由form taglib使用Spring-like创建的表单,则页面将呈现时:

<form:form commandName="addresses" method="post">

这将引导您的页面查找名为“addresses”的适当@ModelAndAttribute。如果您的程序能够找到它(如您定义的)

@modeldattribute(“地址”)
公共列表getAllAddresses(){
//委派服务
return addressService.getAll();
}
使模型可用于您的表单,并准备更改列表

然后,您可以使用其他方法提交表单,如下所示:

@RequestMapping(value="path", method=RequestMethod.POST)
public String doSomethingForMe(@Valid @ModelAttribute("addresses") List <Address>, BindingResult result) {
     if(result.hasErrors()){
         return "TO_THE_PAGE_YOU_WANT_FOR_SHOWING_THE_ERROR_TO_USER";
      }

      // do the other stuffs you want !
}  
@RequestMapping(value=“path”,method=RequestMethod.POST)
公共字符串doSomethingForMe(@Valid@ModelAttribute(“地址”)列表,BindingResult){
if(result.hasErrors()){
返回“到您想要的页面,向用户显示错误”;
}
//做你想做的其他事情!
}  
我只是在这里写了这段代码,如果你发现任何打字错误,我很抱歉,但事情就是这样

祝你好运!

@RequestMapping(value="path", method=RequestMethod.POST)
public String doSomethingForMe(@Valid @ModelAttribute("addresses") List <Address>, BindingResult result) {
     if(result.hasErrors()){
         return "TO_THE_PAGE_YOU_WANT_FOR_SHOWING_THE_ERROR_TO_USER";
      }

      // do the other stuffs you want !
}