[BAEL-3011] Accesing Spring MVC Data from Thymeleaf

- Code example for https://jira.baeldung.com/browse/BAEL-3012
This commit is contained in:
Marius Catalin Munteanu 2020-03-11 10:56:13 +02:00
parent c6c643048f
commit 902bb53089
9 changed files with 275 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.thymeleaf.mvcdata;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
@Configuration
public class BeanConfig {
@Bean
public EmailData emailData() {
return new EmailData();
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.thymeleaf.mvcdata;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
@Controller
public class EmailController {
private EmailData emailData = new EmailData();
private ServletContext servletContext;
public EmailController(ServletContext servletContext) {
this.servletContext = servletContext;
}
@GetMapping(value = "/email/modelattributes")
public String emailModel(Model model) {
model.addAttribute("emaildata", emailData);
return "mvcdata/email-model-attributes";
}
@ModelAttribute("emailModelAttribute")
EmailData emailModelAttribute() {
return emailData;
}
@GetMapping(value = "/email/requestparameters")
public String emailRequestParameters(
@RequestParam(value = "emailsubject") String emailSubject,
@RequestParam(value = "emailcontent") String emailContent,
@RequestParam(value = "emailaddress") String emailAddress1,
@RequestParam(value = "emailaddress") String emailAddress2,
@RequestParam(value = "emaillocale") String emailLocale) {
return "mvcdata/email-request-parameters";
}
@GetMapping("/email/sessionattributes")
public String emailSessionAttributes(HttpSession httpSession) {
httpSession.setAttribute("emaildata", emailData);
return "mvcdata/email-session-attributes";
}
@GetMapping("/email/servletcontext")
public String emailServletContext() {
servletContext.setAttribute("emailsubject", emailData.getEmailSubject());
servletContext.setAttribute("emailcontent", emailData.getEmailBody());
servletContext.setAttribute("emailaddress", emailData.getEmailAddress1());
servletContext.setAttribute("emaillocale", emailData.getEmailLocale());
return "mvcdata/email-servlet-context";
}
@GetMapping("/email/beandata")
public String emailBeanData() {
return "mvcdata/email-bean-data";
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.thymeleaf.mvcdata.repository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class EmailData implements Serializable {
private String emailSubject;
private String emailBody;
private String emailLocale;
private String emailAddress1;
private String emailAddress2;
public EmailData() {
this.emailSubject = "You have received a new message";
this.emailBody = "Good morning !";
this.emailLocale = "en-US";
this.emailAddress1 = "jhon.doe@example.com";
this.emailAddress2 = "mark.jakob@example.com";
}
public String getEmailSubject() {
return this.emailSubject;
}
public String getEmailBody() {
return this.emailBody;
}
public String getEmailLocale() {
return this.emailLocale;
}
public String getEmailAddress1() {
return this.emailAddress1;
}
public String getEmailAddress2() {
return this.emailAddress2;
}
public List<String> getEmailAddresses() {
List<String> emailAddresses = new ArrayList<>();
emailAddresses.add(getEmailAddress1());
emailAddresses.add(getEmailAddress2());
return emailAddresses;
}
}

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${@emailData.emailSubject}">Subject</p>
<h1>Content</h1>
<p th:text="${@emailData.emailBody}">Body</p>
<h1>Email address</h1>
<p th:text="${@emailData.emailAddress1}">Email address</p>
<h1>Language</h1>
<p th:text="${@emailData.emailLocale}">Language</p>
</body>
</html>

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${emaildata.emailSubject}">Subject</p>
<h1>Content</h1>
<p th:text="${emaildata.emailBody}"></p>
<h1>Email addresses</h1>
<p th:each="emailAddress : ${emailModelAttribute.getEmailAddresses()}">
<span th:text="${emailAddress}"></span>
</p>
<h1>Language</h1>
<p th:text="${emaildata.emailLocale}"></p>
</body>
</html>

View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${param.emailsubject}">Subject</p>
<h1>Content</h1>
<p th:text="${param.emailcontent}"></p>
<h1>Email addresses</h1>
<p th:each="emailaddress : ${param.emailaddress}">
<span th:text="${emailaddress}"></span>
</p>
<h1>Email address 1</h1>
<p th:text="${param.emailaddress[0]}"></p>
<h1>Email address 2</h1>
<p th:text="${param.emailaddress[1]}"></p>
<h1>Language</h1>
<p th:text="${param.emaillocale}"></p>
</body>
</html>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${#servletContext.getAttribute('emailsubject')}"></p>
<h1>Content</h1>
<p th:text="${#servletContext.getAttribute('emailcontent')}"></p>
<h1>Email address</h1>
<p th:text="${#servletContext.getAttribute('emailaddress')}"></p>
<h1>Language</h1>
<p th:text="${#servletContext.getAttribute('emaillocale')}"></p>
</body>
</html>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${session.emaildata.emailSubject}"></p>
<h1>Content</h1>
<p th:text="${session.emaildata.emailBody}"></p>
<h1>Email address</h1>
<p th:text="${session.emaildata.emailAddress1}"></p>
<h1>Language</h1>
<p th:text="${#session.getAttribute('emaillocale')}"></p>
</body>
</html>

View File

@ -0,0 +1,72 @@
package com.baeldung.thymeleaf.mvcdata;
import static org.hamcrest.CoreMatchers.containsString;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc(printOnlyOnFailure = false)
public class EmailControllerUnitTest {
EmailData emailData = new EmailData();
@Autowired
private MockMvc mockMvc;
@Test
public void whenCallModelAttributes_thenReturnEmailData() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/email/modelattributes"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("You have received a new message")));
}
@Test
public void whenCallRequestParameters_thenReturnEmailData() throws Exception {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("emailsubject", emailData.getEmailSubject());
params.add("emailcontent", emailData.getEmailBody());
params.add("emailaddress", emailData.getEmailAddress1());
params.add("emailaddress", emailData.getEmailAddress2());
params.add("emaillocale", emailData.getEmailLocale());
mockMvc.perform(MockMvcRequestBuilders.get("/email/requestparameters")
.params(params))
.andExpect(status().isOk())
.andExpect(content().string(containsString("en-US")));
}
@Test
public void whenCallSessionAttributes_thenReturnEmailData() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/email/sessionattributes"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Good morning !")));
}
@Test
public void whenCallServletContext_thenReturnEmailData() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/email/servletcontext"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("jhon.doe@example.com")));
}
@Test
public void whenCallBeanData_thenReturnEmailData() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/email/beandata"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("jhon.doe@example.com")));
}
}