在jpa行动之后明确jsf形式

时间:2011-12-14 15:03:41

标签: jsf jsf-2

我是jsf和jpa技术的新手,并尝试了我的第一个Web应用程序。

我在jsf页面中创建了一个包含所有表字段的表单。 我希望能够清除表单内的所有输入。 (通过单击'清除'按钮,或在更新/插入/删除操作后)。

为了清理表单我创建了一个新对象,它将重置所有值,但我现在的问题是当我试图离开这个页面时(通过点击返回)我不能因为我有一个空字段(有@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;
}

}

任何帮助都会得到满足。

提前感谢。

1 个答案:

答案 0 :(得分:1)

immediate="true"添加到后退按钮,以便跳过所有非立即输入:

<h:commandButton id="back" value="Back" action="#{customer.list}" immediate="true" />

或者,更好的是,只需将其设为正常的GET按钮:

<h:button id="back" value="Back" outcome="list" />

无关具体问题,请阅读有关新JSF2隐式导航功能的信息。那些丑陋的大写结果值表明你仍在使用详细的JSF 1.x方法来定义<navigation-case>中的faces-config.xml元素。