2016-12-16 7 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"コントローラは、 "PUT"です。電子すでに

<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

'method =" put "' 'method =" put "'に変更します – iNan

+0

HTTPステータス405 - リクエストメソッド 'GET 'はサポートされていません – lhw1995

答えて

2

あなたはので、代わりに

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

HTMLフォームのみ有効、ので、それを作る

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

、お使いのコントローラAPIがPUT用に設定されている

Controllerであなたのmethodを修正する必要がありますmethod POSTまたはGET

+0

次に、HTTPステータス405 - リクエストメソッド 'GET'がサポートされていません。 – lhw1995

+0

理由はtomcat7.0を使用している可能性がありますか? – lhw1995

+0

HTMLフォームメソッドはPOSTメソッドとGETメソッドでのみ有効です。 method = "put"を指定すると、単純にメソッドが定義されておらず、GETが考慮されます。私は私の答えを更新しました。あなたのコントローラを変更したくない場合は、フォームsubmitではなくajaxを使用する必要があります – ScanQR

関連する問題