JSF 2.1 Spring 3.0 Integration

Posted by danny.lesnik on Stack Overflow See other posts from Stack Overflow or by danny.lesnik
Published on 2011-03-12T23:20:31Z Indexed on 2011/03/14 8:09 UTC
Read the original article Hit count: 275

Filed under:
|
|
|
|

I'm trying to make very simple Spring 3 + JSF2.1 integration according to examples I googled in the web.

So here is my code:

My HTML submitted to actionController.actionSubmitted() method:

<h:form>
<h:message for="textPanel" style="color:red;" />
    <h:panelGrid columns="3" rows="5" id="textPanel">
        //all my bean prperties mapped to HTML code.
    </h:panelGrid>
    <h:commandButton value="Submit" action="#{actionController.actionSubmitted}" />


</h:form>

now the Action Controller itself:

@ManagedBean(name="actionController")
@SessionScoped
public class ActionController implements Serializable{

    @ManagedProperty(value="#{user}")
    User user;

    @ManagedProperty(value="#{mailService}")
    MailService mailService;

    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }
    public void setUser(User user) {
        this.user = user;
    }
    private static final long serialVersionUID = 1L;
    public ActionController() {}

    public String actionSubmitted(){
        System.out.println(user.getEmail());
    mailService.sendUserMail(user);
        return "success";
    }
}

Now my bean Spring:

public interface MailService {
    void sendUserMail(User user);
}

public class MailServiceImpl implements MailService{

    @Override
    public void sendUserMail(User user) {
        System.out.println("Mail to "+user.getEmail()+" sent." );

    }
}

This is my web.xml

 <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>

  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>

  <!-- JSF mapping -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

my applicationContext.xml

<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-3.0.xsd">

    <bean id="mailService" class="com.vanilla.jsf.services.MailServiceImpl">
    </bean>

</beans>

my faces-config.xml is the following:

  <application>
            <el-resolver>
                org.springframework.web.jsf.el.SpringBeanFacesELResolver
                </el-resolver>
      <message-bundle>
        com.vanilla.jsf.validators.MyMessages
      </message-bundle>
     </application>
        <managed-bean>
        <managed-bean-name>actionController</managed-bean-name>
        <managed-bean-class>com.vanilla.jsf.controllers.ActionController</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>mailService</property-name>
            <value>#{mailService}</value>
        </managed-property>
    </managed-bean>

    <navigation-rule>
    <from-view-id>index.xhtml</from-view-id>
    <navigation-case>
        <from-action>#{actionController.actionSubmitted}</from-action>
        <from-outcome>success</from-outcome>
        <to-view-id>submitted.xhtml</to-view-id>
        <redirect />
    </navigation-case>
    </navigation-rule>

My Problem is that I'm getting NullPointerExeption because my mailService Spring bean is null.

public String actionSubmitted(){
    System.out.println(user.getEmail());
//mailService is null Getting NullPointerException
mailService.sendUserMail(user);
    return "success";
}

© Stack Overflow or respective owner

Related posts about java

Related posts about spring