如何使用FacesContext.getCurrentInstance(),它返回null [英] How to use FacesContext.getCurrentInstance(), it returns null

查看:273
本文介绍了如何使用FacesContext.getCurrentInstance(),它返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天,我一直在努力尝试使用我的网络应用程序的登录部分.我已经到了可以使用tomcat上的JDBCRealm成功验证用户身份的地步(通过从sql server数据库中读取用户).现在,我想在用户帐户被阻止或凭据不正确时发送某种反馈.

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 tomcat(by reading users from a sql server database). Now I want to send some kind of feedback when the user's account has been blocked, or when the credentials are incorrect, this is where I'm stuck now.

我想用这个:

    try {
        request.login(request.getParameter("user"), request.getParameter("pass"));
    } catch (ServletException se) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Wrong Username/Password combination"));
        log(se.getMessage());
    }

但是FacesContext.getCurrentInstance()始终返回null..

But FacesContext.getCurrentInstance() always returns null..

经过研究后,我发现请求必须来自/faces中的页面,以便调用FacesServlet和初始化FacesContext(至少这是我的理解).

After doing some research I found out that the request must come from a page located in /faces so that the FacesServlet gets called and the FacesContext gets initialized(at least that's what I understood).

因此,我将登录页面移到了Web Pages文件夹内一个名为faces的新文件夹中.但是现在每次我尝试调用login.xhtml页面时,都会出现此错误:

So I moved the login page to a new folder named faces, inside the Web Pages folder. But now everytime I try to call the login.xhtml page, I get this error:

/login.xhtml Not Found in ExternalContext as a Resource  

这是堆栈跟踪:

com.sun.faces.context.FacesFileNotFoundException: /login.xhtml Not Found in ExternalContext as a Resource
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:232)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:273)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.getFacelet(DefaultFaceletFactory.java:201)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:764)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)  

即使我直接在浏览器中输入URL,我也会收到此错误.

I get this error even when I enter the URL directly into the browser.

我将Netbeans 7.1与Apache Tomcat 7一起使用(顺便说一句很痛苦,因为我从内存中获得了很多PermGen异常-.-)

Im using Netbeans 7.1 with Apache Tomcat 7(which by the way is a pain because I get a lot of PermGen out of memory exceptions -.-)

对于解决此问题的任何帮助,将不胜感激.

Any help on how to fix this would be greatly appreciated.

推荐答案

当您不在托管bean或任何其他JSF工件中时,它将返回null.例如,在普通的servlet或FacesServlet运行之前运行的servlet过滤器中. FacesServlet即是创建FacesContext并将其作为ThreadLocal放入当前HTTP请求中的那个.您需要在JSF托管Bean中执行登录工作,而不是通常的方式.您在那里的那段代码登录属于JSF托管Bean.

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.

关于登录页面,它与您开发过的任何其他JSF页面没有什么不同.还是您实际上从未使用过JSF进行开发,这是您第一次进行JSF尝试吗?好吧,在您的问题中,您也应该对此更加明确.删除该/faces文件夹,只需将login.xhtml直接放在Web内容中,并确保web.xmlFacesServlet的URL模式设置为*.xhtml.然后,您可以通过 http://localhost:8080/yourapp/login.xhtml 打开它

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.

要更好地学习JSF,请先阅读一些体面的书籍/教程,不要在不真正理解这些代码行的前提下,拼凑您在Internet上发现的零散代码.一旦您真正理解它,便应该能够自己编写/解释它.从这里开始:我们的JSF Wiki页面.

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.

这篇关于如何使用FacesContext.getCurrentInstance(),它返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆