Reputation: 31
I am faced with a peculiar problem. I am learning SpringMVC and this is my first application project with it.
When we use @SesssionAttributes annotation in a controller, the session attribute is automatically updated, if any handler method having a @ModelAttribute argument with the same name as a @SessionAttribute in its signature is invoked.
now i redirected the control to another handler mathod which in turn renders a jsp. The problem I am facing is that I am able to access the @SessionAttribute in the redirected handler method but in the jsp it renders the session attribute is lost.
Here is the code.
@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";
}
}
After the redirect from checkAvailability the @SessionAttribute("courtDetails") is available in the basePageRenderer() but not in the jsp it renders which is welcomePage.jsp.
here are the view resolution configs.
1) Controller servlet config:
<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" />
Upvotes: 1
Views: 3466
Reputation: 48287
Change your second handler to be like this:
@RequestMapping(value="checkAvailability",method=RequestMethod.POST)
public String checkAvailability(
@ModelAttribute("courtDetails") CourtDetails courtDetails,
HttpServletRequest request,
HttpServletResponse response,
Model model
)
This will read the modelAttribute from session and then set the values from the post. You don't need to manually read the value from session.
Upvotes: 0