【发布时间】:2013-12-31 10:03:49
【问题描述】:
我有以下型号代码,我应该使用它。
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
}
现在我创建了以下 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;
}
}
现在我想在 xhtml 页面中使用这个 bean 设置客户的名字:
<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>
现在我的问题是:如何访问客户的名字?我是否应该创建一个代表用户的新 bean 并在 ClientBean 中扩展它? (如果是这样,有模型代码有什么用?我会到处都有双重代码?)或者在 JSF 2.0 中是否有其他更简单的方法来实现这一点?
【问题讨论】:
-
你试过 client.client.firstName 吗?
-
不起作用,客户端是客户端的未知属性
-
您需要将 getter 和 setter 设为公开
-
哦,废话,我为什么要把它们设为私有:O
-
在尝试此操作后,我在运行时收到以下错误:/registerClient.xhtml @15,79 value="#{client.client.firstname}": Target Unreachable, 'null' returned nul