gpt4 book ai didi

java - Spring MVC 中重定向时丢失 @SessionAttribute

转载 作者:行者123 更新时间:2023-11-30 07:59:17 34 4
gpt4 key购买 nike

我面临着一个特殊的问题。我正在学习 SpringMVC,这是我第一个使用它的应用程序项目。

当我们在 Controller 中使用@SessionAttributes注释时,如果调用任何具有与其签名中的@SessionAttribute同名的@ModelAttribute参数的处理程序方法,则 session 属性会自动更新。

现在我将控件重定向到另一个处理程序方法,该方法又呈现一个 jsp。我面临的问题是我能够在重定向处理程序方法中访问 @SessionAttribute 但在 jsp 中它呈现 session 属性丢失。

这是代码。

@Controller
@EnableWebMvc
@RequestMapping("/courtreservation/*")
@SessionAttributes("courtDetails")
public class ControllerServlet {



@RequestMapping(value="basePage",method=RequestMethod.GET)
public String basePageRenderer(Model model,HttpServletRequest request){
CourtDetails tempObj = (CourtDetails)request.getSession().getAttribute("courtDetails");
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load("configs/applicationContext.xml");
context.refresh();
CourtDetails courtDetails = (CourtDetails)context.getBean("courtDetails");
List<String> allSports = courtDetails.getAllSports();
Calendar calendar = Calendar.getInstance();
int numDay = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(numDay);
String[] days = {"sunday","monday","tuesday","wednesday","friday","saturday","sunday"};
/*List<String> daysAvailableForReservation = new ArrayList<>();
if(numDay == 1) {
for(String day:days) {
daysAvailableForReservation.add(day);
}
}
else {
for(int i=numDay--;i<days.length;i++) {
daysAvailableForReservation.add(days[i]);
}
}*/
model.addAttribute("daysAvailableForReservation", days);
model.addAttribute("courtDetails", courtDetails);
model.addAttribute("allSports",allSports);
return "welcomePage";
}

@RequestMapping(value="checkAvailability",method=RequestMethod.POST)
public String checkAvailability(CourtDetails courtDetails,HttpServletRequest request,HttpServletResponse response,Model model) {
HttpSession session = request.getSession();
courtDetails = (CourtDetails)session.getAttribute("courtDetails");
Boolean isAvailable = courtDetails.checkAvailability();
if(isAvailable) {
model.addAttribute("isAvailable","true");
model.addAttribute("courtDetails",courtDetails);
return "welcomePage";
}
model.addAttribute("isAvailable", "false");
model.addAttribute("courtDetails",courtDetails);
return "unavailable";
}






}

从 checkAvailability 重定向后,@SessionAttribute("courtDetails") 在 basePageRenderer() 中可用,但在它呈现的 jsp(welcomePage.jsp)中不可用。

这是 View 分辨率配置。

1) Controller servlet 配置:

<bean class="org.springframework.web.servlet.view.XmlViewResolver" 
p:location="/WEB-INF/classes/configs/appViews.xml"
p:order="0" />

2) appView.xml

<bean id="unavailable"
class="org.springframework.web.servlet.view.RedirectView"
p:url="/Sports_Reservation/courtreservation/basePage" />

最佳答案

似乎解释得很好here 。我遇到了类似的问题,最终使用 forward 而不是 redirect

关于java - Spring MVC 中重定向时丢失 @SessionAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32193025/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com