Hello, when I try to change the record, I get an error:
tomcat - HTTP Status [400] – [Bad Request]

  WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "update" WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Handler execution resulted in exception: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "update" 

update.jsp:

  <form:form action="update" method="post" commandName="word"> <table> <tr> <td><form:label path="russWord">Русское Слово </form:label></td> <td><form:input path="russWord"/></td> </tr> <tr> <td><form:label path="englishWord">Английское Слово </form:label></td> <td><form:input path="englishWord"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="Ok" onclick="/Words/" /></td> </tr> </table> </form:form> 

Controller:

 @RequestMapping(value = "/update", method = RequestMethod.POST) public void update(Word word) { service.updateWord(word); } @RequestMapping(value = "/updateWord/{id}") public String updateWord(@PathVariable("id")int id, Model model) { Word word = service.getForId(id); model.addAttribute("word", word); return "update"; } 

Word class:

  @Entity @Table(name = "words") public class Word { @Id @Column(name = "WordID") @GeneratedValue(strategy = GenerationType.AUTO) private int wordId; @Column(name = "russWord") private String russWord; @Column(name = "englishWord") private String englishWord; //getters and setters } 

What am I doing wrong?

  • What, no idea anyone? - user254933

1 answer 1

spring tries to convert the object from the request to Word in the method

  public void update(Word word) 

In the request, everything comes as a string, and only at the level of the spring is the conversion into the necessary types. Judging by

type 'java.lang.String' to required type 'int'

some field has an int type primitive, it is better to use Integer, because Integer can be null, and int if the value is not defined, then the default is 0 and if 0 is passed, how to distinguish it from the fact that nothing has been transferred ...

There are more options like that .