使用胸腺和春季靴子填充列表的下拉菜单
我正在尝试使用Spring Boot和Thymeleaf添加来自数据库中数据的HTML选择。我无法理解我做错了什么。 这是连接到此问题的代码:
我的模型:
@Entity // This tells Hibernate to make a table out of this class
@Table(name = "Skill")
public class Skill {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="skillID")
private int skillID;
@Column(name="skillname")
private String skillname;
@OneToMany(mappedBy = "skill")
private Collection<UserSkill> UserSkills = new ArrayList<>();
public Skill() {
}
// constructors
public Skill(int skillID, String skillname, Collection<UserSkill> userSkills) {
this.skillID = skillID;
this.skillname = skillname;
this.UserSkills = userSkills;
}
// getter and setter
public int getSkillID() {
return skillID;
}
public void setSkillID(int skillID) {
this.skillID = skillID;
}
public String getSkillname() {
return skillname;
}
public void setSkillname(String skillname) {
this.skillname = skillname;
}
public Collection<UserSkill> getUserSkills() {
return UserSkills;
}
public void setUserSkills(Collection<UserSkill> userSkills) {
UserSkills = userSkills;
}
@Override
public String toString() {
return "Skill [skillID=" + skillID + ", skillname=" + skillname + ", UserSkills=" + UserSkills + "]"; }
}
我的控制器:
@Controller
public class SkillController {
@Autowired
private SkillService skillService;
public SkillController(SkillService skillService) {
this.skillService = skillService;
}
@GetMapping("/skill")
public String skill(Model theModel) {
Skill theSkill = new Skill();
// add existing users to the spring model
theModel.addAttribute("skills", skillService.getSkills());
return "skill";
}
我的html thymeleaf(view):
<div class="form-group col-md-8">
<label class="col-form-label">Category </label>
<select id="category" name="category" th:field="*{skill.skillID}" >
<option th:each="skill : ${skills}" th:value="${skill.skillID}" th:utext="${skill.skillname}"/>
</select>
</div>
我的仓库:
@Repository
public interface SkillRepository extends JpaRepository<Skill, Integer> {
Skill findSkillBySkillname(String skillname);
}
我的服务(接口):
@Service
public interface SkillService {
Skill getSkill(String skillname);
Iterable<Skill>getSkills();
Skill findBySkillId(int theSkillID);
}
我的serviceimpl(interface):
@Service
public class SkillServiceImpl implements SkillService {
private final SkillRepository skillRepository;
/*
* @Autowired SkillRepository skillRepository;
*/
@Autowired
public SkillServiceImpl(SkillRepository theSkillRepository) {
skillRepository = theSkillRepository;
}
@Override
public Iterable<Skill> getSkills() {
System.out.println("Abrufen aller Skills.");
return skillRepository.findAll();
}
@Override
public Skill getSkill(String skillname) {
System.out.println("Abrufen des Skills "+ skillname);
return skillRepository.findSkillBySkillname(skillname);
}
@Override
public Skill findBySkillId(int theSkillID) {
Optional<Skill> result = skillRepository.findById(theSkillID);
Skill theSkill = null;
if (result.isPresent()) {
theSkill = result.get();
}
else {
// we didn't find the vaccination state
throw new RuntimeException("Did not find user id - " + theSkillID);
}
return theSkill;
}
我得到此错误:
ERROR 94396 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/webapp] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/skill.html]")] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'skill' available as request attribute
I'm trying to add an HTML select with data from a Database using Spring Boot and Thymeleaf. I can't get it what I'm doing wrong.
This is the Code that is connected to this Problem:
My Model:
@Entity // This tells Hibernate to make a table out of this class
@Table(name = "Skill")
public class Skill {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="skillID")
private int skillID;
@Column(name="skillname")
private String skillname;
@OneToMany(mappedBy = "skill")
private Collection<UserSkill> UserSkills = new ArrayList<>();
public Skill() {
}
// constructors
public Skill(int skillID, String skillname, Collection<UserSkill> userSkills) {
this.skillID = skillID;
this.skillname = skillname;
this.UserSkills = userSkills;
}
// getter and setter
public int getSkillID() {
return skillID;
}
public void setSkillID(int skillID) {
this.skillID = skillID;
}
public String getSkillname() {
return skillname;
}
public void setSkillname(String skillname) {
this.skillname = skillname;
}
public Collection<UserSkill> getUserSkills() {
return UserSkills;
}
public void setUserSkills(Collection<UserSkill> userSkills) {
UserSkills = userSkills;
}
@Override
public String toString() {
return "Skill [skillID=" + skillID + ", skillname=" + skillname + ", UserSkills=" + UserSkills + "]"; }
}
My Controller:
@Controller
public class SkillController {
@Autowired
private SkillService skillService;
public SkillController(SkillService skillService) {
this.skillService = skillService;
}
@GetMapping("/skill")
public String skill(Model theModel) {
Skill theSkill = new Skill();
// add existing users to the spring model
theModel.addAttribute("skills", skillService.getSkills());
return "skill";
}
My HTML Thymeleaf (View):
<div class="form-group col-md-8">
<label class="col-form-label">Category </label>
<select id="category" name="category" th:field="*{skill.skillID}" >
<option th:each="skill : ${skills}" th:value="${skill.skillID}" th:utext="${skill.skillname}"/>
</select>
</div>
My Repo:
@Repository
public interface SkillRepository extends JpaRepository<Skill, Integer> {
Skill findSkillBySkillname(String skillname);
}
My Service (Interface):
@Service
public interface SkillService {
Skill getSkill(String skillname);
Iterable<Skill>getSkills();
Skill findBySkillId(int theSkillID);
}
My ServiceImpl (Interface):
@Service
public class SkillServiceImpl implements SkillService {
private final SkillRepository skillRepository;
/*
* @Autowired SkillRepository skillRepository;
*/
@Autowired
public SkillServiceImpl(SkillRepository theSkillRepository) {
skillRepository = theSkillRepository;
}
@Override
public Iterable<Skill> getSkills() {
System.out.println("Abrufen aller Skills.");
return skillRepository.findAll();
}
@Override
public Skill getSkill(String skillname) {
System.out.println("Abrufen des Skills "+ skillname);
return skillRepository.findSkillBySkillname(skillname);
}
@Override
public Skill findBySkillId(int theSkillID) {
Optional<Skill> result = skillRepository.findById(theSkillID);
Skill theSkill = null;
if (result.isPresent()) {
theSkill = result.get();
}
else {
// we didn't find the vaccination state
throw new RuntimeException("Did not find user id - " + theSkillID);
}
return theSkill;
}
and I get this Error:
ERROR 94396 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/webapp] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/skill.html]")] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'skill' available as request attribute
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的控制器中,将 theskill 添加到模型中。您创建了它,但没有将其添加到模型中。错误表明您正在引用表格备份对象,但模型中不存在。
In your controller add theSkill to your model. You created it but didn't add it to the model. The error indicates you are making references to the form backing object but it doesn't exist in your model.