Issue
i am a beginner of spring boot. while i doing the login form ran ito the problem with unable to load the userrole in to the dropdown list. data will coming from mysql database.what i tried so far i attached below along with the screen shot. i want to load the user role in to Dropdownlist. enter image description here
Controller
@GetMapping("/")
public String lists(Model model) {
List<User> liststudent = userService.listAll();
model.addAttribute("liststudent", liststudent);
return "login";
}
Service
public List<User> listAll()
{
return repo.findAll();
}
UserClass
@Entity
@Table(name="login")
public class User {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String role;
public User() {
}
public User(Long id, String username, String password, String role) {
this.id = id;
this.username = username;
this.password = password;
this.role = role;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
Repository
@Repository
public interface UserRepository extends JpaRepository<User, Long>{
User findByUsernameAndPassword(String username, String password);
}
Login Form
<tr>
<td>Role</td>
<td>
<select name="liststudents">
<option th:each="liststudents : ${liststudent}"
th:text="${liststudents.role}"
/>
</select>
</td>
</tr>
Database Screen Shot enter image description here
Solution
In your case you're trying to get a list of all users, whereas you wrote that you just need a list of roles. The best way to solve it is to create an entity of Roles and get a list of them from the repository.
Answered By - Valery