Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Spring MVC 3.1中强制初始化@ModelAttributes_Spring_Session_Spring Mvc - Fatal编程技术网

在Spring MVC 3.1中强制初始化@ModelAttributes

在Spring MVC 3.1中强制初始化@ModelAttributes,spring,session,spring-mvc,Spring,Session,Spring Mvc,我正在编写一个类似向导的控制器,它处理跨多个视图的单个bean的管理。我使用@SessionAttributes存储bean,使用SessionStatus.setComplete()在最后一次调用中终止会话。但是,如果用户放弃向导并转到应用程序的另一部分,我需要强制Spring在返回时重新创建@ModelAttribute。例如: @Controller @SessionAttributes("commandBean") @RequestMapping(value = "/order") pu

我正在编写一个类似向导的控制器,它处理跨多个视图的单个bean的管理。我使用@SessionAttributes存储bean,使用SessionStatus.setComplete()在最后一次调用中终止会话。但是,如果用户放弃向导并转到应用程序的另一部分,我需要强制Spring在返回时重新创建@ModelAttribute。例如:

@Controller
@SessionAttributes("commandBean")
@RequestMapping(value = "/order")
public class OrderController
{
  @RequestMapping("/*", method=RequestMethod.GET)
  public String getCustomerForm(@ModelAttribute("commandBean") Order commandBean)
  {
    return "customerForm";
  }

  @RequestMapping("/*", method=RequestMethod.GET)
  public String saveCustomer(@ModelAttribute("commandBean") Order commandBean, BindingResult result)
  {
    [ Save the customer data ];
    return "redirect:payment";
  }

  @RequestMapping("/payment", method=RequestMethod.GET)
  public String getPaymentForm(@ModelAttribute("commandBean") Order commandBean)
  {
    return "paymentForm";
  }

  @RequestMapping("/payment", method=RequestMethod.GET)
  public String savePayment(@ModelAttribute("commandBean") Order commandBean, BindingResult result)
  {
    [ Save the payment data ];
    return "redirect:confirmation";
  }

  @RequestMapping("/confirmation", method=RequestMethod.GET)
  public String getConfirmationForm(@ModelAttribute("commandBean") Order commandBean)
  {
    return "confirmationForm";
  }

  @RequestMapping("/confirmation", method=RequestMethod.GET)
  public String saveOrder(@ModelAttribute("commandBean") Order commandBean, BindingResult result, SessionStatus status)
  {
    [ Save the payment data ];
    status.setComplete();
    return "redirect:/order";
  }

  @ModelAttribute("commandBean")
  public Order getOrder()
  {
    return new Order();
  }
}
如果用户向应用程序发出将触发“GetCustomPerform”方法(即)的请求,并且已经存在“commandBean”会话属性,则不会调用“getOrder”。我需要确保在这种情况下创建一个新的Order对象。我是否需要在GetCustomPerform中手动重新填充它


想法?如果我说得不清楚,请告诉我。

是的,听起来您可能需要在
GetCustomPerform
中手动重新填充它-如果某个属性是
@SessionAttributes
的一部分并且出现在会话中,那么就像您所说的
@ModelAttribute
方法不会被调用


另一种方法是只使用
getCustomerPerform
方法和
@ModelAttribute方法定义一个新控制器,但不使用类型上的@SessionAttributes,这样可以保证调用@ModelAttribute方法,然后继续使用现有控制器中现有的@RequestMapped方法。

是的,听起来您可能需要在
GetCustomPerform
中手动重新填充它-如果某个属性是
@SessionAttributes
的一部分并出现在会话中,然后,正如您所说的,
@modeldattribute
方法不会被调用

另一种方法是只使用
getCustomerPerform
方法和
@ModelAttribute方法定义一个新控制器,但不使用类型上的@SessionAttributes,这样可以保证调用@ModelAttribute方法,然后继续使用现有控制器中的现有@RequestMapped方法