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

当我们在控制器中使用@SesssionAttributes批注时,如果调用了在其签名中具有与@SessionAttribute相同名称的@ModelAttribute参数的任何处理程序方法,则会话属性将自动更新。

现在,我将控件重定向到另一个处理程序mathod,该处理程序又呈现了一个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。

这是视图分辨率配置。

1)控制器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

09-27 02:13