3

I have a little scenario. I have two POJO classes and two tables User and Domain(same name for tables). Every user will belong to one and only one domain.

I have two Action classes one is UsersManagemntAction and other is DomainsManagementAaction. I use UsersManagemntAction to perform CRUD operations that are related to users. In my User class I have an attribute domainId. This attribute will contain the id of Domain to which user belong. My problem is that I when I show user information in jsp page I show the domainId with the users information. This is because user object will have the domainId. Rather than showing domainId I want to show domain name. I can't perform join query. what i supposed to solve this problem that when I am displaying information of user I call a function in user management action class pass the domainId to that function. That function perform search on Domain table and return the domain name. This solutions is not working because I didn't find any way to pass domainId to that function. I am able to call a function of UsersManagemntAction class but unable to pass domainId. Please help me or otherwise suggest me an alternative solution.

Below is the code of JSP page and User class.

JSP:

<s:if test="users.size() > 0">
<tbody>
    <s:iterator value="users" >
        <tr>
            <td><s:property   value="userId" /></td>
            <td><s:property   value="loginId" /></td>
            <td><s:property   value="password" /></td>

<td><s:property value="email" /></td>
<td><s:property value="domainName" /></td> <!--- It will call getDomainName function in   action class -->
</td>
</tr>
</s:iterator>
</tbody>

User.java:

public class User {
private Long userId;
private String loginId;
private String password;
private String email;
private Long domainId;

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public Long getDomainId() {
    return domainId;
}

public void setDomainId(Long domainId) {
    this.domainId = domainId;
}



public void setUserId(Long userId) {
    this.userId = userId;
}

public Long getUserId() {
    return userId;
}

@Override
public String toString() {
    return "User [domainId=" + domainId + ", password=" + password + ", userId=" + userId + ", Login Id=" + getLoginId() + "]";
}

public String getLoginId() {
    return loginId;
}

public void setLoginId(String loginId) {
    this.loginId = loginId;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Waqas Ali
  • 1,642
  • 4
  • 32
  • 55

1 Answers1

3

You can get it from the value stack, while you are iterating the User object in on top of the value stack, so you can get it there

public String getDomainName(){
  User user = (User) ActionContext.getContext().getValueStack().peek();
  return domainService.findDomainById(user.getDomainId()).getName();
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Woah, this is nice... +1 – Andrea Ligios Jan 17 '14 at 10:09
  • @Roman Thanks for your answer. I also have another question about my approach that If I have thousands user to display and then If i call this function and use domainService class to perform search operation on database for every single user then it will become problematic in term of optimal solution? Can you suggest another approach or this one is right? – Waqas Ali Jan 17 '14 at 19:18
  • @WaqasAli If you are doing search by id it should return result immediately because id is a primary key in the database which has a unique index that is optimized to retrieve a record. Retrieving users should be optimized to apply a search condition that limits returned results and doesn't perform full table scan. All of this techniques are used at the persistence layer. On the presentation and service layers you could use a pagination technique. – Roman C Jan 17 '14 at 19:54