在Jsp Struts2中调用Action类的函数 [英] Calling function of Action class in Jsp Struts2

查看:105
本文介绍了在Jsp Struts2中调用Action类的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点情景。我有两个POJO类和两个表用户(表的名称相同)。每个用户都属于一个且只有一个域。

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.

我有两个Action类,一个是 UsersManagemntAction ,另一个是 DomainsManagementAaction 。我使用 UsersManagemntAction 来执行与用户相关的CRUD操作。在我的用户类中,我有一个属性 domainId 。此属性将包含用户所属的 id 。我的问题是,当我在jsp页面中显示用户信息时,我会显示带有用户信息的 domainId 。这是因为用户对象将具有 domainId 。而不是显示 domainId 我想显示域名。我无法执行连接查询。我应该解决这个问题,当我显示用户信息时,我在用户管理动作类中调用一个函数,将 domainId 传递给该函数。该函数在 Domain 表上执行搜索并返回域名。此解决方案无效,因为我没有找到任何方法将 domainId 传递给该函数。我可以调用 UsersManagemntAction 类的函数,但无法传递 domainId 。请帮助我或建议我一个替代解决方案。

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.

下面是JSP页面的代码和用户类。

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;
}

}


推荐答案

您可以从值堆栈中获取它,同时在值堆栈顶部迭代 User 对象,这样您就可以在那里获得它

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();
}

这篇关于在Jsp Struts2中调用Action类的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆