我正在編寫一個類似嚮導的控制器,它處理跨多個視圖的單個bean的管理。我使用@SessionAttributes存儲bean,並使用SessionStatus.setComplete()在最終調用中終止會話。但是,如果用戶放棄嚮導並轉到應用程序的另一部分,則需要強制Spring在返回時重新創建@ModelAttribute。例如:在Spring MVC中強制初始化@ModelAttributes 3.1
@Controller
@SessionAttributes("commandBean")
@RequestMapping(value = "/order")
public class OrderController
{
@RequestMapping("/*", method=RequestMethod.GET)
public String getCustomerForm(@ModelAttribute("commandBean") Order commandBean)
{
return "customerForm";
}
@RequestMapping("/*", method=RequestMethod.GET)
public String saveCustomer(@ModelAttribute("commandBean") Order commandBean, BindingResult result)
{
[ Save the customer data ];
return "redirect:payment";
}
@RequestMapping("/payment", method=RequestMethod.GET)
public String getPaymentForm(@ModelAttribute("commandBean") Order commandBean)
{
return "paymentForm";
}
@RequestMapping("/payment", method=RequestMethod.GET)
public String savePayment(@ModelAttribute("commandBean") Order commandBean, BindingResult result)
{
[ Save the payment data ];
return "redirect:confirmation";
}
@RequestMapping("/confirmation", method=RequestMethod.GET)
public String getConfirmationForm(@ModelAttribute("commandBean") Order commandBean)
{
return "confirmationForm";
}
@RequestMapping("/confirmation", method=RequestMethod.GET)
public String saveOrder(@ModelAttribute("commandBean") Order commandBean, BindingResult result, SessionStatus status)
{
[ Save the payment data ];
status.setComplete();
return "redirect:/order";
}
@ModelAttribute("commandBean")
public Order getOrder()
{
return new Order();
}
}
如果用戶對將觸發「getCustomerForm」的方法(即http://mysite.com/order)的應用程序的請求,並且已經有一個「commandBean」會話屬性,然後在「getOrder」不叫。我需要確保在這種情況下創建一個新的Order對象。我只需要在getCustomerForm中手動重新填充它?
想法?如果我沒有說清楚,請告訴我。