Spring不会在视图

时间:2016-01-13 22:27:58

标签: java spring spring-mvc model-view-controller

我是Spring MVC的新手。我有关于在视图之间传递对象的问题。我可以将对象Flight从视图“flight”传递到查看“searchFlight”而没有问题。不应该将任何对象从“searchFlight”传递到“bookFlight”,因为此时我将所需信息存储在控制器之外的静态对象中。

在“bookFlight”中我从静态对象加载所有数据(在视图中不可编辑),然后我想在“bookFlight”中收集数据并将其传递给“performBook”视图。收集的数据也存储在控制器外的其他静态对象中。此时在“performBook”中我可以引用静态对象并显示存储的数据 - 但是我无法将“bookFlight”中的任何内容保存到我的静态对象中。

如果我将静态对象更改为非静态(只需使用不同的metohd体复制粘贴flighSearch方法),那么我也无法从form:form中收集数据并将其保存在对象中或将其传递给下一个视图。

这是我的控制器。 GlobalCache中的所有内容都是公共静态的。每个模型对象都有setter和getter。

@RequestMapping(value = "/flight", method = RequestMethod.GET)
public ModelAndView flightSearch(ModelMap model) {
    GlobalCache.flight=new Flight();
    GlobalCache.flightTime=GlobalCache.generateFlightTIme()+1;
    GlobalCache.flight.setTravelLength(GlobalCache.flightTime);
    return new ModelAndView("flightSearch","command",new Flight());

}

@RequestMapping(value="searchFlight", method = RequestMethod.POST)
public String search(@ModelAttribute("user") Flight flight, ModelMap model) {
    model.addAttribute("departureDate",flight.getDepartureDate());
    model.addAttribute("departureAirport",flight.getArrivalAirport());
    model.addAttribute("arrivalDate", flight.getArrivalDate());
    model.addAttribute("arrivalAirport", flight.getArrivalAirport());
    model.addAttribute("ticketNo", flight.getTicketNo());
    model.addAttribute("travelType", flight.getTravelType());
    model.addAttribute("flight", flight);

    GlobalCache.flight=flight;
    GlobalCache.flightTime=GlobalCache.generateFlightTIme()+1;
    GlobalCache.flight.setTravelLength(GlobalCache.flightTime);

    return "results";
}

@RequestMapping(value="/bookFlight", method = RequestMethod.POST)
public ModelAndView bookFLight(@ModelAttribute("user") Ticket ticket, ModelMap model) {

    ticket=new Ticket();
    ticket.setClassesList(classes);
    ticket.setPaymentTypeList(paymentType);
    ticket.setSeatPositionList(seatPosition);
    ticket.setFlight(GlobalCache.flight);
    ticket.setPayment(new Payment());
    ticket.setUtil(new Util());
    ticket.getPayment().setAmountToPay(GlobalCache.generateTicketCost());

    Traveler traveler = new Traveler();
    ticket.setTraveler(traveler);

    model.addAttribute("ticket", ticket);
    model.addAttribute("flight", GlobalCache.flight);
    model.addAttribute("traveler", ticket.getTraveler());
    model.addAttribute("payment", ticket.getPayment());

    GlobalCache.ticket=ticket;

    return new ModelAndView("book","command",GlobalCache.ticket);
}

@RequestMapping(value="performBook", method = RequestMethod.POST)
public ModelAndView booked(@ModelAttribute("user") Ticket ticket, ModelMap model) {
    model.addAttribute("ticket", GlobalCache.ticket);
    return new ModelAndView("booked","command",GlobalCache.ticket);
}

在“bookFlight”视图中处理表单的示例。

 <form:input path="traveler.name" value="${traveler.name}" type="text" id="5" class="form-control" aria-describedby="basic-addon1"/>

班级结构:

class Ticket{
  private Flight flight;
  private Traveler traveler;
  private Util util;
  //getters and setters and other attributes
}

此时我没有时间创建单独的控制器,希望只用一个就可以解决。

0 个答案:

没有答案