当我点击/repairOnlineHome
url thymeleaf渲染wizardStep时,尽管已在showRepairStep1()中初始化,但未初始化。
如果在下一步中,我点击了/repairOnlineHome2
url thymeleaf render wizardStep,并在之前的showRepairStep1()中设置了值,即wizardStep等于1.
令我感到非常惊讶的是,在控制器方法设置正确的值之前,看起来像为thymeleaf为wizardStep调用getter。
@Controller
public class RepairController {
private Integer wizardStep;// = new Integer(1);
@ModelAttribute("wizardStep")
public Integer wizardStep(){
return wizardStep;
}
@RequestMapping({"/repairOnlineHome"})
public String showRepairStep1(final SeedStarter seedStarter) {
seedStarter.setDatePlanted(Calendar.getInstance().getTime());
wizardStep = 1;
return "repairOnlineHome";
}
@RequestMapping({"/repairOnlineHome2"})
public String showRepairStep2(final SeedStarter seedStarter) {
seedStarter.setDatePlanted(Calendar.getInstance().getTime());
wizardStep = 2;
return "repairOnlineHome2";
}
...
}
在html中,我将wizardStep渲染为
<div>WizardStep: <span th:text="${wizardStep}">wizardStepNo</span></div>
我的问题是:为什么百事可乐在控制器方法中设置之前渲染模型属性以及如何解决这个问题。
答案 0 :(得分:0)
事实证明,在调用使用@RequestMapping注释的控制器方法之前,会调用@ModelAttribute方法。
所以,我注释掉了wizardStep()并切换到
@RequestMapping({"/repairOnlineHome"})
public String showRepairStep1(Model model, final SeedStarter seedStarter) {
wizardStep = 1;
model.addAttribute("wizardStep", wizardStep);
return "repairOnlineHome";
}