无法将提交按钮映射到" / submit" api叫在百里香

时间:2017-09-25 10:24:03

标签: javascript java html5 thymeleaf

这是我在Spring控制器中的Web服务方法:

@RequestMapping(value = "/submit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public @ResponseBody Response submitAppication(@RequestBody String submitString) {
    System.out.println("***************************  Entering for submit part ********************************");
    Response response = Response.status(400).build();
    try {
        JSONObject jsonObject = new JSONObject(submitString);
        jsonObject.put("event", "submit");

        SMEvent sme = new SMEvent(HttpMethod.POST, jsonObject);

        String tid = Util.getTid(jsonObject);
        response = handleEvent(sme, getVerificationType(tid), tid);
    } catch (JSONException e) {
        //TODO log some exception here
    } finally {
        return response;
    }
}

这是我的HTML页面,其格式为:

<div th:if="${InstaPanState==1 and InstaPanStateStatus==0}">
    <body>
    <form action="#" th:action="@{/submit}" method="post">

        <label><b>Name</b></label>
        <input type="text" placeholder="Your Name" name="name"/><br/><br/>
        <br/>
        <label><b>Father's Name</b></label>
        <input type="text" placeholder="Father's Name" name="fname"/><br/>
        <br/><br/>
        <label><b>Contact Number</b></label>
        <input type="number" placeholder="Contact Number"/><br/><br/><br/>

        <div class="clearfix">
            <button type="button" class="cancelbtn">Cancel</button>
            <button type="submit" class="submitbtn">Submit</button>
        </div>
    </form>
    </body>
</div>

当我点击提交按钮时,它应该调用提到的Web服务方法。

目前我收到以下异常,并且没有调用Web服务。

  

查找路径/提交的处理程序方法         2017-09-25 15:41:36.189 DEBUG 5405 --- [nio-8080-exec-4]         .m.m.a.ExceptionHandlerExceptionResolver:解决来自的异常          handler [null]:         org.springframework.web.HttpMediaTypeNotSupportedException:Content         键入&#39; application / x-www-form-urlencoded&#39;不支持

2 个答案:

答案 0 :(得分:1)

您正在发布HTML表单,但Web服务已配置为使用JSON。

将网络服务方法更改为:

@RequestMapping(value = "/submit", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public @ResponseBody Response submitAppication(
          @RequestBody MultiValueMap<String, String> formParams) {
    // TODO: Extraction logic here
}

答案 1 :(得分:-1)

请尝试以下方法。

  @PostMapping(value = "/submit")
        public ResponseEntity<UserSetail> createUser( @RequestBody UserDetail user) 
        {
    new ResponseEntity<UserSetail>(user, HttpStatus.CREATED);
        }