JavaWeb redirection and forwarding usage scenarios

Redirection is a client jump, 2 requests, 2 responses, so the data submitted at the beginning will be lost in this process.

Forwarded as a server jump, 1 request, 1 response, the data will then be transferred to the page to be jumped.

 

In crud operations, operations that do not require display, such as additions, deletions, and modifications, require redirection, otherwise multiple submissions may produce some additional errors.

When you need to pass parameters, you need to jump to the edit page when you do not enter the edit attribute, then forwarding is used.

 

@RequestMapping("admin_product_add")
    public String add(Model model, Product p) {
        p.setCreateDate(new Date());
        productService.add(p);
        return "redirect:admin_product_list?cid="+p.getCid();
    }
 
    @RequestMapping("admin_product_delete")
    public String delete(int id) {
        Product p = productService.get(id);
        productService.delete(id);
        return "redirect:admin_product_list?cid="+p.getCid();
    }
 
    @RequestMapping("admin_product_edit")
    public String edit(Model model, int id) {
        Product p = productService.get(id);
        Category c = categoryService.get(p.getCid());
        p.setCategory(c);
        model.addAttribute("p", p);
        return "admin/editProduct";
    }
 
    @RequestMapping("admin_product_update")
    public String update(Product p) {
        productService.update(p);
        return "redirect:admin_product_list?cid="+p.getCid();
    }

 

Guess you like

Origin www.cnblogs.com/huqingfeng/p/12687511.html