diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 772bbbb219..a9bbf71ba3 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -1,16 +1,13 @@ - + 4.0.0 org.baeldung spring-mvc-java 0.1-SNAPSHOT - spring-mvc-java war - - - org.springframework spring-web @@ -21,64 +18,53 @@ spring-webmvc ${org.springframework.version} - - javax.servlet javax.servlet-api 3.0.1 provided - javax.servlet jstl 1.2 runtime - org.springframework spring-aop ${org.springframework.version} - org.aspectj aspectjrt ${aspectj.version} - org.aspectj aspectjweaver ${aspectj.version} - org.slf4j slf4j-api ${org.slf4j.version} - org.slf4j slf4j-log4j12 ${org.slf4j.version} - - junit junit-dep ${junit.version} test - org.hamcrest hamcrest-core @@ -91,23 +77,30 @@ ${org.hamcrest.version} test - org.mockito mockito-core ${mockito.version} test - org.springframework spring-test ${org.springframework.version} test - + + + org.thymeleaf + thymeleaf-spring4 + ${thymeleaf.version} + + + org.thymeleaf + thymeleaf + ${thymeleaf.version} + - spring-mvc-java @@ -116,9 +109,7 @@ true - - org.apache.maven.plugins maven-compiler-plugin @@ -128,7 +119,10 @@ 1.8 - + + maven-resources-plugin + 2.7 + org.apache.maven.plugins maven-war-plugin @@ -137,7 +131,6 @@ false - org.apache.maven.plugins maven-surefire-plugin @@ -151,7 +144,6 @@ - org.codehaus.cargo cargo-maven2-plugin @@ -172,50 +164,46 @@ - + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + / + + - - 4.2.2.RELEASE 4.0.2.RELEASE - + 2.1.4.RELEASE 4.3.11.Final 5.1.36 - 1.7.12 1.1.3 - 5.2.1.Final - 18.0 3.4 - 1.3 4.11 1.10.19 - 4.4.1 4.5 - 2.4.1 - 3.3 2.6 2.18.1 2.7 1.4.15 - 1.8.7 - \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/org/baeldung/controller/UserController.java b/spring-mvc-java/src/main/java/org/baeldung/controller/UserController.java new file mode 100644 index 0000000000..3203296a17 --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/controller/UserController.java @@ -0,0 +1,31 @@ +package org.baeldung.controller; + +import org.baeldung.model.UserDetails; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +@RequestMapping("/") +public class UserController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String showForm(final Model model) { + final UserDetails user = new UserDetails(); + user.setFirstname("John"); + user.setLastname("Roy"); + user.setEmailId("John.Roy@gmail.com"); + model.addAttribute("user", user); + return "index"; + } + + @RequestMapping(value = "/processForm", method = RequestMethod.POST) + public String processForm(@ModelAttribute(value = "user") final UserDetails user, final Model model) { + // Insert userDetails into DB + model.addAttribute("name", user.getFirstname() + " " + user.getLastname()); + return "hello"; + } + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/dialect/CustomDialect.java b/spring-mvc-java/src/main/java/org/baeldung/dialect/CustomDialect.java new file mode 100644 index 0000000000..e6d1ad6b74 --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/dialect/CustomDialect.java @@ -0,0 +1,24 @@ +package org.baeldung.dialect; + +import java.util.HashSet; +import java.util.Set; + +import org.baeldung.processor.NameProcessor; +import org.thymeleaf.dialect.AbstractDialect; +import org.thymeleaf.processor.IProcessor; + +public class CustomDialect extends AbstractDialect { + + @Override + public String getPrefix() { + return "custom"; + } + + @Override + public Set getProcessors() { + final Set processors = new HashSet(); + processors.add(new NameProcessor()); + return processors; + } + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/UserDetails.java b/spring-mvc-java/src/main/java/org/baeldung/model/UserDetails.java new file mode 100644 index 0000000000..d0b37fae8a --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/model/UserDetails.java @@ -0,0 +1,32 @@ +package org.baeldung.model; + +public class UserDetails { + private String firstname; + private String lastname; + private String emailId; + + public String getFirstname() { + return firstname; + } + + public void setFirstname(final String firstname) { + this.firstname = firstname; + } + + public String getLastname() { + return lastname; + } + + public void setLastname(final String lastname) { + this.lastname = lastname; + } + + public String getEmailId() { + return emailId; + } + + public void setEmailId(final String emailId) { + this.emailId = emailId; + } + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/processor/NameProcessor.java b/spring-mvc-java/src/main/java/org/baeldung/processor/NameProcessor.java new file mode 100644 index 0000000000..df9a4da7f0 --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/processor/NameProcessor.java @@ -0,0 +1,23 @@ +package org.baeldung.processor; + +import org.thymeleaf.Arguments; +import org.thymeleaf.dom.Element; +import org.thymeleaf.processor.attr.AbstractTextChildModifierAttrProcessor; + +public class NameProcessor extends AbstractTextChildModifierAttrProcessor { + + public NameProcessor() { + super("name"); + } + + @Override + protected String getText(final Arguments arguements, final Element elements, final String attributeName) { + return "Hello, " + elements.getAttributeValue(attributeName) + "!"; + } + + @Override + public int getPrecedence() { + return 1000; + } + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java index 945c1794fb..50681e88f6 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java @@ -1,16 +1,28 @@ package org.baeldung.spring.web.config; +import java.util.HashSet; +import java.util.Set; + +import org.baeldung.dialect.CustomDialect; +import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Description; +import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; +import org.thymeleaf.dialect.IDialect; +import org.thymeleaf.spring4.SpringTemplateEngine; +import org.thymeleaf.spring4.view.ThymeleafViewResolver; +import org.thymeleaf.templateresolver.ServletContextTemplateResolver; @EnableWebMvc @Configuration +@ComponentScan("org.baeldung.controller") public class ClientWebConfig extends WebMvcConfigurerAdapter { public ClientWebConfig() { @@ -28,12 +40,49 @@ public class ClientWebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - + /*final InternalResourceViewResolver bean = new InternalResourceViewResolver(); bean.setViewClass(JstlView.class); bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); + bean.setSuffix(".jsp");*/ - return bean; + final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); + viewResolver.setTemplateEngine(templateEngine()); + + return viewResolver; } + + @Bean + @Description("Thymeleaf template resolver serving HTML 5") + public ServletContextTemplateResolver templateResolver() { + final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); + templateResolver.setPrefix("/WEB-INF/templates/"); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode("HTML5"); + return templateResolver; + } + + @Bean + @Description("Thymeleaf template engine with Spring integration") + public SpringTemplateEngine templateEngine() { + final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(templateResolver()); + final Set dialects = new HashSet<>(); + dialects.add(new CustomDialect()); + templateEngine.setAdditionalDialects(dialects); + return templateEngine; + } + + @Bean + @Description("Spring message resolver") + public MessageSource messageSource() { + final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); + messageSource.setBasename("messages"); + return messageSource; + } + + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + } \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/templates/footer.html b/spring-mvc-java/src/main/webapp/WEB-INF/templates/footer.html new file mode 100644 index 0000000000..f72d553303 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/templates/footer.html @@ -0,0 +1,6 @@ + + + +
© 2013 Footer
+ + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/templates/hello.html b/spring-mvc-java/src/main/webapp/WEB-INF/templates/hello.html new file mode 100644 index 0000000000..1eddd85166 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/templates/hello.html @@ -0,0 +1,14 @@ + + + + + + + Hi + John ! + Test +
© 2013 The Static + Templates
+ + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/templates/index.html b/spring-mvc-java/src/main/webapp/WEB-INF/templates/index.html new file mode 100644 index 0000000000..9b4159c193 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/templates/index.html @@ -0,0 +1,36 @@ + + + + +Thymeleaf Spring Example + + + Hello John! +

+
Please confirm your details
+

+
+ + + + + + + + + + + + + + + + +
Firstname :
Lastname :
EmailId :
+
+ + \ No newline at end of file