【问题标题】:Why am not getting some values of bean object?为什么没有得到 bean 对象的一些值?
【发布时间】:2012-09-17 12:08:10
【问题描述】:

在struts2 中实现了编辑功能。单击提交按钮后,我在 JSP 中显示的 bean 值在动作类中正确获取。

但是我在 JSP 中没有提到的其他 bean 值返回 null。

如果我在 JSP 中显示 bean 的所有值,那么我可以在 Action 中获取所有值。

这是解决此问题的方法。否则,还有其他办法。

Action类的代码是

 UserForm userForm = new UserForm();

public String edit(){
    String result = ActionSupport.ERROR;
    HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
        HttpSession session = request.getSession(false);
        if (null != session
                && null != (UserAccount) session.getAttribute(USER)) {
            String editUser = (String) request
            .getParameter(RequestAttributes.EDIT_USER);
            UserAccount userAccount = userForm.getUserAccount();
        if (null != editUser) {
                    //invoked when edit user page is submitted
            userUtils.updateUserAccount(userAccount);

        } else {
                    // invoked when edit user page gets loaded
            String userAccSID = (String) request
                    .getParameter(USER_ACC_SID);
            String roleSID = (String) request.getParameter(ROLE_SID);
            if (null != userAccSID && null != roleSID) {
                Long userAccSIDVal = Long.valueOf(userAccSID);
                Long roleSIDVal = Long.valueOf(roleSID);
                userAccount = userUtils
                        .loadUserAccount(userAccSIDVal);
                userForm.setUserAccount(userAccount);
            }
        }
    } 

    return result;
}

public UserForm getUserForm() {
    return userForm;
}

public void setUserForm(UserForm userForm) {
    this.userForm = userForm;
}

JSP页面的代码是

<s:form action="edit?editUser=edit">
<table align="center">
                    <s:hidden name="userForm.userAccount.createdBy"/>
        <tr align="center">
            <th>Edit User</th>
        </tr>
        <tr>
            <td><s:textfield name="userForm.userAccount.firstName" label="First Name"/></td>
        </tr>
        <tr>
            <td><s:textfield name="userForm.userAccount.lastName" label="Last Name"/></td>
        </tr>

        <tr>
            <td><s:submit value="Save" /><s:reset value="Cancel" /></td>
        </tr>
</table>

现在,如果我将 createdBy 隐藏,那么我可以在 Action 中获取 createdBy 的值。 它的值已经由动作类设置。 那为什么还要在jsp页面中设置呢?

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 如果没有特定的代码/错误消息/堆栈跟踪/值,几乎不可能给你建议。

标签: jsp struts2


【解决方案1】:

如果您没有从 JSP 返回值,那么它们如何在表单提交时提供给 Action 类。 一种解决方案是创建隐藏字段并设置您不想在 JSP 页面上向用户显示的值,这样当您点击提交按钮时,这些值将被提交给操作。

其他选项是将数据存储在 Session 中或在您的操作类中获取值,但在我们没有其他选择之前,它们不是可取的解决方案。

【讨论】:

  • 谢谢 Umesh。正如你所说,我试过了,它给了我价值观。但是我在struts1.2中完成了相同的任务,没有使用隐藏值,并且我在表单提交后获得了Action类中的所有值。
  • @Abinaya:我不太确定 Struts1,但概念和架构不同,因为有不同的 form-Bean,而 S2 不是这种情况。
  • 是的..我要使用隐藏值
猜你喜欢
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 2021-03-29
相关资源
最近更新 更多