2016-12-16 41 views
-2

为什么我不能用RequestMethod.PUT总是有HTTP状态405 - 请求方法 'POST' 不支持

@Controller 
 
@RequestMapping(value = "/restaurant") 
 
public class ConsumerChangeInfoController { 
 

 
    private ConsumerChangeInfoService consumerChangeInfoService; 
 
    private ConsumerLoginService consumerLoginService; 
 

 
    
 
    @RequestMapping(value = "/consumerInfo",method = RequestMethod.PUT) 
 
    public String changeInfo(ChangeInfoDto changeInfoDto, HttpSession session, HttpServletRequest request){ 
 
     String path = session.getServletContext().getRealPath("/images/headPortrait"); 
 

 
     String fileName = changeInfoDto.getChooseHeadFile().getOriginalFilename(); 
 
     String extensionName = fileName 
 
       .substring(fileName.lastIndexOf(".") + 1); 
 
     String newFileName = String.valueOf(System.currentTimeMillis()) 
 
       + "." + extensionName; 
 

 
     File targetFile = new File(path, newFileName); 
 

 
     if(!targetFile.exists()){ 
 
      targetFile.mkdirs(); 
 
     } 
 

 
     
 
     try { 
 
      changeInfoDto.getChooseHeadFile().transferTo(targetFile); 
 
     } catch (Exception e) { 
 
      e.printStackTrace(); 
 
     } 
 
     String savePath = request.getContextPath()+"/images/headPortrait/"+newFileName; 
 
     consumerChangeInfoService.updateInfo((Consumer) session.getAttribute("loginUser"),changeInfoDto,savePath); 
 
     Consumer consumer = consumerLoginService.consumerLogin((LoginConsumerDto) session.getAttribute("loginConsumerDto")); 
 
     session.setAttribute("loginUser",consumer); 
 
     return "redirect:index"; 
 
    }

<form id="changeInfoForm" action="${pageContext.request.contextPath}/restaurant/consumerInfo" onsubmit="return checkSubmit()" method="post" enctype="multipart/form-data"> 
 
        <input type="text" placeholder="userName" name="showName" value="${sessionScope.loginUser.showName}"/> 
 
        <div class="bubble-box arrow-top" id="showNameBox"> 
 
         <div class="wrap"></div> 
 
        </div> 
 
        <img id="showNameIcon" src="${pageContext.request.contextPath}/images/registerIcon/true.png" height="20px" width="20px"/> 
 
        <div class="clearfix"> </div> 
 

 
        <input type="text" placeholder="phone" name="phone" value="${sessionScope.loginUser.phone}"/> 
 
        <div class="bubble-box arrow-top" id="phoneBox"> 
 
         <div class="wrap"></div> 
 
        </div> 
 
        <img id="phoneIcon" src="${pageContext.request.contextPath}/images/registerIcon/true.png" height="20px" width="20px"/> 
 
        <div class="clearfix"> </div> 
 

 
        <div id="localImag"> 
 
         <img id="ImgPr" src="${pageContext.request.contextPath}${sessionScope.loginUser.headPortrait}"/> 
 
        </div> 
 

 

 
        <input type="file" name="chooseHeadFile" id="up" onchange="upload()"/> 
 
        <div style="color:#888;">jpg,gif,png,max_size:1M</div> 
 
        <span style="color:red" id="errorMessage"></span> 
 

 

 
        <span id="errMessage" style="color: red;"></span> 
 
        <input type="hidden" name="_method" value="PUT"> 
 
        <div class="send"> 
 
         <input type="submit" value="Send" name="changeInfoButton"> 
 
        </div> 
 
       </form>

当我使用RequestMethod。 PUT,有错误HTTP状态405 - 请求方法'POST'不支持在浏览器中,但是当我使用RequestMethod.POST时,没有错误。我已经添加了表单,并且我可以得到“_method”的值控制器,是“放”。我hav Ë已经添加

<filter> 
 
    <filter-name>HiddenHttpMethodFilter</filter-name> 
 
     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
 
    </filter> 
 

 
    <filter-mapping> 
 
     <filter-name>HiddenHttpMethodFilter</filter-name> 
 
     <url-pattern>/*</url-pattern> 
 
    </filter-mapping>

,所以哪里是我的错误?

+0

我已经添加了input type =“hidden”name =“_ method”value =“PUT” – lhw1995

+2

change'method =“post”'to'method =“put”' – iNan

+0

然后我得到HTTP Status 405 - Request method'GET '不支持 – lhw1995

回答

2

你需要修复您的methodController

你的控制器API配置为PUT

@RequestMapping(value = "/consumerInfo",method = RequestMethod.PUT) 
    public String changeInfo(ChangeInfoDto changeInfoDto, HttpSession session, HttpServletRequest request){ 

所以不是让它,

@RequestMapping(value = "/consumerInfo",method = RequestMethod.POST) 
    public String changeInfo(ChangeInfoDto changeInfoDto, HttpSession session, HttpServletRequest request){ 

因为,HTML形式才有效对于method POST或GET

+0

然后我得到HTTP状态405 - 请求方法'GET'不支持。也许 – lhw1995

+0

也许原因是使用tomcat7.0? – lhw1995

+0

没有HTML表单方法仅适用于POST和GET方法。如果你让method =“put”,那么简单的意思就是没有定义方法,并且考虑GET。我已经更新了我的答案。如果你不想改变你的控制器,那么你需要使用Ajax而不是表单提交 – ScanQR

相关问题