search

Home  >  Q&A  >  body text

java - springmvc 后台怎么Post一个请求

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

PHPzPHPz2893 days ago396

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 09:46:26

    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"

    reply
    0
  • PHPz

    PHPz2017-04-18 09:46:26

    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?

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:46:26

    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?

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:46:26

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

    reply
    0
  • Cancelreply