tags:

views:

88

answers:

1

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(JspServletWrapper.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)
A: 

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.

BalusC
The main idea is to be able to communicate JSF values to javascript functions. I'm using an AJAX framework (EXT JS) and I need to convert data using json API to communicate it in graphical component of EXTjs. So the problem is.. I'm stuck with the JSF architecture (MVC) and I can't access to data only through ManagedBeans.This is an example of what I want to do :Ext.onReady(function(){ Ext.MessageBox.alert( '#{bean.prop}' ); });But it's impossible for javascript to evaluate jsf values. It's possible with jsp.It will be more complicated when using JSON converters and so on.
Debbech
You **really** need to have the request passed through the `FacesServlet`. Make it a JSF page. Then you can just use `<h:outputText value="#{bean.prop}" />`. Alternatively, upgrade from JSP to Facelets. Then you can use `#{bean.prop}` inline. Also see [Java/JSP/JSF and JavaScript](http://balusc.blogspot.com/2009/05/javajspjsf-and-javascript.html).
BalusC
Thanks, I get the main idea but I still recieve empty var from server When I try to show this : var variableFromServer = '<h:outputText value="#{someBean.someProperty}" />'do you have any idea why ?
Debbech
Then it is just not been set or you're acting on a different bean or you're not in JSF context.
BalusC
I double checked and I m sure that :The Property is set inside the ManagedBean and setthe called bean is the right one.But how can I be sure that im in a JSF context ?
Debbech
Add `System.out.println(someProperty)` to the `getSomeProperty()` method. Also adding `Thread.dumpStack()` may yield interesting insights. To be sure that you're in the JSF context, just ensure that the request URL matches the `url-pattern` of the `FacesServlet`. As said in the answer, it's the one which creates it. Also rightclick HTML page and check if all JSF tags are been parsed. If so, then the `FacesServlet` has done its job correctly and the cause of the problem is more in your own bean code flow/logic.
BalusC