Reputation: 3967
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?
Upvotes: 2
Views: 10235
Reputation: 9266
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
}
Upvotes: 2