仅在单击“提交”按钮后,才需要重定向到页面,但无需直接在地址栏中输入网址.如果直接在地址栏中输入,则应显示主页.
我使用session.getAttribute来完成上述过程.我正在考虑是否有任何替代方案,因为我需要为每个post方法执行此操作…
下面的方法适用于第一页,在这里我创建了一个会话属性,该属性将在下一页中使用.
@RequestMapping(value = "/payment",method = RequestMethod.POST)
public String submitForPayment(@modelattribute("deposit") Deposit deposit,ModelMap model,HttpServletRequest request) throws IOException
{
try {
HttpSession sessionForNextPage = request.getSession(true);
sessionForNextPage.setAttribute("vNumber",deposit.getValidityNumber());
return "redirect:success";
} catch (NullPointerException exception) {
return "redirect:payment";
}
}
@RequestMapping(value = "/success",method = RequestMethod.GET)
public String showSuccess(ModelMap model,HttpServletRequest request)
{
try {
view = "success";
HttpSession session = request.getSession(false);
int vNumber = (int) session.getAttribute("vNumber");
System.out.println(vNumber);
if (vNumber != 0) {
request.getSession(false).removeAttribute("vNumber");
return view;
}
else
return "pay";
} catch (Exception e) {
return "redirect:pay";
}
}
最佳答案
原文链接:https://www.f2er.com/spring/531852.html