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

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

问题描述

我有一个小场景.我有两个 POJO 类和两个表 UserDomain(表的名称相同).每个用户将属于一个且仅一个域.

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 操作.在我的 User 类中,我有一个属性 domainId.该属性将包含用户所属的 Domainid.我的问题是,当我在 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页面和User类的代码.

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天全站免登陆