h:在页面加载时,commandButton不使用JSF bean

时间:2016-01-20 07:11:09

标签: jsf jsf-2 view-scope

问题:h:commandButton ajax无法使用JSF bean,而页面正在加载。

一个包含长列表的JSF页面(加载几秒钟):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:co="http://java.sun.com/jsf/composite/standart/components"> 

    <h:form id="myForm"> 
        <div><h:outputLabel value="My name is #{myViewBean.name}" /></div>
        <h:commandButton
            action="#{myViewBean.actRedirectToAnotherView()}" 
            value="Redirect"/>                
        <h:dataTable value="#{myViewBean.bigList}" var="entity">
            <h:column>
                #{entity}
            </h:column>
        </h:dataTable>
    </h:form>
</html>

托管视图范围bean“MyViewBean”:

package mypackage;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import org.springframework.context.annotation.Scope;

@Named
@Scope(value = "view")
public class MyViewBean {

    private static int beanNrCounter = 0;
    private static final List<String> bigList = new ArrayList();
    static {
        for (int i = 0; i < 100000; i++) {
            bigList.add("Item in big list " + i);
        }
    }

    private final String name;

    public MyViewBean() {
        beanNrCounter++;
        name = "BEAN NR " + beanNrCounter;
        System.out.println("Created MyViewBean with name " + name);  
    }

    public String actRedirectToAnotherView() {
        String viewName = "anotherView.jsf";//actually some complex operation which decides where to redirect user 
        System.out.println("Redirecting to another view " + viewName );
        return viewName + "&faces-redirect=true";
    }

    public String getName() {
        return name;
    }

    public List<String> getBigList() {
        return bigList;
    }

}

如果我点击“重定向”按钮页面加载方法“actRedirectToAnotherView”未执行,而是创建了另一个“MyViewBean”bean。如果我单击按钮,则页面已加载,一切都按预期工作。

我已经检查过,然后单击“重定向”按钮,如果页面加载形式参数javax.faces.ViewState与ajax post一起传递,但如果页面正在加载 - 缺少javax.faces.ViewState(所以偏离课程新视图已创建。)

如果我将托管bean的范围更改为“请求”问题停留(“重定向”按钮仍然无效,则不执行actRedirectToAnotherView)。

如何在加载页面时修复ajax行为? 在我看来,只要访问MyViewBean,javax.faces.ViewState就在客户端就知道了,所以无论页面是否已经加载,都应该传递javax.faces.ViewState。

我正在使用:

1 个答案:

答案 0 :(得分:-1)

看看这个例子我希望你发现它很有用

Facelets的:

<h:form>
    <h:commandLink value="show" action="#{showProducts.toggleShow}">
        <f:ajax render="products"/>
    </h:commandLink>
    <h:panelGroup id="products">
        <h:dataTable var="product" value="#{showProducts.products}" rendered="#{!showProducts.show}">
            <h:column>#{product.name}</h:column>
        </h:dataTable>
    </h:panelGroup>
</h:form>

和Managed Bean

private List<Product> products;

@PostConstruct
public void init() {
    products = this.getProductManager().getProducts();
}

public List<Product> getProducts() {
    return products;
}
Or if it actually needs to be lazily loaded, then rather do so:

private List<Product> products;

public List<Product> getProducts() {
    if (products == null) {
        products = this.getProductManager().getProducts();
    }
    return products;
}