【问题标题】:DropDrown List not populating data in Spring Boot Mysql下拉列表未在 Spring Boot Mysql 中填充数据
【发布时间】: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


    【解决方案1】:

    在您的情况下,您试图获取所有用户的列表,而您写道您只需要角色列表。解决它的最佳方法是创建一个角色实体并从存储库中获取它们的列表。

    【讨论】:

    • 如何编写代码先生。我不知道该怎么做。
    • 我还附上了数据库截图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多