我不明白为什么当我尝试在 spring 中使用 modelAttributed 表单时,它试图将我的 String 类型变量转换为 Long 类型变量,而它应该只保留 String 类型。我怀疑它试图做的唯一一件事就是填充 Id 变量。
无法将“java.lang.String”类型的值转换为所需类型“javaSpring.DojoOverflow.models.Questions”;嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [java.lang.Long] 的值“为什么?”;嵌套异常是 java.lang.NumberFormatException: For input string: "Why?"
//---------------------------------------------------
// My Model
@Entity
@Table(name="questions")
public class Questions {
// Attributes
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty(message = "Ask a question!")
private String question;
@Column(updatable=false)
private Date createdAt;
private Date updatedAt;
@OneToMany(mappedBy="question", fetch = FetchType.LAZY)
private List<Answer> answers;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable (
name="questions_tags",
joinColumns = @JoinColumn(name="question_id"),
inverseJoinColumns = @JoinColumn(name="tag_id")
)
@Size (max=3)
@NotEmpty(message="Tag your question!")
private List<Tag> tags;
// -------------------------------------
// My Controller Mapping
@PostMapping("/questions/new")
public String processQuestion(@Valid @ModelAttribute("question")Questions question, BindingResult result) {
if(result.hasErrors()) {
return "newQuestion.jsp";
}
questionService.createQuestion(question);
return "redirect:/";
}
//-----------------------------------------------
// My jsp
<body>
<div class="container">
<h1>What is your question?</h1>
<form:form action="/questions/new" method="post" modelAttribute="question">
<div class="form-group">
<form:label path="question">Question</form:label>
<form:textarea rows="5" class="question form-control" path="question"/>
<span class="error"><form:errors path="question"/></span>
</div>
</form:form>
<a href="/questions">Go Back</a>
</div>
</body>
料青山看我应如是
慕斯709654
相关分类