Vishwanth: Merged my changes of Thymeleaf POC (Proof of Concept)
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<IProcessor> getProcessors() {
|
||||
final Set<IProcessor> processors = new HashSet<IProcessor>();
|
||||
processors.add(new NameProcessor());
|
||||
return processors;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<IDialect> 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/");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user