【问题标题】:Getting null while trying to access params from web.xml in JSF2.0尝试从 JSF2.0 中的 web.xml 访问参数时获取 null
【发布时间】:2013-04-12 09:13:05
【问题描述】:

我在web.xml 中定义了一个参数为uploadDirectory,我试图从ManagedBean 之一中获取该参数。下面是web.xml的sn-p-

<!-- 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的sn-p-

@ManagedBean
@ViewScoped
public class ProjectInfo implements Serializable {

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

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

为什么我得到nulldestination

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    您定义的参数是过滤器的参数,因此它们在您的托管 bean 中不可用。例如,您可以将此过滤器子类化并添加将此参数置于请求范围内的代码,并从托管 bean 中读取此参数,或者如果您想从托管 bean(其请求未被此过滤器过滤)访问此参数,请定义他们是&lt;context-param&gt;

    【讨论】:

    • 我的目标只是在managedbean之外定义一个uploadDirectory,以便我可以在需要时更改它。如果需要,您能否通过更新的web.xml 解释您的建议?
    • 是的,我就是这么想的。
    猜你喜欢
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    相关资源
    最近更新 更多