如何从resourceHandler自定义实现获取@ViewScoped托管bean

时间:2018-03-07 16:04:37

标签: jsf jsf-2

是否可以从resourceHandler自定义实现中获取@ViewScoped托管bean?

我的ResourceHandlerImp, (registered in faces-config.xml)工作正常,但我需要对发出请求的@ViewScoped bean的引用,但我总是在 fc.getViewRoot()处获得NullPointerException。据我了解,@ ViewScoped bean should lives with Post requests

ResourceHandler实施:

import java.util.Map;

import javax.faces.application.Resource;
import javax.faces.context.FacesContext;

import com.sun.faces.application.resource.ResourceHandlerImpl;


public class ResourceHandler extends ResourceHandlerImpl {

    @Override
    public Resource createResource (String resourceName, String libraryName) {
        if ("MyLibrary".equals (libraryName)) {

            try {

                FacesContext fc = FacesContext.getCurrentInstance ( );

                Map<String, Object> viewMap = fc.getViewRoot ( ).getViewMap ( );
                Download download = (Download) viewMap.get ("download");

                return (Resource) download.createResource (resourceName);
            } catch (Exception e) {
                e.printStackTrace ( );
                return null;
            }
        } else {
            return super.createResource (resourceName, libraryName);
        }
    }

    @Override
    public boolean libraryExists (String libraryName) {
        if ("MyLibrary".equals (libraryName)) {
            return true;
        } else {
            return super.libraryExists (libraryName);
        }
    }

}

形式:

<form id="subscribe" action="#{download.resourceUrl}"
    method="POST">
    <input type="submit" value="Download as resource" />
</form>

但是,如果尝试download directly from @ViewScopedBean它完美无缺,那么我猜@ViewScoped bean正在运行。而且,如果我改为@SessionScoped,它也可以毫无问题地工作。

0 个答案:

没有答案