I am new to Spring and trying to populate user details in a table, the table will show some basic information of Users of system Structure of table is
Firstname Lastname Email Phone Number Role State
I am using Spring 4 and Hibernate 4.
My Model class
@Entity
@Table(name = "USERS")
public class User {
@Id
@Column(name = "USER_ID")
private String userId;
@Column(name = "PASSWORD")
private String password;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "MIDDLE_NAME")
private String middleName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "EMAIL")
private String email;
@Column(name = "PHONE")
private String phone;
@Column(name = "STATE")
private int state=State.ACTIVE.getState();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "USER_ROLE_MAP", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = {
@JoinColumn(name = "ROLE_ID") })
private Set<Role> blogRoles = new HashSet<>();
/*getters and setter and other utility methods are remove*/
My Controller method of user list
@RequestMapping(value = "/listUser", method = RequestMethod.GET)
public ModelAndView showAllUsers() {
System.out.println("start login");
ModelAndView modelAndView=new ModelAndView("listUser");
List<User> userList=userService.findAll();
System.out.println("user List controler "+userList);
modelAndView.addObject("currentUser", getPrincipal());
modelAndView.addObject("userList", userList);
return modelAndView;
}
My Jsp code to display the user list data
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.email}</td>
<td>${user.phone}</td>
<td>
<c:forEach items="${user.blogRoles}" var="role">
${role.roleType}
</c:forEach>
</td>
<td>${user.state}</td>
</tr>
</c:forEach>
Now I have a enum which has information about the state of user
public enum State {
ACTIVE(1), INACTIVE(2), LOCKED(3), DELETED(4);
private int state;
private State(int state) {
this.state = state;
}
public int getState() {
return state;
}
@Override
public String toString(){
return this.name();
}
public String getName(){
return this.name();
}
}
In JSP the state
comes as integer
I want to display the name
, the string value corresponding to the state
which is an integer injected by spring and fetched by hibernate from DB.
I know there are some ways to do it like I can create a map from enum and send it's object to JSP or defining my own custom TLD.
But I am still not satisfied with these kind solutions, is there any better way to display the value with some more standard way or using some features of spring and EL ?
Any suggestion and help will be help full, thanks.