I know how to request object while processing a bean method:
@ManagedBean
public class HomeAction {
....
public String getHtmlFormattedCookiesInfo(){
FacesContext facesCtx = FacesContext.getCurrentInstance();
ExternalContext extCtx = facesCtx.getExternalContext();
HttpServletRequest req = (HttpServletRequest) extCtx.getRequest();
....
// now do something with the req object such as read cookie
// or pass req object to another another function
// that knows nothing about JSF
....
}
}
}
But, I don’t like putting Faces specific code in my bean object.
Is there a way to pass the request using DI and the faces-config.xml?
Question number 9337433 starts to answer it when you want to pass something that is on the request object. But, I want the entire request object.
The
FacesContextis in EL scope available by#{facesContext}.So, this should do, provided that the managed bean is itself also request scoped.
However, having
javax.servletimports in your JSF code more than often indicate code smells and that the particular functional requirement can also just be solved the JSF way. As per the comments, you seem to be interested in collecting request cookies. You should be using the non-Servlet-API-specific methods of theExternalContextclass for this. See the javadoc for a complete overview. The cookies are also available by justExternalContext#getRequestCookieMap():Which is also available by
#{cookie}in EL (also here, the managed bean must be request scoped):Alternatively, you could look at the
Facesclass of the JSF utility library OmniFaces to save some common boilerplate.