java - springmvc 后台怎么Post一个请求
PHPz
PHPz 2017-04-18 09:44:26
0
4
392

如题,在后台代码进行重定向,发现请求是以GET方式从处理方法A到处理方法B的,但是处理方法B的@RequestMapping限定了只能接Post过来的请求,导致一直报HTTP405 ,错误的请求方式!
貌似return new RedirectView("/postMessage", true, false, false);这个也不行!

PHPz
PHPz

学习是最好的投资!

reply all(4)
洪涛

Spring MVC Study Guide P62-63 mentioned Flash attributes, you can use POST to pass values ​​when redirecting, the Controller code is posted for you:

@RequestMapping(value = "/product_save", method = RequestMethod.POST)
    public String saveProduct(ProductForm productForm, RedirectAttributes redirectAttributes) {
        logger.info("saveProduct called");
        // no need to create and instantiate a ProductForm
        // create Product
        Product product = new Product();
        product.setName(productForm.getName());
        product.setDescription(productForm.getDescription());
        try {
            product.setPrice(Float.parseFloat(productForm.getPrice()));
        } catch (NumberFormatException e) {
        }

        // add product
        Product savedProduct = productService.add(product);
        
        redirectAttributes.addFlashAttribute("message", "The product was successfully added.");

        return "redirect:/product_view/" + savedProduct.getId();
    }
    

"To use Flash properties, you must have one in the Springmvc configuration file <annotation-driven/>元素。然后,还必须在方法上添加一个新的参数类型org.springframework.web.servlet.mvc.support.RedirectAttributes"

PHPzhong

I think this is a design problem. Since you have decided to redirect, of course you cannot use the post method. Of course, you can also use httpclient and other tools to simulate posts. This is a project in progress, right?

刘奇

I agree with the opinion above, there is a problem in terms of design.
Since I have the need to redirect to the past, why limit myself to only accepting get requests?

Ty80

Your approach is impossible. Spring redirection does not support changing GET to POST

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template