TIP: before reading further, look at http://showcase.omnifaces.org/components/param . Make it yourself, probably obsolete, seeing how omniscience is the de facto standard today. I probably wouldn’t write it if all this had it at that time
CDI does not solve specialized problems, such as entering a query parameter. This should be allowed by extensions.
This is already provided by solder. http://docs.jboss.org/seam/3/solder/latest/reference/en-US/html/injectablerefs.html
It will probably be included in Deltaspike 0.4-incubating or similar.
However, the required code is quite simple to implement. Example below:
Annotation to use for an injection point (e.g. private String myParam; )
import javax.enterprise.util.Nonbinding; import javax.inject.Qualifier; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.RUNTIME; @Qualifier @Retention(RUNTIME) @Target({METHOD, FIELD, PARAMETER }) public @interface RequestParam { @Nonbinding public String value() default ""; }
Now we have the annotation, but we can’t just ask the container about add a @RequestParam - we obviously need an implementation.
import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; import javax.faces.context.FacesContext; import javax.inject.Inject; public class RequestParamProducer implements Serializable { private static final long serialVersionUID = -4260202951977249652L; @Inject FacesContext facesContext;
So how does it work? Well, this just checks to see if you really specified which parameter you need, as in @Requestparam("longAndTerribleFieldNameBestToSpecify") ;
If you have not done so, it will use fieldName. Therefore, if you edited a setter called setMyInstance, it will look for the setMyInstance parameter.
In the normal use case, it will have a string variable that is called exactly the same as the desired parameter.
Note that we are introducing a FacesContext, which must also be created. The producer of FacesContext might look like this:
class FacesContextProducer { @Produces @RequestScoped FacesContext getFacesContext() { return FacesContext.getCurrentInstance(); } }
End use:
@Inject @RequestParam private String session_secret;
Note that this will not work for a servlet or similar, as this requires access to FacesContext. In these cases, you need to wrap the injection, for example, a bean, which is @RequesScoped. Instead, you enter a bean.