此任務可以使用JSF 2附帶的<f:ajax>
標籤完成,並且在this tutorial中有詳細說明。第一個例子說明如何完成手頭的任務。另一個建議是爲您的託管bean使用@ViewScoped
註釋,如BalusC博客所述:Communication in JSF 2.0 - Managed bean scopes。
有了這一切信息和您使用的託管bean,演示可以是:
Customer類
public class Customer {
private int customer_id;
private String name;
//constructor, getters and setters...
}
ReservationHandler託管bean
@ManagedBean
@ViewScoped
public class ReservationHandler {
private Customer customer;
//this EJB will retrieve the Customer data
//if you don't have it like this, then use your own
//custom CustomerService class/implementation to retrieve the data from dabatase
@EJB
private CustomerService customerService;
//constructor, getters and setters...
@PostConstruct
public void init() {
customer = new Customer();
}
public void showCustomerDataListener(AjaxBehaviorEvent event) {
Customer customerFromDB =
customerService.getCustomer(customer.getCustomer_id());
if (customerFromDB != null) {
customer = customerFromDB;
}
}
}
客戶Facelets視圖(只相關代碼)
<h:form>
<h:outputText value="Customer ID:" />
<h:inputText id="customer_id"
value="#{reservationHandler.customer.customer_id}">
<f:ajax event="blur"
listener="#{reservationHandler.showCustomerDataListener}"
render="customer_name" />
</h:inputText>
<h:outputText value="Customer name:" />
<h:inputText id="customer_name"
value="#{reservationHandler.customer.name}" />
</h:form>
如果您Customer
類posses超過1個屬性,你有兩個選擇:
坐落在<f:ajax>
組件的render
屬性每個輸入的ID,但是這是非常幼稚的。
將所有組件與UIContainer
分組並呈現此UIContainer。實施例中給出假設Customer
類具有一個附加private String address
屬性:
<h:form>
<h:outputText value="Customer ID:" />
<h:inputText id="customer_id"
value="#{reservationHandler.customer.customer_id}">
<f:ajax event="blur"
listener="#{reservationHandler.showCustomerDataListener}"
render="customerData" />
</h:inputText>
<h:panelGrid id="customerData" columns="2">
<h:outputText value="Customer name:" />
<h:inputText id="customer_name"
value="#{reservationHandler.customer.name}" />
<h:outputText value="Address:" />
<h:inputText id="customer_address"
value="#{reservationHandler.customer.address}" />
</h:panelGrid>
</h:form>
要知道,CNA在<f:ajax event="what_to_write_here">
施加的支持的事件,請參考f:ajax JSF Core Tag Reference,事件代碼屬性描述:
將調用Ajax請求的事件,例如「點擊」,「更改」,「模糊」,「按鍵」等。啓用了Ajax行爲的組件必須支持該事件。注意:事件名稱與沒有「on」前綴的DOM事件名稱相同,因此對於「onclick」事件,正確的事件名稱是「click」。命令組件(如h:commandButton和h:commandLink)支持「action」事件,輸入組件(如h:inputText和h:selectOneMenu)支持「valueChange」事件。
您使用的是JSF 1.x還是JSF 2.x? –
我正在使用JSF 2 – hurley