您好,登錄后才能下訂單哦!
在Spring MVC中,可以使用RedirectAttributes來(lái)傳遞重定向后的數(shù)據(jù)。下面是一個(gè)簡(jiǎn)單的示例:
@Controller
public class MyController {
@RequestMapping("/redirect")
public String redirectToPage(RedirectAttributes redirectAttributes) {
// 添加重定向后的數(shù)據(jù)
redirectAttributes.addFlashAttribute("message", "Redirected successfully!");
return "redirect:/targetPage";
}
@RequestMapping("/targetPage")
public String targetPage(@ModelAttribute("message") String message) {
// 獲取重定向前傳遞的數(shù)據(jù)
System.out.println("Message: " + message);
return "targetPage";
}
}
在上面的示例中,redirectToPage
方法重定向到targetPage
頁(yè)面,并將消息"Redirected successfully!"
通過(guò)RedirectAttributes
傳遞給targetPage
頁(yè)面。在targetPage
方法中,可以通過(guò)@ModelAttribute
注解獲取重定向前傳遞的數(shù)據(jù)并進(jìn)行處理。
需要注意的是,RedirectAttributes
中添加的數(shù)據(jù)使用addFlashAttribute
方法,這樣可以確保數(shù)據(jù)只在重定向過(guò)程中傳遞,在目標(biāo)頁(yè)面中可以通過(guò)@ModelAttribute
注解獲取到這些數(shù)據(jù)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。