我想创建一个普通表单向导,在确认页面上有一个特殊操作。
SessionStatus#setComplete()
我的问题是当用户想要在确认页面上打印时(他转到打印页面),但我的会话属性已在步骤5中被销毁。而我正在丢失我的模型数据。
如果我删除SessionStatus#setComplete()
,当用户返回到第1步时,之前的数据会被预先填充而不是新数据。
那么,我该怎么做呢?
@SessionAttributes({"addressForm", "userContactDetails", "newAddressInformation"})
public class AddressProfileController {
//....
@ModelAttribute("addressForm")
public AddressForm getAddressForm() {
// Form is pre-filled
return webModelMapper.processMapping(new AddressForm(), getUserContactDetails().getAddressInformation());
}
@ModelAttribute("newAddressInformation")
public AddressInformation getNewAddressInformation() {
return new AddressInformation();
}
@ModelAttribute("userContactDetails")
public ContactDetails getUserContactDetails() {
return profilService.getUserContactDetails();
}
//....
在第2步,我使用新的newAddressInformation
值更新addressForm
因此,如果我回到第1步,则会在表单上预先填写新值。
步骤5,删除确认页面上的会话属性:
@RequestMapping(value = "/dcr/dcr-confirmation-adresse.html")
public String confirmUserAddressForm(@ModelAttribute("newAddressInformation") AddressInformation newAddressInformation, @ModelAttribute("userContactDetails") ContactDetails contactDetails, Model model, SessionStatus sessionStatus) {
// do something
sessionStatus.setComplete(); // invalidate session attributes
在步骤5之后,会话属性不可用(使用以前的数据):
@RequestMapping(value = "/print-address.html")
public String printUserAddressForm(@ModelAttribute("newAddressInformation") AddressInformation newAddressInformation, @ModelAttribute("userContactDetails") ContactDetails contactDetails, Model model) {
// newAddressInformation is empty bean, because session attributes has been previously detroyed
答案 0 :(得分:0)
更新
@RequestMapping(value = "/dcr/dcr-confirmation-adresse.html")
public String confirmUserAddressForm(@ModelAttribute("newAddressInformation") AddressInformation newAddressInformation, @ModelAttribute("userContactDetails") ContactDetails contactDetails, Model model, SessionStatus sessionStatus, HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("addressForm", addressForm);
所以你用更新的对象设置会话属性!