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

霸气de小男生 提交于 2019-12-05 01:26:03
BalusC

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!