【问题标题】:Struts2 Populate form bean value from an Object located in the SessionStruts2 从位于会话中的对象填充表单 bean 值
【发布时间】:2013-01-20 09:53:07
【问题描述】:

我想更新我的数据库中的一个表,但在此之前我会向用户显示他的当前信息。现在我有了这个表单bean。

我的动作类中有一个名为的表单 bean。

public void setProfileBean(UserProfile profileBean) {
    this.profileBean = profileBean;
}

public UserProfile getProfile() {
    return profileBean;
}

我希望首先从会话范围内的另一个 bean 填充它的值。我将如何使用模型驱动的界面来实现这一点?

【问题讨论】:

  • 这不是bean属性,你想得到什么?
  • @Roman C 我已经更新了
  • 为什么不在返回结果之前填充到action中?
  • 使用模型驱动的界面,您在操作中不需要此属性。见how Struts2 ModelDriven interface works

标签: java struts2 javabeans model-driven


【解决方案1】:

正如@roman 建议的那样,Struts2 ModelDriven 接口可以很好地解决这种情况,有关如何使用它的一些代码帮助,您可以参考This 链接

【讨论】:

    【解决方案2】:

    首先,为什么你需要使用模型驱动接口,在你的情况下,你可以简单地使用动作支持在你的视图上渲染 bean。你必须这样:

    1:首先,您应该在要更新之前填充视图,我的意思是,当您被重定向到您要从中更新数据库的页面时,您应该在视图中填充数据。

    2:您可以通过对操作进行一些更改来实现此目的:

    在你的行动中

    public void setProfileBean(UserProfile profileBean) {
        this.profileBean = profileBean;
    }
    
    public UserProfile getProfile() {
        return profileBean;
    }
    
    **i assume this is your target method**
    
    public string execute()
    {
    UserProfile user=new UserProfile();
    
    user.setProfileBean("whatever you want to set");
    ......
    ......
    
    return SUCCESS;
    
    }
    

    就是这样,struts2 会自动在你的视图中填充数据,确保你已经定义了这样的属性:

    <s:property value="UserProfile .xyz">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多