spring我在表单中输入字符串,但得到java。lang.NumberFormatException:SpringBoot hibernate Java应用程序
我需要帮助。我花了两个小时,什么也找不到
问题是:我正在以html格式在技能和熟练程度字段中输入文本。模型采用字符串。但我得到的错误是:无法转换为数字
这就是我得到的错误:
Failed to bind request element:
org.springframework.beans.TypeMismatchException: Failed to convert value of
type 'java.lang.String' to required type 'com.byAJ.persistence.models.Skills';
nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to convert
from type [java.lang.String] to type [java.lang.Long] for value 'Designing
engines'; nested exception is java.lang.NumberFormatException: For input
string: "Designingengines"
2017-07-09 15:48:57.233 WARN 16312 --- [nio-8080-exec-9]
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by
Handler execution: org.springframework.beans.TypeMismatchException: Failed to
convert value of type 'java.lang.String' to required type
'com.byAJ.persistence.models.Skills'; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to convert
from type [java.lang.String] to type [java.lang.Long] for value 'Designing
engines'; nested exception is java.lang.NumberFormatException: For input
string: "Designingengines"
HTML页面:
<form autocomplete="off" action="#" th:action="@{/skill}"
th:object="${skill}" method="post">
<div class="form-group">
<!-- <label for="degree">Degree <mark><strong><span th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}">First Name Error</span></strong></mark></label>-->
<label for="skill">Skill<mark><strong><span th:if="${#fields.hasErrors('skill')}" th:errors="*{skill}">can't be empty</span></strong></mark></label>
<input type="text" class="form-control" id="skill" placeholder="Engineer" th:field="*{skill}" />
</div>
<div class="form-group">
<label for="proficiency">Proficiency( Beginner, Proficient, Expert) <mark><strong><span th:if="${#fields.hasErrors('proficiency')}" th:errors="*{proficiency}"> can't be empty</span></strong></mark></label>
<input type="text" class="form-control" id="proficiency" placeholder="Expert" th:field="*{proficiency}" />
</div>
<div class="form-group">
<p>Enter More Skills?</p>
<input type="radio" name="yesOrNo" value="yes"> Yes
<input type="radio" name="yesOrNo" value="no" checked> No
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
技能控制员:
@RequestMapping(value="/skill", method = RequestMethod.GET)
public String getSkill(Model model)
{
model.addAttribute("skill", new Skills());
return "skillForm";
}
@RequestMapping(value="/skill", method = RequestMethod.POST)
public String processSkil(@Valid @ModelAttribute("skill") Skills skill, BindingResult result, Model model, @RequestParam("yesOrNo") String yesNo){
System.out.println("helo");
System.out.println(result.toString());
if (result.hasErrors()) {
return "skillForm";
} else {
skill.setUsername(userService.getUserDetails().getUsername());
userService.saveSkill(skill);
}
if( yesNo.equals("yes")){
return "redirect:/skill";
}
return "myResume";
}
技能模式:
@Entity
public class Skills {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotEmpty
private String skill, proficiency;
private String username;
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
public String getProficiency() {
return proficiency;
}
public void setProficiency(String proficiency) {
this.proficiency = proficiency;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public long getId() {
return id;
}
}
最后,技能库积垢:
import com.byAJ.persistence.models.Skills;
import org.springframework.data.repository.CrudRepository;
public interface SkillRepository extends CrudRepository<Skills, Long>{
}
任何提示都非常感谢。谢谢
当我在数据库中输入一个值时,我不会尝试更新该值。这真的很让人困惑,因为我没有一个字段期望这些值是整数
# 1 楼答案
我注意到我对对象、字段和映射有相同的名称。所以我把它们改成了不同的名字,现在程序开始工作了