Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 and JSF 2 integration issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3 and JSF 2 integration issue

    hi guys!
    As title is saying I want to integrate Spring with JSF and aslo with Spring Security...
    The problem that Im having now is that I can't access springs @Component beans form JSF pages even though I have org.springframework.web.jsf.el.SpringBeanFacesELRe solver declared in faces-config.xml.
    Here is my project snippet:

    web.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        
        <context-param>
            <param-name>javax.faces.PROJECT_STAGE</param-name>
            <param-value>Development</param-value>
        </context-param>
        
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/applicationContext.xml
                /WEB-INF/spring-security.xml
            </param-value>
        </context-param>
        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.faces</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>redirect.jsp</welcome-file>
        </welcome-file-list>
        
        
        <!-- Spring Security -->
    	<filter>
    		<filter-name>springSecurityFilterChain</filter-name>
    		<filter-class>
                      org.springframework.web.filter.DelegatingFilterProxy
                    </filter-class>
    	</filter>
     
    	<filter-mapping>
    		<filter-name>springSecurityFilterChain</filter-name>
    		<url-pattern>/*</url-pattern>
                    <url-pattern>/pages/*</url-pattern>
    	</filter-mapping>
    </web-app>
    faces-config.xml
    Code:
    <?xml version='1.0' encoding='UTF-8'?>
    
    <!-- =========== FULL CONFIGURATION FILE ================================== -->
    
    <faces-config version="2.0"
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
    
    <!-- JSF and Spring are integrated -->
       <application>
         <el-resolver>
          org.springframework.web.jsf.el.SpringBeanFacesELResolver
         </el-resolver>
       </application>
        
        
    </faces-config>
    dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
              <context:component-scan base-package="sk.insdata.webmaven" />
             
    </beans>
    applicationContext.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:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    
       
    </beans>
    configuration java class:
    Code:
    @Configuration
    public class SpringConfig {
        
        // Resolve logical view names to .jsp resources in the /WEB-INF/jsp directory
        @Bean
        ViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("WEB-INF/jsp/");
            resolver.setSuffix(".jsp");
            return resolver;  
        }   
    }
    finally presentation layer:
    Code:
    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:p="http://primefaces.org/ui"
          xmlns:f="http://java.sun.com/jsf/core">
        <h:head>
            <title>Facelet Title</title>
        </h:head>
        <h:body>
            Hello from Facelets
            
            <div>
                <p:calendar autocomplete="true" label="kalendar"></p:calendar>
            </div>
            
            <div>
                <h:outputLabel value="#{testBean.text}" rendered="true"/>
            </div>
        </h:body>
    </html>
    TestBean class:
    Code:
    @Component("testBean")
    @Scope(value="request")
    public class TestBean {
        
        String text = "spring with jsf is working!";
    
        public TestBean() {
        }
    
        public String getText() {
            return text;
        }
    }
    Weird is that when I have testBean declared as bean in xml configm its working..
    Also I wanted to ask why this app is not working when <context:component-scan base-package="sk.insdata.webmaven" /> is declared in aplicationContext.xml instead of dispatcher-servlet.xml?

    Thanks for advices!

  • #2
    SpringMVC and JSF(2) don't really mix use either one not both...

    JSF can only access beans loaded by the ContextLoaderListener (this contains nothing in your case) not the beans loaded by a DispatcherServlet.

    Comment


    • #3
      not both? And why it is working when beans are declared in xml? see tutorial, perhaps: http://papweb.wordpress.com/2011/07/...-2-and-tomcat/

      Comment


      • #4
        Because they load the same applicationcontext twice (once in the contextloaderlistener and once in the DispatcherServlet). Both the ContextLoaderListener and DispatcherServlet load the file applicationContext.xml.

        The FacesServlet doesn't know about other servlets and as such cannot access its environment so that way it is impossible to get access to variables declared in that servlet. The ApplicationContext is just a variable stored in the servletcontext.

        Comment

        Working...
        X