[BAEL-3011] Accesing Spring MVC Data from Thymeleaf
- Code example for https://jira.baeldung.com/browse/BAEL-3012
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
+48
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user