How to use FacesContext.getCurrentInstance(), it returns null

后端 未结 1 785
终归单人心
终归单人心 2021-02-20 16:19

I\'ve been struggling for the last couple of days with the login part of my web app. I\'ve gotten to the point where I can succesfully authenticate a user using the JDBCRealm on

相关标签:
1条回答
  • 2021-02-20 16:27

    It will return null when you're not in a managed bean or any other JSF artifact. For example, in a plain vanilla servlet or a servlet filter which runs before FacesServlet runs. The FacesServlet is namely the one who creates the FacesContext and puts it as a ThreadLocal in the current HTTP request. You need to do the login job in a JSF managed bean instead the usual way. That piece of code login which you've there belongs in a JSF managed bean.

    As to the login page, it's not different from any other JSF page which you've ever developed. Or have you actually never developed with JSF and is this your first JSF attempt ever? Well, you should have been more explicit about this in your question as well. Get rid of that /faces folder, just put the login.xhtml straight in the webcontent and make sure that the URL pattern of the FacesServlet in web.xml is set to *.xhtml. Then you can just open it by http://localhost:8080/yourapp/login.xhtml.

    To learn JSF better, go through a bit decent book/tutorial first and don't try to cobble loose pieces which you found on the Internet together without really understanding what those lines of code are doing. You're supposed to be able to write/explain it yourself once you really understand it. Start here: Our JSF wiki page.

    0 讨论(0)
提交回复
热议问题