'Populate a drop down with a list using Thymeleaf and Spring Boot
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
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lee Greiner |
