Announcement Announcement Module
Collapse
No announcement yet.
FacesContext Null - intentional in SWF Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • FacesContext Null - intentional in SWF

    Hi,

    Finally, with the help of forums, I got a simple flow to work in SWF. I am trying to implement SWF into an existing JSF/Spring app. I am trying to get the FacesContext and is null. I read the forums and keith's reply saying, that was intentional.

    I did not get the annotations to work. So my config files are as follows
    web-application-config.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    
    
    	<!-- Imports the configurations of the different infrastructure systems of the application
    -->
    
    	<import resource="webmvc-config.xml" />
    	<import resource="webflow-config.xml" />
    
    </beans>
    webmvc-config.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    	<!-- Maps request URIs to controllers -->			
    	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    			<value>
    				/main=flowController
    			</value>
    		</property>
    		<property name="defaultHandler">
    			<!-- Selects view names to render based on the request URI: e.g. /main selects "main" -->	
    			<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    		</property>
    	</bean>
    	<!-- Handles requests mapped to the Spring Web Flow system -->
    	<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
    		<property name="flowExecutor" ref="flowExecutor"/>
    	</bean>
    	<!-- Maps logical view names to Facelet templates (e.g. 'search' to '/WEB-INF/search.xhtml' -->
    	<bean id="faceletsViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="viewClass" value="org.springframework.faces.mvc.JsfView"/>
    		<property name="prefix" value="/WEB-INF/" />
    		<property name="suffix" value=".xhtml" />
    	</bean>		
    </beans>
    webflow-config.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:webflow="http://www.springframework.org/schema/webflow-config"
           xmlns:faces="http://www.springframework.org/schema/faces"
           xmlns:tx="http://www.springframework.org/schema/tx" 
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/webflow-config
               http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
               http://www.springframework.org/schema/faces
               http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    		   http://www.springframework.org/schema/aop 
    		   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
    	<!-- Executes flows: the central entry point into the Spring Web Flow system -->
    	<webflow:flow-executor id="flowExecutor" />
    	
    	
    	<!-- The registry of executable flow definitions -->
    	<webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices">
    		<webflow:flow-location path="/WEB-INF/flows/main/main.xml" />
    	</webflow:flow-registry>
    	
    	<!-- Configures the Spring Web Flow JSF integration -->
    	<faces:flow-builder-services id="facesFlowBuilderServices" />
    
    	<!-- Installs a listener that manages JPA persistence contexts for flows that require them -->
    	
    		<aop:config>
    		<aop:advisor
    			pointcut="execution(* com.converge.trade.persistent.services..*.*(..))"	advice-ref="txAdvice" />
    	</aop:config>
    
    	<tx:advice id="txAdvice">
    		<tx:attributes>
    			<tx:method name="save*" />
    			<tx:method name="update*" />
    			<tx:method name="*" read-only="true" />
    		</tx:attributes>
    	</tx:advice>
    	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    		<property name="allowCustomIsolationLevels">
    			<value>true</value>
    		</property>
    
    	</bean>
    		<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    		<property name="configLocation">
    			<value>/WEB-INF/SqlMapConfig.xml</value>
    		</property>
    	</bean>
    	<bean id="datawarehouse" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName" value="java:jdbc/datawarehouse" />
    	</bean>
    	<bean id="stockItemsService"
    		class="com.converge.trade.persistent.model.stock.StockItemsServiceImpl">
    
    		<property name="stockItemsVwDAO">
    			<ref local="stockItemsVwDAO" />
    		</property>
    	</bean>
    	<bean id="stockItemsVwDAO"
    		class="com.converge.trade.persistent.model.stock.view.stockitemsvw.StockItemsVwDAOImpl">
    		<property name="dataSource">
    			<ref local="datawarehouse" />
    		</property>
    		<property name="sqlMapClient">
    			<ref local="sqlMapClient" />
    		</property>
    	</bean>
    	
    	<!-- Installs a listener to apply Spring Security authorities -->
    
    </beans>
    When I click on a button I am calling a method in StockItemsVwHandler

    Code:
    public void searchRecords() {
    ServletContext context = FacesContext.getCurrentInstance().getExternalContext().getContext();
    ApplicationContext appContext= WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    
    stockItemsService = appContext.getBean("stockItemsService");
    	System.out.println("here asdasd'asda;" + stockItemsService);
    // .....
    .....
    .....
        }
    I get stockItemsService as null. Is there any other way to get the bean defined in spring?
    How can I get the servletContext in SWF?

    Thanks
    Vinaya

  • #2
    A few things:

    Try adding this element to one of your config files to restore annotations:

    Code:
    <context:annotation-config/>
    You may want to read up on both this and component scanning.


    In any case, whether you're using annotations or registering your service as a Spring bean, it should be available through the app context or just through EL by its name. I'd say a better way to do this, for you, is to simply pass the stockItemsService as a parameter to your searchRecords within your flow definition.

    If the service is null within the method, then there's definitely a problem somewhere. Maybe it's not being wired up correctly?

    Comment


    • #3
      Hi,

      I am getting the service as null due to context being null.

      Code:
      ServletContext context = FacesContext.getCurrentInstance().getExternalContext().getContext();
      I had this working by having the following code
      Code:
      MvcExternalContext context = (MvcExternalContext) ExternalContextHolder.getExternalContext();
      	
      	WebConfiguration scope = (WebConfiguration) context.getApplicationMap().get(
      		"com.sun.faces.config.WebConfiguration");
      	System.out.println("getServletContext :" + scope.getServletContext());
      	ApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(scope
      		.getServletContext());
      
      	System.out.println("appContext :" + appContext);
      	return appContext.getBean(serviceName)
      I am not sure if I can do that?

      Comment


      • #4
        It just seems to me you're jumping through quite a few unnecessary hoops. The easiest way for this to work is simply to pass your service as a parameter to your method as you call it from your flow definition. If it's been registered as a spring bean, it will be accessible.

        Now, if you're set on programmatically getting your service from the application context (not recommended, but it's your call), you can get it easily like so:

        Code:
        ApplicationContext context =  RequestContextHolder.getRequestContext().getActiveFlow().getApplicationContext();

        Comment


        • #5
          Thank you very much.
          I got it to work without the code.

          Comment

          Working...
          X