开发者

Auto-create ManagedBean problem

I need to instanciate a ManagedBean manually in jsp code. I used the following code :

FacesContext context = FacesContext.getCurrentInstance();
  ActorBean bean = (ActorBean) context.getApplication().createValueBinding("#{actorBean}").getValue(context);
   response.getWriter().print(bean.getChaine());

but I still get a NullPointerException ! :( Any suggestion please.

This is the stacktracelog:

11 juin 2010 12:33:44 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: "Servlet.service()" pour la servlet jsp a généré une exception
java.lang.NullPointerException
 at org.apache.jsp.jspx.portal_jsp._jspService(portal_jsp.java:157)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWr开发者_StackOverflow社区apper.java:377)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
 at java.lang.Thread.run(Unknown Source)


There are several misconceptions here. A managed bean is a JSF thing and it requires the FacesContext to be available in the current request. The FacesServlet is the one which creates the FacesContext. Based on the stacktrace this request isn't been passed through the url-pattern of the FacesServlet at all. So FacesContext#getCurrentInstance() would return null and hence this NullPointerException.

If the request were passed through the FacesServlet, then you could just do #{actorBean} somewhere in the JSF page and the bean will be autocreated for you. You really don't need to hassle with ugly scriptlets in the JSF page.

But if you don't want it to be a JSF page for some odd reasons, then you'll need to fallback to the old fashioned <jsp:useBean> approach and forget the whole JSF thing. Otherwise it look much like that you're trying to reinvent on your own way the same what JSF is already capable of. Use the one or other.

<jsp:useBean id="actorBean" class="com.example.ActorBean" />
...
<p>${actorBean.someProperty}</p>

If this doesn't solve your actual functional requirement, then you need to be more clear about it in your question. This really sounds like as if you're looking for a solution in the wrong direction. We can't suggest the right solution as long as the functional requirement is unclear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜