Environment :
Tomcat 8
Spring Boot 1.5
JSF 2.2
Apache MyFaces
Spring MVC
Code :
I am integrating Spring Boot and JSF 2.2 in Servlet 3.0 environment.
Config Classes :
JSFConfig.java - Config for JSF.
@Configuration @ComponentScan({"com.atul.jsf"}) public class JSFConfig { @Bean public ServletRegistrationBean servletRegistrationBean() { FacesServlet servlet = new FacesServlet(); return new ServletRegistrationBean(servlet, "*.jsf"); } }
Spring Boot Main Class :
@SpringBootApplication @Import({ // @formatter:off JPAConfig.class, ServiceConfig.class, // this contains UserServiceImpl.java class. WebConfig.class, JSFConfig.class, }) public class SpringbootJpaApplication extends SpringBootServletInitializer{ public static void main(String[] args) { SpringApplication.run(SpringbootJpaApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringbootJpaApplication.class); } }
Managed Bean :
UserBean.java - Managed Bean for JSF
@ManagedBean @SessionScoped public class UserBean implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String name; @ManagedProperty(value="#{userServiceImpl}") private UserServiceImpl userServiceImpl; public void addUser(){ System.out.println("User Gets added "+this.name); } public String getName() { return name; } public void setName(String name) { this.name = name; } public UserServiceImpl getUserServiceImpl() { return userServiceImpl; } public void setUserServiceImpl(UserServiceImpl userServiceImpl) { this.userServiceImpl = userServiceImpl; } }
Facelets :
home.xhtml - home page
<?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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF 2.0 Hello World</title> </h:head> <h:body> <h2>JSF 2.0 Hello World Example - hello.xhtml</h2> <h:form> <h:inputText value="#{userBean.name}"></h:inputText> <h:commandButton value="Submit" action="#{userBean.addUser}"></h:commandButton> </h:form> </h:body> </html>
faces-config.xml :
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2"> <application> <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> </application> <lifecycle> <phase-listener>org.springframework.web.jsf.DelegatingPhaseListenerMulticaster</phase-listener> </lifecycle> </faces-config>
Issue :
1)when I submit form in home.xhtml , userBean.addUser
gets called. 2)userBean.name
gets set with values entered by user. 3)But userServiceImpl
is NULL. 4)Does that mean that Spring and JSF is not getting integrated ? I have also registered SpringBeanFacesELResolver
as mentioned in
faces-config.xml
I also tried removing all JSF specific annotations from UserBean.java and used only Spring specific annotations like below -
@Component @SessionScoped public class UserBean implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String name; @Autowired private UserServiceImpl userServiceImpl; }
But when I submit form , I am getting target Unreachable error for #{userBean)
. That means userBean
is not discoverable for Spring
5)Am I missing anything here ? 6)I am not using embedded tomcat provided with Spring Boot