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

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

PHPz
PHPz

学习是最好的投资!

Antworte allen(4)
洪涛

Spring MVC学习指南P62-63提到了Flash属性,在重定向时可以用POST传值,Controller的代码贴给你:

@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();
    }
    

“要使用Flash属性,必须在Springmvc配置文件中有一个<annotation-driven/>元素。然后,还必须在方法上添加一个新的参数类型org.springframework.web.servlet.mvc.support.RedirectAttributes

PHPzhong

我觉得这是设计上的问题,既然决定重定向当然不能走那个post方法,当然也可以自己用httpclient等工具模拟post,话说这是一个工程中吧

刘奇

同意楼上意见,从设计上讲就有问题。
既然自己有重定向过去的需求,为什么限定只接受 get 请求 ?

Ty80

你这种搞法是不可能的,spring的重定向不支持GET改POST

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage