Following example shows how to integrate JSF framework into Spring Web application.
The difference between Spring Web and Spring MVC is that, Spring Web provides Spring context along with servlet based core infrastructure which is necessary to integrate with other web frameworks including Spring MVC itself.
Example
Maven dependencies
pom.xml<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.17</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.17</version>
</dependency>
Initializing JSF and Spring Context
package com.logicbig.example;
import com.sun.faces.config.FacesInitializer;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
public class MyWebInitializer extends FacesInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MySpringBeanConfig.class);
context.setServletContext(servletContext);
servletContext.addListener(new ContextLoaderListener(context));
}
}
In above class we are extending FacesInitializer so that FacesServlet will be registered automatically (tutorial here). We are also implementing WebApplicationInitializer to configure SpringContext .
Spring ELResolver
src/main/webapp/WEB-INF/faces-config.xml<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.1"
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_1.xsd">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
SpringBeanFacesELResolver allows us to use JSF managed beans as Spring beans and also to inject other spring beans there.
JSF Managed Bean
@Component
@RequestScoped
@ManagedBean
public class TestJsfBean {
@Autowired
private MsgService msgService;
public String getMsg() {
return msgService.getMsg();
}
}
JSF page
src/main/webapp/index.xhtml<?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:h="http://java.sun.com/jsf/html">
<h:head>
<title>Spring + JSF</title>
</h:head>
<h:body>
<h3>Spring + JSF Example</h3>
<h:outputText value = "#{testJsfBean.msg}"/>
</h:body>
</html>
Example Spring service bean
public interface MsgService {
String getMsg();
@Service
public static class DefaultMsgService implements MsgService {
@Override
public String getMsg() {
return String.format("Hi there!! it's %s here..",
LocalDateTime.now().format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));
}
}
}
JavaConfig
@Configuration
@ComponentScan
public class MySpringBeanConfig {
}
Running
To try examples, run embedded tomcat (configured in pom.xml of example project below):
mvn tomcat7:run-war
Output
http://localhost:8080/index.xhtml
Example ProjectDependencies and Technologies Used: - spring-web 5.0.7.RELEASE: Spring Web.
- spring-context 5.0.7.RELEASE: Spring Context.
- javax.servlet-api 3.0.1 Java Servlet API
- jsf-api 2.2.17:
This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
- jsf-impl 2.2.17:
This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
- JDK 10
- Maven 3.5.4
|