0
我想在確認頁面上創建一個具有特殊操作的常規表單嚮導。摘要頁面上@SessionAttributes如何正確清除會話屬性
- 採集表格數據(表格預填充與會話屬性)
- 上的OK,通過張貼JSON保存表單數據和更新 會話屬性
- 表演形式更新會話屬性
- 點擊確定後,將表單數據保存到我的存儲。在按下「正確」後,我回到步驟1(表格被預填充更新的會話屬性)。
- 顯示確認頁面(一個成功的消息),並顯示出 「打印」按鈕(這是另一個網頁),並通過調用
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中,我與新addressForm
值更新newAddressInformation
所以,如果我回到步驟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
如果我刪除'sessionStatus.setComplete',我得到了我的第1步中的問題(新值預填充的形式) – Aure77
1)你必須爲AddressForm對象: - 街道= A,城= B 您可以更改值= Street = C 2)當您按下OK時,獲取控制器中的值爲Street = C,City = B的newAddressInformation對象。現在將addressForm對象從Streez = A更新爲Street = C,並顯示摘要頁面! 3)在摘要頁面按OK鍵後,如果按下「正確」返回到首頁,則顯示Street = C,City = B! 這是正確的嗎?你能告訴我你在哪個頁面上使用哪個對象嗎?你可以發佈我以前的答案嗎? – conFusl
因爲地址可以通過其他方式更新(例如移動應用程序)。如果用戶開始更改地址並在webapp中確認。他在移動設備上更正他的地址後。他想調整webapp上的內容。 Webapp不能包含設備上的新數據集,因爲webapp在會話中使用數據...是否可以只刪除一些會話屬性(在我的情況下是「addressForm」),而不是全部? – Aure77