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

  • Spring 3 + JSF 2

    is it possible to use Spring 3 with JSF 2?

    is there an sample webapp?


    thanks!


    Nicola

  • #2
    does anyone knows if its possible?

    Comment


    • #3
      Yes, it is possible

      Yes, you can successfully use JSF 2.0 with Spring 3.0.

      Refer to Spring Reference documentation SpringBeanFacesELResolver (JSF 1.2+)

      In WEB-INF/faces-config.xml
      Code:
      <faces-config>
        <application>
          <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
        </application>
      </faces-config>
      Be sure to use proper namespace version.

      The above works quite well with Mojarra, reference JSF 2.0 implementation.

      As with most JEE6 specifications, you can avoid XML for standard configuration, but like in this case, you can override it with providing the file. Having that, all Spring beans will be available within JSF context (no need to use JSF annotations or define them in faces-config.xml). You can also use spring scope for beans (request, session, etc.). There might be some very peculiar things not working, I didn't give it thorough test drive yet.

      On side note, you should be aware that your beans will be managed by Spring, so in order for bean scopes to work properly you should set up proper filter in web.xml.
      Last edited by jarek.rozanski; Mar 23rd, 2010, 09:49 AM.

      Comment


      • #4
        ok thanks!

        this sounds pretty cool
        thank u!

        Comment


        • #5
          And a sample Webapp?

          do u know by the way if there is a sample webapp somewhere using this stack?


          thanks

          Comment


          • #6
            Most tutorials still refer to JSF1.2 (at least in context of Spring integration). In the attachment you will find 5 minute JSF2 + Spring 3.0 tutorial. It is simple Hello World, but maybe enough to get started.

            It is maven project, you can import it to Eclipse, or just fire Tomcat from maven:
            1. unzip
            2. in the project folder: mvn clean package
            3. mvn tomcat:run
            4. visit: http://localhost:8080/jsf2/index.html

            Comment


            • #7
              JSF 2 + Spring Security + Spring 3

              I have posted my code example here:
              http://www.java-forum.org/646175-post4.html

              HTH Tai

              Comment


              • #8
                JSF2 and Spring Security 3.0

                Do you have any samples of Spring Security 3.0 (<sec:authorize) and JSF 2.0? i am looking for JSF component level authorisation....using spring provided tags.

                Comment


                • #9
                  Gracia

                  Gracias jarek.rozanski, baje tu ejemplo y me fue completamente útil, sencillo, directo, justo lo que necesitaba. Saludos a esta Gran Comunidad!

                  Comment


                  • #10
                    Hi folks.

                    I´m currently using this approach to make JSF 2.0 works with Spring 3.0.5.RELEASE

                    I believe that I found a case that the SpringBeanFacesELResolver doesn't work as expected in order to make the JSF 2.0 works in all cases.

                    In JEE 6 we should be able to do this:
                    Code:
                    <h:dataTable var="row" ...>
                       ...     
                       <h:column>
                          <h:commandLink action="#{myBean.doSomething(row)}" .../>
                       </h:column>
                    </h:dataTable>
                    I'm trying to run in this way, but the following exception is throw:
                    Code:
                    java.lang.NoSuchMethodError: javax.el.ELResolver.invoke(Ljavax/el/ELContext;Ljava/lang/Object;Ljava/lang/Object;[Ljava/lang/Class;[Ljava/lang/Object;)Ljava/lang/Object;
                    	at com.sun.el.parser.AstValue.getValue(AstValue.java:111)
                    	at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
                    	at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
                    ...
                    My faces-config is set to use
                    Code:
                    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
                    Checking the source-code, SpringBeanFacesELResolver extends SpringBeanELResolver that extends ELResolver (el-api 2.2), I believe that el 1.1 doens't have the method
                    Code:
                    public Object invoke(ELContext context,
                    Object base, Object method, Class<?>[] paramTypes, Object[] params
                    Is there any workaround available or is this the case to open a ticket in issue tracking?

                    Comment


                    • #11
                      I'm facing the same problem, i think the problem is that your app server contains javax.el.ELResolver too. Check which .jar-file you have loaded the class from:

                      Code:
                      		try {
                      			Class<?> elc = Class.forName("javax.el.ELResolver");
                      			String info =
                      				elc.getProtectionDomain().getCodeSource().getLocation().toExternalForm();
                      				        System.out.println("info = " + info);
                      		} catch (ClassNotFoundException e) {
                      			// TODO Auto-generated catch block
                      			e.printStackTrace();
                      		}

                      Comment


                      • #12
                        Yeah... bloody Google App Engine.

                        I took a different approach.

                        Thx.

                        Comment


                        • #13
                          Problems following the posted code by Tai

                          Hi all!, I follow the approach proposed in this thread and I am having a problem. I am getting a NullPointerExcepcion in the LoginBean class in line 30:

                          Code:
                          Authentication result = am.authenticate(request);
                          My complete LoginBean class code is:

                          Code:
                          package com.zien.barmanagement.beans;
                          
                          import javax.faces.application.FacesMessage;
                          import javax.faces.context.FacesContext;
                          import javax.faces.bean.ManagedBean;
                          import javax.inject.Inject;
                          import javax.inject.Named;
                          import org.springframework.context.annotation.Scope;
                          import org.springframework.security.authentication.AuthenticationManager;
                          import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
                          import org.springframework.security.core.Authentication;
                          import org.springframework.security.core.AuthenticationException;
                          import org.springframework.security.core.context.SecurityContextHolder;
                          
                          
                          @ManagedBean(name="loginBean")
                          @Scope("request")
                          public class LoginBean {
                          
                              @Inject
                              @Named("authenticationManager")
                              protected AuthenticationManager am;
                          
                              private String userName;
                              private String password;
                          
                              public String login() {
                                  try {
                                      Authentication request = new UsernamePasswordAuthenticationToken(this.getUserName(), getPassword());
                                      Authentication result = am.authenticate(request);
                                      SecurityContextHolder.getContext().setAuthentication(result);
                          
                                  } catch (AuthenticationException e) {
                          
                                      FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "El usuario o el password ingresado son invalidos.", null);
                                      FacesContext.getCurrentInstance().addMessage(null, facesMsg);
                                      return null;
                                  }
                                  return "/faces/home.xhtml";
                              }
                          
                              public String getUserName() {
                                  return userName;
                              }
                          
                              public void setUserName(String userName) {
                                  this.userName = userName;
                              }
                          
                              public String getPassword() {
                                  return password;
                              }
                          
                              public void setPassword(String password) {
                                  this.password = password;
                              }
                          }
                          and this is my login.xhtml page's code:

                          Code:
                          <?xml version='1.0' encoding='UTF-8' ?>
                          <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                          <ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                                          template="./templates/layout.xhtml"
                                          xmlns:h="http://java.sun.com/jsf/html"
                                          xmlns:rich="http://richfaces.org/rich"
                                          xmlns:f="http://java.sun.com/jsf/core">
                          
                              <ui:define name="content">
                                  <f:view>
                                  <h:form id="loginForm" prependId="false">
                                          <rich:panel header="Bienvenido">
                                              <h:panelGrid columns="2">
                                                  <h:outputLabel for="username" value="Usuario: " />
                                                  <h:inputText id="username" required="true" value="#{loginBean.userName}" requiredMessage="Debe ingresar un nombre de usuario."/>
                                                  <h:outputLabel for="password" value="Contraseña: " />
                                                  <h:inputSecret id="password" value="#{loginBean.password}" required="true" requiredMessage="Debe ingresar una contraseña."/>
                                                  <h:outputLabel for="_spring_security_remember_me" value="Recordarme: " />
                                                  <h:selectBooleanCheckbox id="_spring_security_remember_me" />
                                                  <h:commandButton id="submit" type="submit" value="Ingresar" action="#{loginBean.login}"/>
                                              </h:panelGrid>
                                          </rich:panel>
                                      </h:form>
                                  </f:view>
                              </ui:define>
                          
                          </ui:composition>
                          If someone can help me, it will be great!

                          Best regards!,
                          JM

                          Comment


                          • #14
                            Seems like it is never injected...

                            @Inject
                            @Named("authenticationManager")
                            protected AuthenticationManager am;


                            Check if am is null.

                            Comment


                            • #15
                              @Inject
                              @Named("authenticationManager")
                              protected AuthenticationManager am;

                              This field may be not injected because you use @ManagedBean. Try to use @Named instead of @ManagedBean.

                              Comment

                              Working...
                              X