【问题标题】:JSF viewAction passing param and it gets set, but returns to nullJSF viewAction 传递参数并设置,但返回 null
【发布时间】:2014-01-13 02:25:11
【问题描述】:

JSF 2.2
Primefaces 4.0
JBoss Wildfly

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

url中是客户id

newItem.jsf;jsessionid=Xw7tdljr9f0atzyET2Fy6_WI?customerId=3

我可以调试新项目 bean 中的 set customer id 方法调用的值为 3,很好 :)
但是在我调试调用获取客户ID方法之后..现在客户ID为空:(

我做了一个syso:

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>

现在它可以工作了... :) 但现在我有一个新问题.. 我将为此创建一个单独的问题 :) 感谢您的帮助

【问题讨论】:

  • customerDas 没有在newItemController.java 中声明,猜猜是在最小化代码示例时丢失了?
  • 是的,我删除了很多以最小化:@Inject private CustomerService customerDas;哪个是简单的数据访问服务
  • 酷,很高兴看到它现在正在工作。您能否将答案标记为“已接受”,以便其他成员和系统正确识别?祝你有美好的一天……L.

标签: jsf-2 primefaces


【解决方案1】:

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

  • 作为@PostConstruct
  • 通过 f:viewAction

如果你还是调用方法,你就不需要注解了,不是吗?

【讨论】:

    【解决方案2】:

    我永远不会理解f:viewParam...也许你错过了CustomerController.newItem() 中的includeViewParams=true?从未在表单上见过,可能是 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>
    

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      相关资源
      最近更新 更多