Spring mvc Spring MVC中的Lazy@ModelAttribute

Spring mvc Spring MVC中的Lazy@ModelAttribute,spring-mvc,model,annotations,Spring Mvc,Model,Annotations,在spring中是否有类似于惰性模型属性注释的东西 下面是和平的代码来解释我在寻找什么 @Controller class MyController { private boolean hasValue=false; @RequestMapping(value "test.html") public String testMEthod(ModelMap model, @RequestParam(value = "person", defaultValue = "null") Perso

在spring中是否有类似于惰性模型属性注释的东西

下面是和平的代码来解释我在寻找什么

@Controller
class MyController {
    private boolean hasValue=false;

@RequestMapping(value "test.html")
public String testMEthod(ModelMap model, @RequestParam(value = "person", defaultValue = "null") Person person)
    person==null ? false : true;
    return "testResults";
}

@ModelAttribute("hasValue")
public boolean hasValue(){
     return hasValue;
}
上面的代码总是将false设置为model,因为所有@modeldattribute都是在调用任何@RequestMapping之前执行的。要工作,需要强制在从请求映射调用的方法之后将hasValue放入model。

使用默认的@modeldattribute注释,就像您提到的那样,@modeldattribute方法将在@RequestMapping注释的方法之前得到评估。可能有两种懒散的方法:

a。显式,有一个设置模型属性的方法在方法调用后,从@RequestMapping方法调用它:

public void setLazyModelAttribs(Model model){
     model.addAttribute("hasValue", hasValue);
}


@RequestMapping
public void testMethod(..Model model, ...){
    //normal request mapping
    setLazyModelAttribs(model);
}

b。使用after aspectj advice进行操作。

我正在寻找简化加载特定模型属性的方法,所以这不是我想要的。可能,我必须以正常的方式将其添加到模型中:好的,是的,可能我误解了这个问题,请您再次解释一下为什么要延迟加载该值,在方法调用之前加载它如何影响任何事情?例如,字段的值可以在请求映射调用的代码中更改。让我寻找lazy方法的原因是,在请求映射期间,我在代码中调用了几个方法,其中有几个方法的值可以更改,所以要解决这个问题,我应该做两件事之一:将模型传递给每个方法。我希望避免这种情况,因为它感觉不对劲,或者在调用方法后将其放入模型,在返回视图或重定向信息之前,此方法将操作拼接到非常远的代码行中。懒惰模型属性在这种情况下是完美的。你们是如何解决这个问题的?若我没记错的话,我放弃了,做了下面的加载函数之类的事情。