尝试从JSF2.0中的web.xml访问params时获取null

时间:2013-04-12 09:13:06

标签: jsf-2 primefaces

我已将web.xml中的参数定义为uploadDirectory,我试图从ManagedBean之一获取该参数。以下是web.xml -

的摘要
<!-- Primefaces file upload -->
<!-- thresholdSize specifies the maximum file size in bytes to keep uploaded 
    files in memory. If it exceeds this limit, it’ll be temporarily written to 
    disk. -->
<!-- uploadDirectory is the folder where to keep temporary files that exceed 
    thresholdSize. -->
<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    <init-param>
        <param-name>thresholdSize</param-name>
        <param-value>51200</param-value>
    </init-param>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>/opt/upload/</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

以下是ManagedBean -

的摘要
@ManagedBean
@ViewScoped
public class ProjectInfo implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private static final String destination = FacesContext.getCurrentInstance()
            .getExternalContext().getInitParameter("uploadDirectory");

为什么我将null作为destination

1 个答案:

答案 0 :(得分:1)

您定义的参数是过滤器的参数,因此它们在您的托管bean中不可用。例如,您可以将此Filter子类化,并添加将此参数放入请求范围的代码,并从托管bean读取此参数,或者如果要从托管bean访问此参数,其请求未被此过滤器过滤,请定义它们为<context-param>