我试图显示H2数据库中的用户列表。但是,结果是应该调用表中用户属性而不是数据库中用户详细信息的确切脚本。没有错误。
请帮忙,因为经过数天的研究和重新编码后我不知道该怎么办,但没有成功。
表中的ACTUAL结果:
我的观点:
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Status</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="@{user.id}" scope="row"></td>
<td th:text="@{user.name}"></td>
<td th:text="@{user.lastname}"></td>
<td th:text="@{user.email}"></td>
<td th:text="@{user.active}"></td>
<td>
<a th:href="@{/edit/{id}(id=${user.id})}" class="btn btn-primary"> <i class="fas fa-user-edit ml-2"></i>
</a>
</td>
<td>
<a th:href="@{/delete/{id}(id=${user.id})}" class="btn btn-primary"> <i class="fas fa-user-times ml-2"></i>
</a>
</td>
</tr>
</tbody>
</table>
我的模型(实体):
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import org.hibernate.validator.constraints.Length;
import groovy.transform.builder.Builder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "USER")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID")
private int id;
@Column(name = "EMAIL")
@Email(message = "*Please provide a valid Email")
@NotEmpty(message = "*Please provide an email")
private String email;
@Column(name = "PASSWORD")
@Length(min = 5, message = "*Your password must have at least 5 characters")
@NotEmpty(message = "*Please provide your password")
private String password;
@Column(name = "NAME")
@NotEmpty(message = "*Please provide your name")
private String name;
@Column(name = "LAST_NAME")
@NotEmpty(message = "*Please provide your last name")
private String lastName;
@Column(name = "ACTIVE")
private int active;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "USER_ROLE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
private Set<Role> roles;
}
我的UserService:
import java.util.List;
import com.GentelellaAlela.Authorization.Entity.User;
public interface UserService {
public User findUserByEmail(String email) ;
public List<User> getAllUsers();
public User saveUser(User user);
User updateUser(User user);
User getUserById(int userId);
void deleteUser(int id);
}
我的UserServiceImpl:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import com.GentelellaAlela.Authorization.Configuration.ResourceNotFoundException;
import com.GentelellaAlela.Authorization.Entity.Role;
import com.GentelellaAlela.Authorization.Entity.User;
import com.GentelellaAlela.Authorization.Repository.RoleRepository;
import com.GentelellaAlela.Authorization.Repository.UserRepository;
@Service
public class UserServiceImpl implements UserService{
private UserRepository userRepository;
private RoleRepository roleRepository;
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
public UserServiceImpl(UserRepository userRepository, RoleRepository roleRepository, BCryptPasswordEncoder bCryptPasswordEncoder) {
this.userRepository = userRepository;
this.roleRepository = roleRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
@Override
public User findUserByEmail(String email) {
return userRepository.findByEmail(email);
}
@Override
public User saveUser(User user) {
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
user.setActive(1);
Role userRole = roleRepository.findByRole("ADMIN");
user.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
return userRepository.save(user);
}
@Override
public List<User> getAllUsers() {
List<User> result = (List<User>) userRepository.findAll();
if(result.size() > 0) {
return result;
} else {
return new ArrayList<User>();
}
}
}
我的控制器:
@Controller
@RequestMapping("/admin/")
public class AdminController {
@Autowired
private UserService userService;
@GetMapping("list")
public String getAllUsers(Model model)
{
List<User> users = userService.getAllUsers();
model.addAttribute("users", users);
return "admin/listUser";
}
}
答案 0 :(得分:0)
使用这种方式<td th:text="${user.id}" scope="row"></td>