I've a CDI managed bean wherein I'd like to set request parameters as managed properties:
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
@Named
@RequestScoped
public class ActivationBean implements Serializable {
@ManagedProperty(value="#{param.key}")
private String key;
@ManagedProperty(value="#{param.id}")
private Long id;
// Getters+setters
The URL is domain/activate.jsf?key=98664defdb2a4f46a527043c451c3fcd&id=5
, however the properties are never set and remain null
.
How is this caused and how can I solve it?
I am aware that I can manually grab them from ExternalContext
as below:
Long id = Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"), 10);
String key = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("key");
However, I'd rather use injection.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…