1
votes

I have a context-param in web.xml:

<context-param>
        <description>Version number will be prefixed to url of requests</description>
        <param-name>version_id</param-name>
        <param-value>11</param-value>
</context-param>

I want to inject this into a ManagedBean This bean has a scope None

I tried the code below but it's not working, I'm getting an exception on startup with this error:

The scope of the object referenced by expression #{initParam[version_id]}, application, is shorter than the referring managed beans (responseData) scope of none

@ManagedBean
@NoneScoped
public class ResponseData implements Serializable {


    @ManagedProperty(value = "#{initParam.version_id}")
    private String version;


    public ResponseData() {
    }

    /**
     * @param version the version to set
     */
    public void setVersion(String version) {
        this.version = version;
    }

}

What is the right way to inject value of context-param into a managed bean as a managed property?

1

1 Answers

1
votes

What you are attempting is that you are trying to get the value of a managed property that has a smaller scope than the ResponseData managed bean that is NoneScoped.

You should be able to get the context params from the ServletContext however without needing to reference another bean.

ServletContext servletContext = (ServletContext) FacesContext
   .getCurrentInstance().getExternalContext().getContext();
servletContext.getInitParameter("version_id");