Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
912 views
in Technique[技术] by (71.8m points)

pass same data to multiple methods in the same controller using spring MVC / Boot

I am implementing a small shopping site, for the site i have an OrderController which is used for capturing CustomerData using form, showing orderDetails & placing the order. I have an AddToCartController which stores the cart entries in the session. When customer is in the cart page and clicks on next button the customer get's a form with four fields name, phone, email, paymenttype, upon clicking on save button the customer details will be sent to the OrderController and an order with a unique id having all the cartEntries will be generated and the customer will be redirected to orderDetailsPage which is in the same controller. Mind that i haven't saved my customer data yet to Database because i only want to save data when customer clicks on PlaceOrder Button. The problem comes here when customer clicks on PlaceOrder button i want to pass the complete orderData which i have generated previously to the placeOrder mapping and save the orderData. I don't want the orderData to be saved to the session because i already have a session cart. if you say generate the Data again with the service but for that i need customerData which i haven't saved to the Database yet and i really don't want to save this to DB unless the user cliks on placeOrder. Please tell me the best practice to follow here. what should i do to pass the data from one request to another ?

Skeleton code looks like this.

@PostMapping("/savecustomerinfo")
    public RedirectView saveCustomerInfo(@ModelAttribute("customerDetails") final CustomerDetailsModel customerDetailsModel, final HttpServletRequest request, final Model model,
                                         final RedirectAttributes redirectAttributes) {
        final OrderData orderData = orderService.createOrder(customerDetailsModel, request);
        redirectAttributes.addFlashAttribute("orderData",orderData);
        return  new RedirectView("orderdetails");
    }

    @GetMapping("/orderdetails")
    public String showOrderDetails(@ModelAttribute("orderData") final OrderData orderData, final Model model, final RedirectAttributes redirectAttributes) {
        model.addAttribute("orderData",orderData);
        redirectAttributes.addFlashAttribute("orderData", orderData);
        return "orderDetailsPage";
    }

    @PostMapping("/placeorder")
    public String placeOrder(){
        
        return "redirect:/summaryPage";
    }

Thank you so much for your valuable input.

question from:https://stackoverflow.com/questions/65897786/pass-same-data-to-multiple-methods-in-the-same-controller-using-spring-mvc-boo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...