简体   繁体   中英

How do i pass parameters between methods in a spring Controller?

I want to send the value of the variable "b" from AfterLogin function to EnterCoupon function. Is it possible to send the CustomerID and Password parameters to the EnterCoupon function from AfterLogin function?

@RequestMapping("AfterLogin")
    public String AfterLogin(Model m,@RequestParam("CustomerID") String cid, @RequestParam("Password") String pswd) {
        double b=dao.getCustomer(cid, pswd);
        System.out.println(b);
        if(b!=0) {
            m.addAttribute("Balance",b);
            m.addAttribute("cid", cid);
            m.addAttribute("pswd", pswd);
            m.addAttribute("cst", new Coupon());
            return "EnterCoupon";
        }
        {
            m.addAttribute("msg","Invalid Customer ID or Password");
            return "Failure";
        }   
}
@RequestMapping("EnterCoupon")
public ModelAndView EnterCoupon(@ModelAttribute("cst")Coupon c, @RequestParam("CouponCode")String cc)
{
    double nb=cs.NewBalance(b, dao.getCoupon(cc));
    
    if(nb!=0)
    {
        ModelAndView mv=new ModelAndView("CashBack","cst",cst);
        
        return mv;
    }
    else
    {
        ModelAndView mv=new ModelAndView("Failure","msg","Invalid Coupon Code");
        return mv;
    }
}

Thank you for the help.

This is contradicting the whole purpose of Spring security. You should not be handling user login in that way.

Ideally, you should implement spring security. Use it to authenticate and authorise your users. Then, your logged in user will be accessible to any method throughout the application through security context holder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM