【发布时间】:2021-06-10 06:05:49
【问题描述】:
我是弹簧靴的初学者。当我做登录表单时,遇到了无法将用户角色加载到下拉列表的问题。数据将来自 mysql 数据库。到目前为止,我在下面附上了屏幕截图。 我想将用户角色加载到下拉列表中。 enter image description here
控制器
@GetMapping("/")
public String lists(Model model) {
List<User> liststudent = userService.listAll();
model.addAttribute("liststudent", liststudent);
return "login";
}
服务
public List<User> listAll()
{
return repo.findAll();
}
用户类
@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
public interface UserRepository extends JpaRepository<User, Long>{
User findByUsernameAndPassword(String username, String password);
}
登录表单
<tr>
<td>Role</td>
<td>
<select name="liststudents">
<option th:each="liststudents : ${liststudent}"
th:text="${liststudents.role}"
/>
</select>
</td>
</tr>
数据库屏幕截图 enter image description here
【问题讨论】:
标签: spring spring-boot