绑定一个bean 属性的值,同时在字段中显示不同的文本

发布于 2024-12-24 22:57:12 字数 1511 浏览 1 评论 0原文

我知道这个标题可能听起来很荒谬,但我不知道如何解释我的意思:)

我有一个用于编辑实体的表单。 每个字段都链接到其相关的 bean 属性,并且工作正常。

我需要做的是在表单字段中显示实体的当前值,以便使编辑速度更快一些(使用当前值而不是空字段非常有用!)

所以我进行了此编辑。 xhtml 页面,该页面从 get 参数获取 ID,并使用支持 bean 中的 getFromId 方法检索实体。

假设这个实体存储在一个名为“contact”的变量中,我想要这种形式:

<f:view>
        <h:form>
            <h1><h:outputText value="Edit"/></h1>
            <h:panelGrid columns="2">
                <h:outputLabel value="Name:" for="name" />
                <h:inputText id="name" value="#{contactsMB.name}" title="Name" required="true"/>
                <h:outputLabel value="Surname:" for="surname" />
                <h:inputText id="surname" value="#{contactsMB.surname}" title="Cognome"/>
                <h:outputLabel value="Email:" for="email" />
                <h:inputText id="email" value="#{contactsMB.email}" title="Email" required="true"/>
                <h:outputLabel value="Number:" for="number" />
                <h:inputText id="number" value="#{contactsMB.number}" title="Number" />
                <h:inputHidden id="id" value="#{contactsMB.id}"/>
                <h:commandButton value="Edit" action="#{contactsMB.editContact}"/>
            </h:panelGrid>
        </h:form>
    </f:view>

但我想在文本字段中显示联系人变量的值。

因此,例如,第一个 inputText 必须显示“stefano”作为名称,但它必须链接到contactsMB.name。

基本上它就像 inputText 的默认值,并且该默认值必须来自实体。

这可能吗?

I know that the title may sound absurd, but I don't know ho to explain what I mean :)

I've got a form that I use to edit an entity.
Each field is linked to its related bean property and it works fine.

What I need to do is to show the current values of the entity in the form fields, in order to make editing a bit faster (having the current value instead of an empty field is quite useful!)

So I've got this edit.xhtml page, that takes the ID from a get parameter and retrieves the entity using a getFromId method in the backing bean.

Assuming that this entity is stored into a var called "contact", I'd like to have this form :

<f:view>
        <h:form>
            <h1><h:outputText value="Edit"/></h1>
            <h:panelGrid columns="2">
                <h:outputLabel value="Name:" for="name" />
                <h:inputText id="name" value="#{contactsMB.name}" title="Name" required="true"/>
                <h:outputLabel value="Surname:" for="surname" />
                <h:inputText id="surname" value="#{contactsMB.surname}" title="Cognome"/>
                <h:outputLabel value="Email:" for="email" />
                <h:inputText id="email" value="#{contactsMB.email}" title="Email" required="true"/>
                <h:outputLabel value="Number:" for="number" />
                <h:inputText id="number" value="#{contactsMB.number}" title="Number" />
                <h:inputHidden id="id" value="#{contactsMB.id}"/>
                <h:commandButton value="Edit" action="#{contactsMB.editContact}"/>
            </h:panelGrid>
        </h:form>
    </f:view>

but I want to show into the texfields the values of the contact var.

So, for example, the first inputText must show "stefano" as name but it has to be linked to contactsMB.name.

Basically it's just like a default value for the inputText, and this default value must come from an entity.

Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我三岁 2024-12-31 22:57:12

首先,您应该问自己:您真的需要这样做吗?。如果您正在编辑实体,则应该直接利用该实体的 getter 和 setter。它可能是这样的:

@ManagedBean
@ViewScoped
public void ContactsMB {

   @EJB
   private SomeEJB someEJB;
   @ManagedProperty(value = "#{param.username}")
   private String username;
   private User   user;

   @PostConstruct
   public void prepareEditing() {
      this.user = someEJB.loadUserFromDatabase(username);
   }

   // Getters and Setters
}

那么您可以,例如,像这样显示用户的名称:

<h:outputLabel value="Name:" for="name" />
<h:inputText id="name" value="#{contactsMB.user.name}" />

上面的 将显示用户的当前名称作为默认值。当您输入新名称并提交时,User 实体将直接更新。

如果您确实想按照您的解释进行操作,您可以将 @PostConstruct 方法更改为如下所示:

@ManagedBean
@ViewScoped
public void ContactsMB {

   @EJB
   private SomeEJB someEJB;
   @ManagedProperty(value = "#{param.username}")
   private String username;
   private User   user;

   private String name;
   private String email;

   @PostConstruct
   public void prepareEditing() {
      this.user  = someEJB.loadUserFromDatabase(username);
      this.name  = user.getName();
      this.email = user.getEmail();
   }

   // Getters and Setters
}

First of all, you should ask yourself: Do you really need to do that?. If you are editing an Entity, you should directly take advantage of the getters an setters of that Entity. It may be something like this:

@ManagedBean
@ViewScoped
public void ContactsMB {

   @EJB
   private SomeEJB someEJB;
   @ManagedProperty(value = "#{param.username}")
   private String username;
   private User   user;

   @PostConstruct
   public void prepareEditing() {
      this.user = someEJB.loadUserFromDatabase(username);
   }

   // Getters and Setters
}

Then you can, for example, display the user's name like this:

<h:outputLabel value="Name:" for="name" />
<h:inputText id="name" value="#{contactsMB.user.name}" />

The above <h:inputText> will display the user's current name as default value. When you type in a new name and submit, the User Entity will be updated directly.

If you really want to do it as you explained, you can change the @PostConstruct method to be like this:

@ManagedBean
@ViewScoped
public void ContactsMB {

   @EJB
   private SomeEJB someEJB;
   @ManagedProperty(value = "#{param.username}")
   private String username;
   private User   user;

   private String name;
   private String email;

   @PostConstruct
   public void prepareEditing() {
      this.user  = someEJB.loadUserFromDatabase(username);
      this.name  = user.getName();
      this.email = user.getEmail();
   }

   // Getters and Setters
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文