Torsten Oppermann wrote:as far as i can tell, you are not supposed to use servlets with JSF. You only have one universal controller servlet, the facelet. Everything else you can handle with managed Beans or CDI
Nope. Not so. JSF is not a greedy all-or-nothing framework. A webapp can have JSF, raw JSPs, servlets, and even
Struts code in it. If a request is a JSF request, it's routed to the JSF main Controller (FacesServlet), and if not, it's routed to whatever
JSP or servlet is configured to receive it. JSF is built on the same object types as regular
J2EE, so JSF code and non-JSF code can cheerfully chatter back and forth all day long.
People need to understand that JSF is primarily about forms and HTML. Too many people try and force JSF to output PDFs and Excel spreadsheets, and that's just wrong. It's the "small-child-with-hammer" syndrome where every problem looks like a nail. Servlets are a much better fit for stuff like that.
I can see from the questions in this
thread, however, that there's some confusion about how to get to/from servlets from JSF code. Allow me to explain.
A JSF action method returns a navigation token (alternatively in JSF2 a relative URL) that's designed to allow it to cause a new JSF View to be presented. So what if you want a Servlet, instead?
Well, first of all, the navigation is for the outgoing display! And the servlet would be an ingoing request. So that kind of rules out using JSF navigation anyway. The exception to that is in cases where you are actually doing a JSF internal forward to a servlet, but that's not necessarily what you want to do anyway in most cases.
The key is in understanding that there are 2 different types of actions that can be coded on a JSF View. The commandLink and commandButton elements fire a JSF action, which initiates a JSF form postback. Meaning that JSF backing beans will be updated and action logic will be invoked. But there's also a non-JSF submit, which is the h:outputLink tag.
The outputLink tag emits a non-JSF URL, adjusted to reflect the application context (which is convenient, since you can do app-relative URLs). That URL can reference anything. Servlets, JSPs, even JSF. However, since it's a link and not a command, it won't submit any form data. If you absolutely have to submit a form, use the raw HTML form and a raw HTML submit.