1

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.

Himanshu Mishra
  • 100
  • 2
  • 10
  • May be useful http://stackoverflow.com/questions/3889248/java-getting-the-enum-name-given-the-enum-value – Diogo Moreira Oct 14 '16 at 17:20
  • you might try to define a method in your model called getStateName and return the enum.name method. Then from jsp use user.stateName? I am not familiar with jsp but with spring this would work. – alan7678 Oct 14 '16 at 17:26
  • @ alan7678 Then how can I access the value return by this method in JSP using EL ? – Himanshu Mishra Oct 14 '16 at 17:31
  • How about adding a stateName private field to your User object, then right before sending it to the JSP page for rendering, you set the stateName with the proper String value from the state Integer? Oh, and the stateName field would not be persisting to the database, i.e wont be managed by the Hibernate session – Mechkov Oct 14 '16 at 17:37
  • @Mechkov If add a private field in User class Hibernate try to find it in DB, which is not there so exception triggered. – Himanshu Mishra Oct 14 '16 at 17:38
  • Dont annotate it. – Mechkov Oct 14 '16 at 17:39
  • To be honest, you would never want to send your domain model object to any UI. YOu would use a Transfer object, which is a proxy of the domain model, with only the needed fields. On that Proxy/Transfer object, i would place the state name. – Mechkov Oct 14 '16 at 17:40
  • @Mechkov Can you please explain in more detail, i tried it but did not found any annotation or other way round – Himanshu Mishra Oct 14 '16 at 17:46
  • @HimanshuMishra posted an answer to your question. Hope it helps – Mechkov Oct 14 '16 at 17:54

1 Answers1

0

Here is one way to make it work. In your User class, add @Transient instance variable called stateName. @Transient means that Hibernate/JPA will NOT try to persist this field in the database! You would only use it for your UI/JSP purposes.

@Entity
@Table(name = "USERS")
public class User {    

    @Column(name = "STATE")
    private int state=State.ACTIVE.getState();

    @Transient
    private String stateName;

So, when a User object gets populated from the DB, you would want to populate the stateName field with the correct STATE from your enum.

Something like:

user.setStateName(/*get the STATE by the INTEGER id*/)

Then, you can render it in the the JSP page like:

<td>${user.state}</td>
<td>${user.stateName}</td>

Hope this help!

RECOMMENDATION

Never send your Domain model to the UI, always use a Proxy/Transfer object for UI purposes.

Mechkov
  • 4,294
  • 1
  • 17
  • 25