I'm trying to post a text from home.html template
<form th:action="@{/process_addText}" th:object="${textzz}" method="post" >
<input type="text" th:field="*{text}" />
<button type="submit" >Add</button>
</form>
and here is my controller
@PostMapping("/process_addText")
public String processAddText(Text text1) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName();
User myUser=userRepo.findByEmail(name);
text1.setUser(myUser);
textRepo.save(text1);
return "redirect:/home";
}
@GetMapping("/home")
public String mySuccess(Model model) {
model.addAttribute("textzz",new Text());
LOGGER.info("verif===" model.toString());
return "home";
}
And it's my Text class:
@Entity
@Table(name = "texts")
public class Text {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idText;
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "id")
private User user;
private String text;
}
when I'm trying to post the "text" value from home.html,I'm getting this error:
WARN 680 --- [nio-8088-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.myblog.app.model.Text'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'qvefd'; nested exception is java.lang.NumberFormatException: For input string: "qvefd"]
I have no idea why I'm getting this, because the types are correct
Update: When I remove the input and I post(without text) in my DB I get correct rows(for text id and User's foreign key), of course with text value=NULL. So the problem is in type of input, maybe.
CodePudding user response:
When your POST request is fired the only information that gets to Spring is the value of the input(in this situation "qvefd"). The problem is that your Text class expects not only text, but also idText and user. Spring automatically tries to map the given value("qvefd") to the Text class and therefore tries to fill the idText field first, but fails to do so as the types don't match. The solution would be to replace the argument Text with a POJO whose only field is a String named "text"(the same as the name of the input tag).
CodePudding user response:
Your controller method accepts a Text entity but your frontend form sends with post request only a simple String in the body of the request.
Then Spring can not transform that String into a Text object.
So your controller method should be
@PostMapping("/process_addText")
public String processAddText(String text) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName();
User myUser=userRepo.findByEmail(name);
Text text1 = textRepo.findByUser(myUser);
if (text1 == null){
text1 = new Text();
text1.setUser(myUser);
}
text1.setText(text);
textRepo.save(text1);
return "redirect:/home";
}
