绑定一个bean 属性的值,同时在字段中显示不同的文本
我知道这个标题可能听起来很荒谬,但我不知道如何解释我的意思:)
我有一个用于编辑实体的表单。 每个字段都链接到其相关的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该问自己:
您真的需要这样做吗?
。如果您正在编辑实体,则应该直接利用该实体的 getter 和 setter。它可能是这样的:那么您可以,例如,像这样显示用户的名称:
上面的
将显示用户的当前名称作为默认值。当您输入新名称并提交时,User
实体将直接更新。如果您确实想按照您的解释进行操作,您可以将
@PostConstruct
方法更改为如下所示: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:Then you can, for example, display the user's name like this:
The above
<h:inputText>
will display the user's current name as default value. When you type in a new name and submit, theUser
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: