我是jsf和jpa技術的新手,並嘗試使用他們的第一個Web應用程序。在jpa動作後清除jsf窗體
我在我的jsf頁面中創建了一個包含所有表格字段的表單。 我希望能夠清除表單內的所有輸入。 (通過點擊'乾淨'按鈕,或在更新/插入/刪除操作之後)。
爲了清理表單我創建了一個新的對象,它的所有值將被重置,但現在我的問題是當我嘗試離開這個頁面時(通過點擊回來)我不能因爲我有一個null字段(它有@notNull),我也不確定這是清除表單的正確方法 - 是否意味着我還需要清除對象?
什麼是清理表單的寫入方式?它是否也應該清除對象,實體字段(因爲表單和對象實際上是相同的)?
這是我的代碼: CustomerDetails.htmlx:
<h:body>
<h2><h:outputText value="Customer Details"/></h2>
<h:form>
<h:panelGrid columns="2" bgcolor="#eff5fa">
<h:outputLabel value="Customer ID:"/>
<h:inputText id="customerId" value="#{customer.details.customerId}">
<f:ajax event="blur" listener="#{customer.showRecord()}" execute="customerId" render="@all" />
</h:inputText>
<h:outputLabel value="Customer Name:"/>
<h:inputText id="customerName" value="#{customer.details.name}"/>
<h:outputLabel value="Credit Limit:"/>
<h:inputText id="creditLimit" value="#{customer.details.creditLimit}"/>
<h:outputLabel value="Discount Code"/>
<h:selectOneMenu id="discountCode" value="#{customer.details.discount}">
<f:selectItems value="#{customer.discountCodes}"/>
</h:selectOneMenu>
<h:outputLabel value="Email:"/>
<h:inputText id="email" value="#{customer.details.email}"/>
<h:outputLabel value="Phone:"/>
<h:inputText id="phone" value="#{customer.details.phone}"/>
<h:outputLabel value="Fax:"/>
<h:inputText id="fax" value="#{customer.details.fax}"/>
<h:outputLabel value="Address (Line 1):"/>
<h:inputText id="address1" value="#{customer.details.addressline1}"/>
<h:outputLabel value="Address (Line 2):"/>
<h:inputText id="address2" value="#{customer.details.addressline2}"/>
<h:outputLabel value="State:"/>
<h:inputText id="state" value="#{customer.details.state}"/>
<h:outputLabel value="City:"/>
<h:inputText id="city" value="#{customer.details.city}"/>
<h:outputLabel value="Zip:"/>
<h:inputText id="zip" value="#{customer.details.zip}"/>
</h:panelGrid>
<h:commandButton value="Clean" id="clean" action="#{customer.clean}"/>
<h:commandButton id="back" value="Back" action="#{customer.list}"/>
<h:commandButton id="update" value="Update" action="#{customer.update}"/>
<h:commandButton id="delete" value="Delete" action="#{customer.delete}"/>
</h:form>
<p:messages showDetail="true" />
</h:body>
CustomerMBean.java:
public class CustomerMBean {
@EJB
private CustomerSessionBean customerSessionBean;
private Customer customer;
public CustomerMBean() {
}
public List getCustomers()
{
return customerSessionBean.retrieve();
}
public Customer getDetails()
{
//Can either do this for simplicity or fetch the details again from the
//database using the Customer ID
return customer;
}
public Customer showRecord()
{
Integer test = customer.getCustomerId();
customer = customerSessionBean.retrieveByCustomer(customer);
if(customer == null)
{
customer = new Customer(test);
customer.setDiscount('H');
}
return customer;
}
public Customer clean()
{
customer = new Customer();
customer.setDiscount('H');
return customer;
}
public String showDetails(Customer customer)
{
this.customer = customer;
return "DETAILS";
}
public String update()
{
customer = customerSessionBean.update(customer);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
"Sucessful", "Record successfully saved!"));
return "SAVED";
}
public String delete()
{
customerSessionBean.delete(customer);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
"Sucessful", "Record successfully deleted!"));
return "DELETE";
}
public String list()
{
System.out.println("###LIST###");
return "LIST";
}
public javax.faces.model.SelectItem[] getDiscountCodes()
{
SelectItem[] options = null;
List<DiscountCode> discountCodes = customerSessionBean.getDiscountCodes();
if (discountCodes != null && discountCodes.size() > 0)
{
int i = 0;
options = new SelectItem[discountCodes.size()];
for (DiscountCode dc : discountCodes)
{
options[i++] = new SelectItem(dc.getDiscountCode(),
dc.getDiscountCode() + " (" + dc.getRate() + "%)");
}
}
return options;
}
}
任何幫助將appriciated ..
感謝的在提前。