Reputation: 4974
I have the following model code, which I am supposed to use.
public class Client extends User {
private String userName;
public Client(String firstName, String lastName, String userName){
super(firstName, lastName);
this.userName = userName;
}
//getters and setters
}
public abstract class User {
String firstName;
String lastName;
//getters and setters
}
Now I have created the following bean:
@ManagedBean(name = "client")
@SessionScoped
public class ClientBean implements Serializable {
private final long serialVersionUID = 1L;
private Client client;
public Client getClient(){
return client;
}
public void setClient(Client client){
this.client = client;
}
}
Now I want to set the clients' firstName using this bean in an xhtml page:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Register as a client</title>
</head>
<body>
<h:form>
First Name:<h:inputText value="#{???}"></h:inputText>
<br/>
<h:commandButton value="Register" action="registered?faces-redirect=true"/>
</h:form>
</body>
</html>
Now my question is: How do I access the clients' firstName? Am I supposed to create a new bean that represents user as well and extend it in ClientBean? (If so, what's the use of having model code at all? I would have double code everywhere?) Or is there any other simpler way to implement this in JSF 2.0?
Upvotes: 0
Views: 1631
Reputation: 3641
You will need the following for the page to display the last name correctly.
-- The class User must have a constructor as below along with getters and setters for firstname and lastname.
public User (String firstName, String lastName)
-- Public getter and setter method for username in Client class.
-- In the ClientBean class, I would suggest you change the name to clientBean. Also, change the getter and setter method to public instead of private. You would need to create an object of type client and initialize the client object to some value if you need to display it on the screen. In the code provided, you are not creating an object or giving any value to any of the name properties.
-- In the JSF page, you can access the values using "#{clientBean.client.firstName}"
Upvotes: 1
Reputation: 8924
You would take this approach with a managed bean and a pojo if you want to for instance persist the values in a database, or do some other magic stuff, like calling a webservice.
To access the first name you write #{client.client.firstName}
Indeed it looks a bit awesome, so I suggest give the managed bean another name.
You could create an instance of the pojo in the managed bean:
public class ClientBean implements Serializable {
private Client client = new Client();
...
}
You could also include first name and last name in the managed bean directly, this would make sense in case you are creating the pojo during some action to save the values.
JSF doesn't press you in a corset, instead you can choose the way that fits you.
Upvotes: 0
Reputation: 13556
Make getter and setter public
public Client getClient(){
return client;
}
public void setClient(Client client){
this.client = client;
}
If your Client
is null
, simply instantiate it.
private Client client = new Client();
Upvotes: 0