How can I discount in JSP

Viewed 59

I'm a newbie and trying to make JSP project, I'm stuck at making Controller.java This is the code that I already have:

package com.assignment;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeContronller {
    @RequestMapping("/order")
    public ModelAndView message(@RequestParam("name") String name,
                               @RequestParam("email") String email,
                               @RequestParam("pnumber") String pnumber,
                              @RequestParam("address") String address,
                              @RequestParam("pizztype") String pizzatype,
                              @RequestParam("pizzasize") String pizzasize,
                              @RequestParam("numpizza") String numpizza
            )
    {
        
        
        ModelAndView m = new ModelAndView("page1");
        
        m.addObject("name", name);
        m.addObject("email", email);
        m.addObject("pnumber", pnumber);
        m.addObject("adress", address);
        m.addObject("pizzatype", pizzatype);
        m.addObject("pizzasize", pizzasize);
        
        return m;
    }
}
    

I need help to create a discount code that when type 1111 will give 10 % discount and 2222 will give 30% discount

1 Answers

Try add "discount" variable in your ModelAndView.

m.addObject("discount", null);

Then, map "discount" model key with an <input/> field in your "page1" view.

For example, using apache velocity and jquery:

 <form method="POST">
  #springBind("discount") 
    <input type="text" value="$!{status.value}" />
 </form> 
 <button type="submit" onclick="$('#frm').submit()" />

After that, you need to create a new POST method to process provided infromation:

@RequestMapping(value = "/order", method = RequestMethod.POST) 
public ModelAndView message(ModelAndView model) {

   //Here you can read "discount" key with the value provided by user
   final Integer discount = (String) model.getAttribute("discount");

   //Here you can process this information
   final Integer price = applyDiscount(discount); //Here apply the algorithm of 10% or 30%.

   ...

  
   return model;
}
Related