JSF viewAction传递param并且它被设置,但返回null

时间:2014-01-13 02:25:11

标签: jsf-2 primefaces

JSF 2.2
Primefaces 4.0
JBoss Wildfly

来自包含客户列表的页面,并希望每个客户都有一个按钮,用户可以在其中添加项目。 当我单击“新项目”按钮时,我被重定向到新项目页面。

在网址中是客户ID

newItem.jsf;jsessionid=Xw7tdljr9f0atzyET2Fy6_WI?customerId=3

我可以调试新的项目bean中的set customer id方法,调用值3,不错:) 但在我调试之后,调用了get customer id方法..现在客户id为null :(

我制作了一个系统:

  

18:10:25,312 INFO [stdout](默认任务-9)设置客户ID 3

所以客户ID开始设置...但是以某种方式重置为null ????

customers.xhtml

<ui:define name="content">
<f:metadata>
    <f:viewParam name="customerId" value="#{customerController.customerEnt.id}" />
</f:metadata>
<h:form id="customers" prependId="false" includeViewParams="true">
    <p:dataTable id="dataTable" var="customer"
        value="#{customerController.customers}" rowKey="#{customer.id}"
        styleClass="userDataTableStyle" paginator="true" rows="10"
        selection="#{customerController.selectedCustomers}"
        paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
        lazy="true" rowsPerPageTemplate="10,15,50">
  ... 

        <p:column>
            <p:commandButton ajax="false" value="New Item" action="#{customerController.newItem(customer)}"/>
        </p:column>

    </p:dataTable>

</h:form>

newItem.xhtml

<ui:define name="content">
<f:metadata>
    <f:viewParam name="customerId"
        value="#{newItemController.customerId}" />
    <f:viewAction action="#{newItemController.init()}"/>
</f:metadata>
<h:form id="item" includeViewParams="true">
...

newItemController.java

@SuppressWarnings("serial")
@ViewScoped
@Named
public class NewItemController implements Serializable {

    private CustomerEnt customerEnt;
    private String customerId;

    @PostConstruct
    public void init() {
        itemEnt = new ItemEnt();

        if (customerId == null) {
            String message = "Bad request. Please use a link from within the system.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
            return;
        }

        customerEnt = customerDas.find(Long.parseLong(customerId));

        if (customerEnt == null) {
            String message = "Bad request. Unknown customer.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
        }
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
        System.out.println("Setting customer id " + customerId);
    }
}

CustomerController.java

@SuppressWarnings("serial")
@SessionScoped
@Named
public class CustomerController implements Serializable {

    private Long customerId;

    public String newItem(CustomerEnt customerEnt) {
        customerId = customerEnt.getId();
        return "newItem?faces-redirect=true&customerId=" + customerId;
    }

正如L-Ray所说,init被调用了两次,所以我在NewItemController中做了这个改变:

        public void init() {
            System.out.println("In init");
        }

        @PostConstruct
        public void postConstruct() {
            itemEnt = new ItemEnt();
            System.out.println("In postConstruct");

        }

    public void loadData() {
            if (customerId == null) {
                String message = "Bad request. Please use a link from within the system.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
                return;
            }
        }


public void save() throws Exception {
        try {
            serviceSLSB.save(Long.parseLong(customerId), itemEnt);
            FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved!", "Item saved successful");
            facesContext.addMessage(null, m);
            postConstruct();
        } catch (ConstraintViolationException e) {
            itemEnt.setBid(null);
            String errorMessage = getRootErrorMessage(e);
            FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Saving unsuccessful");
            facesContext.addMessage(null, m);
        } catch (Exception e) {
            String errorMessage = getRootErrorMessage(e);
            FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Saving unsuccessful");
            facesContext.addMessage(null, m);
        }
    }

和newItem.xhtml

<f:metadata>
                <f:viewParam name="customerId"
                    value="#{newItemController.customerId}" />
                <f:viewAction action="#{newItemController.loadData()}"/>
            </f:metadata>

现在它有效...... :)但现在我有一个新问题..我将为此创建一个单独的问题:) 谢谢你的帮助

2 个答案:

答案 0 :(得分:4)

给定的源代码看起来不错 - 只有一件事引起了我的注意:目前,你的NewItemController.init()被调用了两次

  • as @PostConstruct
  • 到f:viewAction

如果您仍然调用该方法,则不需要注释,不是吗?

答案 1 :(得分:-1)

我永远不会理解f:viewParam ...也许你会错过includeViewParams=true中的CustomerController.newItem()?从来没有在表格上看过,也许是JSF 2.2

我这样做:

@ViewScoped
@Named
public class NewItemController implements Serializable
{
    private CustomerEnt customerEnt;

    @ManagedProperty("#{param.customerId}")
    private String customerId;

    @PostConstruct
    public void init()
    {
        if(customerId == null)
        {
            String message = "Bad request. Please use a link from within the system.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
            return;
        }

        customerEnt = customerDas.find(Long.parseLong(customerId));

        if(customerEnt == null)
        {
            String message = "Bad request. Unknown customer.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
        }
    }

    public String getCustomerId()
    {
        return customerId;
    }

    public void setCustomerId(String customerId)
    {
        this.customerId = customerId;
        System.out.println("Setting customer id " + customerId);
    }
}

和newItem.xhtml

<ui:define name="content">
    <!-- 
    <f:metadata>
         <f:viewParam name="customerId"
             value="#{newItemController.customerId}" />
         <f:viewAction action="#{newItemController.init()}"/>
    </f:metadata>
    -->

    <h:form id="item">
    ...
</ui:define>