-2

@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>

When I use RequestMethod.PUT,there is error HTTP Status 405 - Request method 'POST' not supported in browser,but when I use RequestMethod.POST, there is no error.I have already add in form,and I can get "_method"'s value in controller,is "PUT".I have already add

    <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>

,so where is my error?

lhw1995
  • 17
  • 2
  • 10
  • I have already add input type="hidden" name="_method" value="PUT" – lhw1995 Dec 16 '16 at 05:41
  • 2
    change `method="post"` to `method="put"` – Raghav Dec 16 '16 at 05:42
  • Then i get HTTP Status 405 - Request method 'GET' not supported – lhw1995 Dec 16 '16 at 06:04
  • the 405 error might be a result of the redirect at the end of the `changeInfo(...)` method. Do you have a method that handles the `redirect:index` mapping (or a mapping somewhere to handle the `GET` of `index` in the redirect? – blurfus Dec 16 '16 at 06:09
  • I have two other method handles the redirect:index mapping,but it's url is diffirent.How to change them? – lhw1995 Dec 16 '16 at 06:16
  • can you be more specific? are those methods in the same class? do they work? - this might be a case where `redirect:index` is looking at an URL `/restaurant/index` or `/restaurant/index.html` and changing it to `redirect:../index` or `redirect:/index` might be enough – blurfus Dec 16 '16 at 06:24
  • Ah, it seems that, just like @ScanQR suggested, you can only use `POST` or `GET` in your HTML forms - if you use anything other those two, the default method used is `GET` see - http://stackoverflow.com/a/8054444/600486 – blurfus Dec 16 '16 at 06:33
  • Possible duplicate of [Using PUT method in HTML form](http://stackoverflow.com/questions/8054165/using-put-method-in-html-form) – blurfus Dec 16 '16 at 06:37
  • Can you post the network tracking ? – chaoluo Dec 16 '16 at 06:41
  • I tested it on chrome and safari, something is not our expect on safari, detail see my answer. – chaoluo Dec 16 '16 at 14:17
  • Can you show us the rest of the web.xml config? – rhinds Dec 16 '16 at 15:06

1 Answers1

2

You need to fix your method in Controller,

Your controller API is configured for PUT,

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

So instead make it,

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

Because, HTML form only valid for method POST or GET

ScanQR
  • 3,740
  • 1
  • 13
  • 30
  • Then I get HTTP Status 405 - Request method 'GET' not supported.Maybe – lhw1995 Dec 16 '16 at 06:10
  • Maybe the reason is using tomcat7.0? – lhw1995 Dec 16 '16 at 06:12
  • No HTML form method only valid for POST and GET methods. If you make method="put" it simple means method not defined and GET is considered. I have updated my answer. If you dont want to change your controller then you need to use ajax and not the form submit – ScanQR Dec 16 '16 at 06:13
  • But springMVC can handle RequestMethod 'PUT',when there is input type="hidden" name='_method' value ='put' – lhw1995 Dec 16 '16 at 06:42
  • and your form method is POST or GET? – ScanQR Dec 16 '16 at 06:46
  • it's post,see phihag'answer in stackoverflow.com/a/8054444/600486 – lhw1995 Dec 16 '16 at 06:52
  • Yes so it says same thing I mentioned, PUT with FORM is not valid and if you use PUT then do ajax only. I suggest to alter controller to POST. – ScanQR Dec 16 '16 at 06:55
  • But I want to use PUT in controller,because in RESTful,PUT usually be used to update and POST usually be used to insert. – lhw1995 Dec 16 '16 at 07:00
  • I completely agree with you. I too use same convention but your limitation lies on client side while using form. I use jQuery ajax! – ScanQR Dec 16 '16 at 07:01
  • if this solves your approach issue then accept answer and close the question. – ScanQR Dec 16 '16 at 09:52
  • I think is more "rest" to use POST without Ajax than PUT with Ajax, see http://stage.vambenepe.com/archives/1801 http://www.elasticvapor.com/2010/06/dichotomy-of-ajax-and-restful-apis.html – Testo Testini Dec 16 '16 at 22:21