From f0ac4db89b92c520889edb71723e8ff7dd773343 Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Thu, 3 Oct 2024 01:46:26 +0200 Subject: [PATCH] Add WelcomePageServlet see https://github.com/joinfaces/joinfaces-gradle-jar-example/issues/101 --- .../example/JarExampleApplication.java | 2 ++ .../joinfaces/example/WelcomePageServlet.java | 23 +++++++++++++++++++ .../example/JarExampleApplicationTest.java | 11 +++++++++ 3 files changed, 36 insertions(+) create mode 100644 src/main/java/org/joinfaces/example/WelcomePageServlet.java diff --git a/src/main/java/org/joinfaces/example/JarExampleApplication.java b/src/main/java/org/joinfaces/example/JarExampleApplication.java index 9417025..707a919 100644 --- a/src/main/java/org/joinfaces/example/JarExampleApplication.java +++ b/src/main/java/org/joinfaces/example/JarExampleApplication.java @@ -2,11 +2,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletComponentScan; /** * @author Lars Grefer */ @SpringBootApplication +@ServletComponentScan public class JarExampleApplication { public static void main(String[] args) { diff --git a/src/main/java/org/joinfaces/example/WelcomePageServlet.java b/src/main/java/org/joinfaces/example/WelcomePageServlet.java new file mode 100644 index 0000000..b978e30 --- /dev/null +++ b/src/main/java/org/joinfaces/example/WelcomePageServlet.java @@ -0,0 +1,23 @@ +package org.joinfaces.example; + +import jakarta.servlet.GenericServlet; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; + +/** + * @author Lars Grefer + */ +@WebServlet(urlPatterns = "/") +public class WelcomePageServlet extends GenericServlet { + + @Override + public void service(ServletRequest req, ServletResponse res) throws IOException { + if (res instanceof HttpServletResponse httpServletResponse) { + httpServletResponse.sendRedirect("/index.xhtml"); + } + } +} diff --git a/src/test/java/org/joinfaces/example/JarExampleApplicationTest.java b/src/test/java/org/joinfaces/example/JarExampleApplicationTest.java index 3ed7fd8..880eafa 100644 --- a/src/test/java/org/joinfaces/example/JarExampleApplicationTest.java +++ b/src/test/java/org/joinfaces/example/JarExampleApplicationTest.java @@ -24,6 +24,17 @@ public class JarExampleApplicationTest { public void testHelloFromSpring() { ResponseEntity entity = restTemplate.getForEntity("/index.xhtml", String.class); + checkResponse(entity); + } + + @Test + public void testWelcomePageRedirect() { + ResponseEntity entity = restTemplate.getForEntity("/", String.class); + + checkResponse(entity); + } + + private static void checkResponse(ResponseEntity entity) { assertThat(entity.getStatusCode().is2xxSuccessful()).isTrue(); assertThat(entity.getBody()).contains("Hello from Spring:");