When using CDI and JSF2 How can a HTTP request parameter be injected into a bean?
相关问题
- Infinite setStatus() loop when requesting a JSF pa
- JSF2 composite cc.attrs expression does not evalua
- How to address the surrounding naming container in
- Why am I getting a WELD-001303: No active contexts
- My web app works only with META-INF instead of WEB
相关文章
- Is it possible to destroy a CDI scope?
- Inject producer method that returns String CDI
- What's the default scope for a bean created by
- change rendered attribute from managed bean
- How to show confirm (richfaces) popup when
- How to override primefaces component api class wit
- JPA lazy loading Collections in JSF view - better
- How to design a session-less JSF 2.0 web applicati
HINT: before reading any further have a look at http://showcase.omnifaces.org/components/param . Do it yourself is probably obsolete seeing how omnifaces is a de facto standard today. I would probably not have written this if omnifaces had this at the time
CDI does not solve specialized problems like injecting a request parameter. That's supposed to be solved 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 as well.
That said the code required is rather simple to implement yourself. Example below:
Annotation to use for the injection point (For example
private String myParam;
)Now we have the annotation but we can't just ask the container to dependency inject a
@RequestParam
- we obviously need an implementation.So how does it work? Well quite simply it first checks if you did specify what parameter you wanted as in
@Requestparam("longAndTerribleFieldNameBestToSpecify")
;If you didn't it will use the fieldName. So if you annoted a setter called setMyInstance it will look for a parameter called setMyInstance.
The normal use case would be to have a String variable that is named exactly like the parameter you want.
Note that we inject FacesContext, that must also be produced. A FacesContext producer could look like this:
End usage:
Note that this will not work for Servlet or similar as it requires access to FacesContext. In those cases one need to wrap the injection with for example a bean that is @RequesScoped. You inject that bean instead.