diff --git a/spring-thymeleaf/README.md b/spring-thymeleaf/README.md index 3f8f0ab244..2c37e73ada 100644 --- a/spring-thymeleaf/README.md +++ b/spring-thymeleaf/README.md @@ -27,3 +27,6 @@ http://localhost:8082/spring-thymeleaf/addStudent/ http://localhost:8082/spring-thymeleaf/listStudents/ The first URL is the home page of the application. The home page has links to the other two pages. + +### Security +The user/password required is: user1/user1Pass diff --git a/spring-thymeleaf/pom.xml b/spring-thymeleaf/pom.xml index 058bd10450..f626b8dc8e 100644 --- a/spring-thymeleaf/pom.xml +++ b/spring-thymeleaf/pom.xml @@ -1,210 +1,210 @@ - - 4.0.0 - com.baeldung - spring-thymeleaf - 0.1-SNAPSHOT - war - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - 1.8 - - 4.3.4.RELEASE - 4.2.0.RELEASE - 3.1.0 - - 3.0.3.RELEASE - 3.0.0.RELEASE - 2.1.2 - - 1.1.0.Final - 5.3.3.Final - 5.2.5.Final - - - 2.6 - 1.6.1 - 2.2 - - - - - - org.springframework - spring-context - ${org.springframework-version} - - - - commons-logging - commons-logging - - - - - org.springframework - spring-webmvc - ${org.springframework-version} - - - - org.springframework.security - spring-security-web - ${springframework-security.version} - - - org.springframework.security - spring-security-config - ${springframework-security.version} - - - - org.thymeleaf - thymeleaf - ${org.thymeleaf-version} - - - org.thymeleaf - thymeleaf-spring4 - ${org.thymeleaf-version} - - - - nz.net.ultraq.thymeleaf - thymeleaf-layout-dialect - ${thymeleaf-layout-dialect.version} - - - org.thymeleaf.extras - thymeleaf-extras-java8time - ${org.thymeleaf.extras-version} - - - - javax.servlet - javax.servlet-api - ${javax.servlet-version} - provided - - - - javax.validation - validation-api - ${javax.validation-version} - - - org.hibernate - hibernate-validator - ${hibernate-validator.version} - - - - - org.springframework - spring-test - ${org.springframework-version} - test - - - - - org.springframework.security - spring-security-test - ${springframework-security.version} - test - - - - - - - org.apache.maven.plugins - maven-war-plugin - ${maven-war-plugin.version} - - false - - - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - jetty8x - embedded - - - - - - 8082 - - - - - - org.apache.tomcat.maven - tomcat7-maven-plugin - ${tomcat7-maven-plugin.version} - - - tomcat-run - - exec-war-only - - package - - / - false - webapp.jar - utf-8 - - - - - - - - - - integration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*LiveTest.java - - - **/*IntegrationTest.java - - - - - - - json - - - - - - - + + 4.0.0 + com.baeldung + spring-thymeleaf + 0.1-SNAPSHOT + war + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + 1.8 + + 4.3.4.RELEASE + 4.2.0.RELEASE + 3.1.0 + + 3.0.3.RELEASE + 3.0.0.RELEASE + 2.1.2 + + 1.1.0.Final + 5.3.3.Final + 5.2.5.Final + + + 2.6 + 1.6.1 + 2.2 + + + + + + org.springframework + spring-context + ${org.springframework-version} + + + + commons-logging + commons-logging + + + + + org.springframework + spring-webmvc + ${org.springframework-version} + + + + org.springframework.security + spring-security-web + ${springframework-security.version} + + + org.springframework.security + spring-security-config + ${springframework-security.version} + + + + org.thymeleaf + thymeleaf + ${org.thymeleaf-version} + + + org.thymeleaf + thymeleaf-spring4 + ${org.thymeleaf-version} + + + + nz.net.ultraq.thymeleaf + thymeleaf-layout-dialect + ${thymeleaf-layout-dialect.version} + + + org.thymeleaf.extras + thymeleaf-extras-java8time + ${org.thymeleaf.extras-version} + + + + javax.servlet + javax.servlet-api + ${javax.servlet-version} + provided + + + + javax.validation + validation-api + ${javax.validation-version} + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + + + org.springframework + spring-test + ${org.springframework-version} + test + + + + + org.springframework.security + spring-security-test + ${springframework-security.version} + test + + + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + 8082 + + + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + ${tomcat7-maven-plugin.version} + + + tomcat-run + + exec-war-only + + package + + / + false + webapp.jar + utf-8 + + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + \ No newline at end of file diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java index b52e26b93e..2e76877199 100644 --- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java @@ -77,14 +77,14 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application return resolver; } - private TemplateEngine templateEngine(ITemplateResolver templateResolver) { - SpringTemplateEngine engine = new SpringTemplateEngine(); - engine.addDialect(new LayoutDialect(new GroupingStrategy())); - engine.addDialect(new Java8TimeDialect()); - engine.setTemplateResolver(templateResolver); - engine.setTemplateEngineMessageSource(messageSource()); - return engine; - } + private TemplateEngine templateEngine(ITemplateResolver templateResolver) { + SpringTemplateEngine engine = new SpringTemplateEngine(); + engine.addDialect(new LayoutDialect(new GroupingStrategy())); + engine.addDialect(new Java8TimeDialect()); + engine.setTemplateResolver(templateResolver); + engine.setTemplateEngineMessageSource(messageSource()); + return engine; + } private ITemplateResolver htmlTemplateResolver() { SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); @@ -142,7 +142,8 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/"); + registry.addResourceHandler("/resources/**", "/css/**") + .addResourceLocations("/WEB-INF/resources/", "/WEB-INF/css/"); } @Override diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FragmentsController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FragmentsController.java new file mode 100644 index 0000000000..1dd2caedaf --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FragmentsController.java @@ -0,0 +1,33 @@ +package com.baeldung.thymeleaf.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +import com.baeldung.thymeleaf.utils.StudentUtils; + +@Controller +public class FragmentsController { + + @GetMapping("/fragments") + public String getHome() { + return "fragments.html"; + } + + @GetMapping("/markup") + public String markupPage() { + return "markup.html"; + } + + @GetMapping("/params") + public String paramsPage() { + return "params.html"; + } + + @GetMapping("/other") + public String otherPage(Model model) { + model.addAttribute("data", StudentUtils.buildStudents()); + return "other.html"; + } + +} diff --git a/spring-thymeleaf/src/main/resources/messages_en.properties b/spring-thymeleaf/src/main/resources/messages_en.properties index 33295968e0..d29fd24088 100644 --- a/spring-thymeleaf/src/main/resources/messages_en.properties +++ b/spring-thymeleaf/src/main/resources/messages_en.properties @@ -1,9 +1,9 @@ -msg.id=ID -msg.name=Name -msg.gender=Gender -msg.percent=Percentage -welcome.message=Welcome Student !!! -msg.AddStudent=Add Student -msg.ListStudents=List Students -msg.Home=Home - +msg.id=ID +msg.name=Name +msg.gender=Gender +msg.percent=Percentage +welcome.message=Welcome Student !!! +msg.AddStudent=Add Student +msg.ListStudents=List Students +msg.Home=Home + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/css/styles.css b/spring-thymeleaf/src/main/webapp/WEB-INF/css/styles.css new file mode 100644 index 0000000000..85313345a7 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/css/styles.css @@ -0,0 +1,13 @@ +body { + background-color: lightGray +} + +.dark { + background-color: black; + color: white; +} + +.light { + background-color: #f1f1f1; + color: #ccc; +} \ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html index c5466def25..f3d28d7ef4 100644 --- a/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html @@ -1,43 +1,43 @@ - - - -Add Student - - -

Add Student

-
- - - - - - - - - - - - - - - - - - - - - -
-
- - + + + +Add Student + + +

Add Student

+
+ + + + + + + + + + + + + + + + + + + + + +
+
+ + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments.html new file mode 100644 index 0000000000..8688c5b370 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments.html @@ -0,0 +1,12 @@ + + + +Thymeleaf Fragments: home + + + +
+

Go to the next page to see fragments in action

+
+ + \ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/forms.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/forms.html new file mode 100644 index 0000000000..8e905216b0 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/forms.html @@ -0,0 +1,12 @@ +
+
+ +
+
+ +
+
\ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/general.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/general.html new file mode 100644 index 0000000000..d615f42550 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/general.html @@ -0,0 +1,23 @@ + + + + + + + +
+

Thymeleaf Fragments sample

+
+

Go to the next page to see fragments in action

+ +
This is another sidebar
+ + + \ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/menus.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/menus.html new file mode 100644 index 0000000000..e8825e5053 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/menus.html @@ -0,0 +1,2 @@ +
Data received
+
No data
\ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/subtitle.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/subtitle.html new file mode 100644 index 0000000000..cb8c04dc76 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/subtitle.html @@ -0,0 +1 @@ +

This is a subtitle

\ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/tables.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/tables.html new file mode 100644 index 0000000000..c13bf83d98 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/fragments/tables.html @@ -0,0 +1,14 @@ + + + + + + + + + + + + + +
0Name
diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html index c7d17a4f66..4e281bb27f 100644 --- a/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html @@ -1,45 +1,45 @@ - - - -Student List - - - - -

Student List

- - - - - - - - - - -
- - - -
- - -
-
-

- -

-
- - - - + + + +Student List + + + + +

Student List

+ + + + + + + + + + +
+ + + +
+ + +
+
+

+ +

+
+ + + + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/markup.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/markup.html new file mode 100644 index 0000000000..666c95fc16 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/markup.html @@ -0,0 +1,13 @@ + + + + +Thymeleaf Fragments: markup + + +
+
+
+
+ + \ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/other.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/other.html new file mode 100644 index 0000000000..00a77ccb09 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/other.html @@ -0,0 +1,27 @@ + + + +Thymeleaf Fragments: other + + +
+
+
+ + + + + + + + +
+
IdName
+
+ + \ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/params.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/params.html new file mode 100644 index 0000000000..a27cb00672 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/params.html @@ -0,0 +1,14 @@ + + + + +Thymeleaf Fragments: params + + +
+
+
+
+ + \ No newline at end of file diff --git a/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FragmentsTest.java b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FragmentsTest.java new file mode 100644 index 0000000000..93261b02df --- /dev/null +++ b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FragmentsTest.java @@ -0,0 +1,90 @@ +package com.baeldung.thymeleaf.controller; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.RequestPostProcessor; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.thymeleaf.config.InitSecurity; +import com.baeldung.thymeleaf.config.WebApp; +import com.baeldung.thymeleaf.config.WebMVCConfig; +import com.baeldung.thymeleaf.config.WebMVCSecurity; + +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import javax.servlet.Filter; + +import static org.hamcrest.Matchers.containsString; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }) +public class FragmentsTest { + + @Autowired + WebApplicationContext wac; + @Autowired + MockHttpSession session; + + private MockMvc mockMvc; + + @Autowired + private Filter springSecurityFilterChain; + + private RequestPostProcessor testUser() { + return user("user1").password("user1Pass").roles("USER"); + } + + @Before + public void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build(); + } + @Test + public void whenAccessingFragmentsRoute_thenViewHasExpectedContent() throws Exception { + this.mockMvc + .perform(get("/fragments").with(testUser())) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Thymeleaf Fragments: home"))); + } + + @Test + public void whenAccessingParamsRoute_thenViewHasExpectedContent() throws Exception { + this.mockMvc + .perform(get("/params").with(testUser())) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Name"))); + } + + @Test + public void whenAccessingMarkupRoute_thenViewHasExpectedContent() throws Exception { + this.mockMvc + .perform(get("/markup").with(testUser())) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("
This is another sidebar
"))); + } + @Test + public void whenAccessingOtherRoute_thenViewHasExpectedContent() throws Exception { + this.mockMvc + .perform(get("/other").with(testUser())) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("John Smith"))); + } +} \ No newline at end of file