jsf按钮不会调用适用于bean的方法

时间:2017-06-12 16:26:29

标签: jsf managed-bean commandbutton

我有这个 jsf 简单表单,我尝试使用设置所选发票的 id 的方法:

    <h:form>

        <fieldset>
            <label for="email">E-Mail</label> 
            <h:inputText id="email" value="#{invoiceManageController.selectedInvoiceId}"></h:inputText>

            <h:commandButton styleClass="button-primary" action="#{invoiceManageController.putInvoice()}" value="Login"></h:commandButton>
            <h:outputText value="#{ invoiceSessionBean.invoiceId}" />  
        </fieldset>

   </h:form>

然后我得到了一个适用于bean的控制器类

@Model
public class InvoiceManageController {

    @Inject
    private InvoiceDao invoiceDao;

    @Inject
    private InvoiceSessionBean invoiceSession;

    private List<Invoice> lastInvoicesData;

    private Long selectedInvoiceId;

    public InvoiceManageController(){

         selectedInvoiceId = new Long(3);

    }

    public void putInvoice(){


        invoiceSession.setInvoiceId(selectedInvoiceId);


    }

    public void setSelectedInvoiceId(Long selectedInvoiceId) {
        this.selectedInvoiceId = selectedInvoiceId;
    }



    public Long getSelectedInvoiceId() {
        return selectedInvoiceId;
    }

}

最后我得到了一个简单的bean,用于保存在会话期间选择的发票的ID:

import javax.faces.bean.SessionScoped;
import javax.inject.Named;

@SessionScoped
@Named
public class InvoiceSessionBean implements Serializable{

    private static final long serialVersionUID = 1L;

    private Long invoiceId;

    public Long getInvoiceId() {
        return invoiceId;
    }

    public void setInvoiceId(Long invoiceId) {

        this.invoiceId = invoiceId;

    }

}

当我点击按钮时,方法未被调用且 outputText 未更新,为什么?相反,如果我调用其他不使用InvoiceSessionBean的方法,为什么呢?

我的测试:

如果我将 InvoiceSessionBean 上的注释替换为:

@ManagedBean
@SessionScoped

并尝试直接访问bean,将表单更改为:

它起作用的原因是什么?

     <h:form>
        <fieldset>
            <label for="email">E-Mail</label> 
            <h:inputText id="email" value="#{ invoiceSessionBean.invoiceId }"></h:inputText>

            <h:commandButton styleClass="button-primary"  action="#{invoiceSessionBean.setInvoiceId(invoiceSessionBean.invoiceId)}" />
           <h:outputLabel value="#{ invoiceSessionBean.invoiceId }" />  
        </fieldset>

  </h:form>

解决: 我的问题是我使用 JSF bean 来减少 CDI bean ,因此我更改导入并运行

 import javax.enterprise.context.SessionScoped;
 import javax.inject.Named;

0 个答案:

没有答案