Results 1 to 9 of 9

Thread: FacesContext is null in web flow 2.0.5

  1. #1
    Join Date
    Jan 2009
    Posts
    14

    Default FacesContext is null in web flow 2.0.5

    hi
    on rendering the page i fill the h:selectOneMenu if error occured i create a message with FacesContext.getCurrentInstanece()..........
    but FacesContext.getCurrentInstanece() is null.

    this is my code
    search-flow.xml
    Code:
    	
    <view-state id="list" view="../test/search.xhtml">
    
    	<on-render>
    		<evaluate expression="search.getAccounts()" result="flowScope.accounts"/>
    
    	</on-render>
    		<transition on="cancel" to="end" />
    	</view-state>
    Code:
    	public List<SelectItem> getAccounts() {
    
        	try {
               
                ......
        	}
        	catch (Exception e) {
        		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", ""));
        	}
    
        }
    web-mvc.xml

    Code:
    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    		<property name="flowRegistry" ref="flowRegistry" />
    		<property name="defaultHandler">
    			<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    		</property>
    	</bean>
    
    	<bean id="faceletsViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="viewClass" value="org.springframework.faces.mvc.JsfView"/>
    		<property name="prefix" value="/test/" />
    		<property name="suffix" value=".xhtml" />
    	</bean>
    
    	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    
    	<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    		<property name="flowExecutor" ref="flowExecutor" />
    	</bean>
    web-flow.xml

    Code:
    <webflow:flow-executor id="flowExecutor">
    		<webflow:flow-execution-listeners>
    			<webflow:listener ref="securityFlowExecutionListener" />
    		</webflow:flow-execution-listeners>
    		<webflow:flow-execution-attributes>
    			<webflow:always-redirect-on-pause value="false"/>
    		</webflow:flow-execution-attributes>
    	</webflow:flow-executor>
    
    	<webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices" base-path="/WEB-INF/f">
    		<webflow:flow-location-pattern value="/**/*-flow.xml" />
    	</webflow:flow-registry>
    
    	<faces:flow-builder-services id="facesFlowBuilderServices" development="true" />
    
    	<bean id="securityFlowExecutionListener" class="org.springframework.webflow.security.SecurityFlowExecutionListener" />
    faces-config.xml

    Code:
    <managed-bean>
      <managed-bean-name>search</managed-bean-name>
      <managed-bean-class>com.test.Search</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
     </managed-bean>
     <application>
      <variable-resolver>org.springframework.web.jsf.SpringBeanVariableResolver</variable-resolver>
      <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
      <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
     </application>

    if i define bean in application-context or add this part to application-context
    Code:
    <context:component-scan base-package="com.test" />
    nothing change
    Last edited by ashkan.am528; Jan 22nd, 2009 at 06:52 AM.

  2. #2
    Join Date
    Apr 2005
    Location
    San Francisco, CA
    Posts
    1,224

    Default

    FacesContext has not been re-constructed yet at that point as on-render actually fires just before the rendering process. Easiest alternative is to pass the SWF MessageContext into your method and use that to add the message (with Spring Faces, any message added to the SWF MessageContext will be available as a FacesMessage).

    So in your flow definition:
    Code:
    <on-render>
        <evaluate expression="search.getAccounts(messageContext)" result="flowScope.accounts"/>
    </on-render>
    and in your Java code:
    Code:
    public List<SelectItem> getAccounts(MessageContext messages) {
    
        try {
               
            ......
        }
        catch (Exception e) {
            messages.addMessage(new MessageBuilder().error().defaultText("error").build());
        }
    }
    Jeremy Grelle

    Staff Engineer, Web Products Team
    SpringSource

  3. #3
    Join Date
    Jan 2009
    Posts
    14

    Default

    thanks for your response.
    can you tell me the harder way for this question.
    because i use FacesContext for getting component , adding message, getting http request for many process, and so on.

  4. #4
    Join Date
    Jan 2009
    Posts
    14

    Default

    no message displayed in style that you said.
    i put externalContext as input parameter of getAccounts method, from request and response i create facesContext object, but message didn't displayed in page.
    how can i pass facesContext as method parameter?

  5. #5
    Join Date
    Jan 2009
    Posts
    1

    Default alternative is to pass

    at that point as on-render actually fires just before the rendering process. Easiest alternative is to pass the SWF MessageContext into your method and use that to add the message (with Spring Faces, any message added to the SWF MessageContext

  6. #6
    Join Date
    Jan 2009
    Posts
    14

    Default

    i send request to service layer of my application in on-renderer and based on result of service i load different ui for client. in this situation i need FacesContext.
    i there another way for solving this problem with web flow and jsf?

  7. #7
    Join Date
    Aug 2004
    Location
    St. Petersburg, Russia
    Posts
    25

    Default

    Quote Originally Posted by ashkan.am528 View Post
    i send request to service layer of my application in on-renderer and based on result of service i load different ui for client. in this situation i need FacesContext.
    i there another way for solving this problem with web flow and jsf?
    You can use JSF capability (PhaseListener for instance) to execute code at the specified moment of the JSF lifecycle.

  8. #8
    Join Date
    Jan 2009
    Posts
    14

    Default

    can you explain how to use phaselistener?
    if you meen beforPhase and afterPhase ,it has a lot of problem.
    you must change one class of facelet in 1.1.9.
    i need something in web flow, or something like @postConstruct that i can inject spring service to it.

  9. #9
    Join Date
    Aug 2004
    Location
    St. Petersburg, Russia
    Posts
    25

    Default

    Quote Originally Posted by ashkan.am528 View Post
    can you explain how to use phaselistener?
    if you meen beforPhase and afterPhase ,it has a lot of problem.
    you must change one class of facelet in 1.1.9.
    i need something in web flow, or something like @postConstruct that i can inject spring service to it.
    We use facelets 1.1.14 and don't have any problem with PhaseListener's. All is needed - is just register them in faces-config.xml. If you need access to the flow context - invoke RequestContextHolder.getRequestContext().

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •