如题,在后台代码进行重定向,发现请求是以GET方式从处理方法A到处理方法B的,但是处理方法B的@RequestMapping限定了只能接Post过来的请求,导致一直报HTTP405 ,错误的请求方式!貌似return new RedirectView("/postMessage", true, false, false);这个也不行!
学习是最好的投资!
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」
<annotation-driven/>
org.springframework.web.servlet.mvc.support.RedirectAttributes
我覺得這是設計上的問題,既然決定重定向當然不能走那個post方法,當然也可以自己用httpclient等工具模擬post,話說這是一個工程中吧
同意樓上意見,從設計上講就有問題。 既然自己有重定向過去的需求,為什麼限定只接受 get 請求 ?
你這種搞法是不可能的,spring的重定向不支援GET改POST
Spring MVC學習指南P62-63提到了Flash屬性,在重定向時可以用POST傳值,Controller的程式碼貼給你:
「要使用Flash屬性,必須在Springmvc設定檔中有一個
<annotation-driven/>
元素。然后,还必须在方法上添加一个新的参数类型org.springframework.web.servlet.mvc.support.RedirectAttributes
」我覺得這是設計上的問題,既然決定重定向當然不能走那個post方法,當然也可以自己用httpclient等工具模擬post,話說這是一個工程中吧
同意樓上意見,從設計上講就有問題。
既然自己有重定向過去的需求,為什麼限定只接受 get 請求 ?
你這種搞法是不可能的,spring的重定向不支援GET改POST