From d62deb3bf544f206b4be645f1c5b4627449f9a27 Mon Sep 17 00:00:00 2001 From: Justin Musgrove Date: Tue, 7 Jul 2015 09:43:17 -0500 Subject: [PATCH] initial commit --- .gitignore | 14 ++++ pom.xml | 69 +++++++++++++++++++ src/main/java/com/levelup/Application.java | 67 ++++++++++++++++++ src/main/java/com/levelup/ConfigureJSF.java | 43 ++++++++++++ src/main/java/com/levelup/JSFSpringBean.java | 10 +++ src/main/java/com/levelup/JsfBean.java | 16 +++++ .../java/com/levelup/ServletInitializer.java | 13 ++++ src/main/resources/META-INF/faces-config.xml | 15 ++++ src/main/resources/application.properties | 1 + src/main/webapp/index.xhtml | 26 +++++++ .../java/com/levelup/ApplicationTests.java | 18 +++++ 11 files changed, 292 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/levelup/Application.java create mode 100644 src/main/java/com/levelup/ConfigureJSF.java create mode 100644 src/main/java/com/levelup/JSFSpringBean.java create mode 100644 src/main/java/com/levelup/JsfBean.java create mode 100644 src/main/java/com/levelup/ServletInitializer.java create mode 100644 src/main/resources/META-INF/faces-config.xml create mode 100644 src/main/resources/application.properties create mode 100644 src/main/webapp/index.xhtml create mode 100644 src/test/java/com/levelup/ApplicationTests.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4bf25e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +*.class + +# Package Files # +*.jar +*.war +*.ear + +/mydb +/target +/.settings +*.classpath +.project +/target/ +/target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f0dc84a --- /dev/null +++ b/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + com.levelup + 037-integrate-jsf-spring-boot + 0.0.1-SNAPSHOT + war + + 037-integrate-jsf-spring-boot + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.2.5.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + org.springframework.boot + spring-boot-starter-jetty + + + com.sun.faces + jsf-api + 2.2.8 + provided + + + com.sun.faces + jsf-impl + 2.2.8 + compile + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/levelup/Application.java b/src/main/java/com/levelup/Application.java new file mode 100644 index 0000000..1613adc --- /dev/null +++ b/src/main/java/com/levelup/Application.java @@ -0,0 +1,67 @@ +package com.levelup; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.embedded.ServletContextInitializer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @Bean + public JSFSpringBean jsfSpringBean() { + return new JSFSpringBean(); + } + + @Configuration + @Profile("dev") + static class ConfigureJSFContextParameters implements ServletContextInitializer { + + @Override + public void onStartup(ServletContext servletContext) + throws ServletException { + + servletContext.setInitParameter("javax.faces.DEFAULT_SUFFIX", + ".xhtml"); + servletContext.setInitParameter( + "javax.faces.PARTIAL_STATE_SAVING_METHOD", "true"); + servletContext.setInitParameter("javax.faces.PROJECT_STAGE", + "Development"); + servletContext.setInitParameter("facelets.DEVELOPMENT", "true"); + servletContext.setInitParameter( + "javax.faces.FACELETS_REFRESH_PERIOD", "1"); + + } + } + + @Configuration + @Profile("production") + static class ConfigureJSFContextParametersProd implements ServletContextInitializer { + + @Override + public void onStartup(ServletContext servletContext) + throws ServletException { + + servletContext.setInitParameter("javax.faces.DEFAULT_SUFFIX", + ".xhtml"); + servletContext.setInitParameter( + "javax.faces.PARTIAL_STATE_SAVING_METHOD", "true"); + servletContext.setInitParameter("javax.faces.PROJECT_STAGE", + "Production"); + servletContext.setInitParameter("facelets.DEVELOPMENT", "false"); + servletContext.setInitParameter( + "javax.faces.FACELETS_REFRESH_PERIOD", "-1"); + + } + } + +} diff --git a/src/main/java/com/levelup/ConfigureJSF.java b/src/main/java/com/levelup/ConfigureJSF.java new file mode 100644 index 0000000..d05d007 --- /dev/null +++ b/src/main/java/com/levelup/ConfigureJSF.java @@ -0,0 +1,43 @@ +package com.levelup; + +import java.util.HashSet; +import java.util.Set; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import org.springframework.boot.context.embedded.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.sun.faces.config.FacesInitializer; + +@Configuration +public class ConfigureJSF { + + @Bean + public ServletRegistrationBean facesServletRegistration() { + + ServletRegistrationBean servletRegistrationBean = new JsfServletRegistrationBean(); + + return servletRegistrationBean; + } + + public class JsfServletRegistrationBean extends ServletRegistrationBean { + + public JsfServletRegistrationBean() { + super(); + } + + @Override + public void onStartup(ServletContext servletContext) + throws ServletException { + + FacesInitializer facesInitializer = new FacesInitializer(); + + Set> clazz = new HashSet>(); + clazz.add(ConfigureJSF.class); + facesInitializer.onStartup(clazz, servletContext); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/levelup/JSFSpringBean.java b/src/main/java/com/levelup/JSFSpringBean.java new file mode 100644 index 0000000..bbefeda --- /dev/null +++ b/src/main/java/com/levelup/JSFSpringBean.java @@ -0,0 +1,10 @@ +package com.levelup; + +public class JSFSpringBean { + + private String welcomeMessage = "Populated by spring created bean"; + + public String getWelcomeMessage() { + return welcomeMessage; + } +} diff --git a/src/main/java/com/levelup/JsfBean.java b/src/main/java/com/levelup/JsfBean.java new file mode 100644 index 0000000..652b9bf --- /dev/null +++ b/src/main/java/com/levelup/JsfBean.java @@ -0,0 +1,16 @@ +package com.levelup; + +import javax.annotation.ManagedBean; +import javax.faces.bean.RequestScoped; + +@ManagedBean +@RequestScoped +public class JsfBean { + + private String welcomeMessage = "Populated by JSF created bean"; + + public String getWelcomeMessage() { + return welcomeMessage; + } + +} diff --git a/src/main/java/com/levelup/ServletInitializer.java b/src/main/java/com/levelup/ServletInitializer.java new file mode 100644 index 0000000..ef2b611 --- /dev/null +++ b/src/main/java/com/levelup/ServletInitializer.java @@ -0,0 +1,13 @@ +package com.levelup; + +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.web.SpringBootServletInitializer; + +public class ServletInitializer extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(Application.class); + } + +} diff --git a/src/main/resources/META-INF/faces-config.xml b/src/main/resources/META-INF/faces-config.xml new file mode 100644 index 0000000..697a3d5 --- /dev/null +++ b/src/main/resources/META-INF/faces-config.xml @@ -0,0 +1,15 @@ + + + + + org.springframework.web.jsf.el.SpringBeanFacesELResolver + + + + org.springframework.web.jsf.DelegatingPhaseListenerMulticaster + + + \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..257b306 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.profiles.active=dev \ No newline at end of file diff --git a/src/main/webapp/index.xhtml b/src/main/webapp/index.xhtml new file mode 100644 index 0000000..fc5b4ef --- /dev/null +++ b/src/main/webapp/index.xhtml @@ -0,0 +1,26 @@ + + + + + + + + + + + + +

Welcome to JSF spring boot tutorial

+ +

#{jsfBean.welcomeMessage}

+

#{jsfSpringBean.welcomeMessage}

+ +
+ + +
\ No newline at end of file diff --git a/src/test/java/com/levelup/ApplicationTests.java b/src/test/java/com/levelup/ApplicationTests.java new file mode 100644 index 0000000..c64c9dc --- /dev/null +++ b/src/test/java/com/levelup/ApplicationTests.java @@ -0,0 +1,18 @@ +package com.levelup; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@WebAppConfiguration +public class ApplicationTests { + + @Test + public void contextLoads() { + } + +}