Merge remote-tracking branch 'upstream/master' into JAVA-3570
This commit is contained in:
@@ -15,6 +15,15 @@
|
||||
|
||||
<modules>
|
||||
<module>spring-mvc-basics</module>
|
||||
<module>spring-mvc-basics-2</module>
|
||||
<module>spring-mvc-basics-3</module>
|
||||
<module>spring-mvc-basics-4</module>
|
||||
<module>spring-mvc-crash</module>
|
||||
<module>spring-mvc-forms-jsp</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-thymeleaf</module>
|
||||
<module>spring-thymeleaf-2</module>
|
||||
<module>spring-thymeleaf-3</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
## Spring MVC Basics 2
|
||||
|
||||
This module contains articles about Spring MVC
|
||||
|
||||
## Relevant articles:
|
||||
- [HandlerAdapters in Spring MVC](https://www.baeldung.com/spring-mvc-handler-adapters)
|
||||
- [Template Engines for Spring](https://www.baeldung.com/spring-template-engines)
|
||||
- [Spring 5 and Servlet 4 – The PushBuilder](https://www.baeldung.com/spring-5-push)
|
||||
- [Servlet Redirect vs Forward](https://www.baeldung.com/servlet-redirect-forward)
|
||||
- [Guide to Spring Email](https://www.baeldung.com/spring-email)
|
||||
- [Using ThymeLeaf and FreeMarker Emails Templates with Spring](https://www.baeldung.com/spring-email-templates)
|
||||
- [Request Method Not Supported (405) in Spring](https://www.baeldung.com/spring-request-method-not-supported-405)
|
||||
- [Spring @RequestParam Annotation](https://www.baeldung.com/spring-request-param)
|
||||
- [Spring @RequestParam vs @PathVariable Annotations](https://www.baeldung.com/spring-requestparam-vs-pathvariable)
|
||||
- More articles: [[more -->]](/spring-mvc-basics-3)
|
||||
- More articles: [[<-- prev]](/spring-mvc-basics)
|
||||
@@ -0,0 +1,184 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-mvc-basics-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-mvc-basics-2</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-oxm</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>${javax.mail.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>${javax.servlet.jsp-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>${jstl.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>${commons-fileupload.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- thymeleaf dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-spring5</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- freemarker dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
<version>${freemarker.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- groovy template dependency -->
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-templates</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- jade dependency -->
|
||||
<dependency>
|
||||
<groupId>de.neuland-bfi</groupId>
|
||||
<artifactId>spring-jade4j</artifactId>
|
||||
<version>${jade.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--Testing -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.rometools</groupId>
|
||||
<artifactId>rome</artifactId>
|
||||
<version>${rome.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.thoughtworks.xstream</groupId>
|
||||
<artifactId>xstream</artifactId>
|
||||
<version>${xstream.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.scribejava</groupId>
|
||||
<artifactId>scribejava-apis</artifactId>
|
||||
<version>${scribejava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>${json.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-logger-api</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<!-- to get around bug https://github.com/junit-team/junit5/issues/1367 -->
|
||||
<scope>test</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-mvc-basics-2</finalName>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<warSourceDirectory>src/main/webapp</warSourceDirectory>
|
||||
<warName>springMvcSimple</warName>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
<outputDirectory>${deploy-path}</outputDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||
<javax.servlet.jsp-api.version>2.3.2-b02</javax.servlet.jsp-api.version>
|
||||
<javax.servlet-api.version>4.0.0</javax.servlet-api.version>
|
||||
<hibernate-validator.version>6.0.10.Final</hibernate-validator.version>
|
||||
<deploy-path>enter-location-of-server</deploy-path>
|
||||
<commons-fileupload.version>1.3.2</commons-fileupload.version>
|
||||
<org.thymeleaf-version>3.0.11.RELEASE</org.thymeleaf-version>
|
||||
<groovy.version>2.4.12</groovy.version>
|
||||
<freemarker.version>2.3.27-incubating</freemarker.version>
|
||||
<jade.version>1.2.5</jade.version>
|
||||
<rome.version>1.9.0</rome.version>
|
||||
<xstream.version>1.4.9</xstream.version>
|
||||
<scribejava.version>5.1.0</scribejava.version>
|
||||
<json.version>20180130</json.version>
|
||||
<javax.mail.version>1.6.1</javax.mail.version>
|
||||
<spring-boot.version>2.3.4.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.http.converter.feed.RssChannelHttpMessageConverter;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
import com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver;
|
||||
import com.baeldung.spring.controller.rss.JsonChannelHttpMessageConverter;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator", "com.baeldung.spring.mail", "com.baeldung.spring.service" })
|
||||
public class ApplicationConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ContentNegotiatingViewResolver viewResolver(ContentNegotiationManager cnManager) {
|
||||
ContentNegotiatingViewResolver cnvResolver = new ContentNegotiatingViewResolver();
|
||||
cnvResolver.setContentNegotiationManager(cnManager);
|
||||
List<ViewResolver> resolvers = new ArrayList<>();
|
||||
|
||||
InternalResourceViewResolver bean = new InternalResourceViewResolver("/WEB-INF/views/",".jsp");
|
||||
ArticleRssFeedViewResolver articleRssFeedViewResolver = new ArticleRssFeedViewResolver();
|
||||
|
||||
resolvers.add(bean);
|
||||
resolvers.add(articleRssFeedViewResolver);
|
||||
|
||||
cnvResolver.setViewResolvers(resolvers);
|
||||
return cnvResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MultipartResolver multipartResolver() {
|
||||
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
|
||||
multipartResolver.setMaxUploadSize(5242880);
|
||||
return multipartResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
converters.add(new StringHttpMessageConverter());
|
||||
converters.add(new RssChannelHttpMessageConverter());
|
||||
converters.add(new JsonChannelHttpMessageConverter());
|
||||
}
|
||||
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import freemarker.cache.ClassTemplateLoader;
|
||||
import freemarker.cache.TemplateLoader;
|
||||
import freemarker.template.Configuration;
|
||||
import java.util.Properties;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
||||
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||
|
||||
@ComponentScan(basePackages = { "com.baeldung.spring.mail" })
|
||||
@PropertySource(value={"classpath:application.properties"})
|
||||
public class EmailConfiguration {
|
||||
|
||||
@Value("${spring.mail.host}")
|
||||
private String mailServerHost;
|
||||
|
||||
@Value("${spring.mail.port}")
|
||||
private Integer mailServerPort;
|
||||
|
||||
@Value("${spring.mail.username}")
|
||||
private String mailServerUsername;
|
||||
|
||||
@Value("${spring.mail.password}")
|
||||
private String mailServerPassword;
|
||||
|
||||
@Value("${spring.mail.properties.mail.smtp.auth}")
|
||||
private String mailServerAuth;
|
||||
|
||||
@Value("${spring.mail.properties.mail.smtp.starttls.enable}")
|
||||
private String mailServerStartTls;
|
||||
|
||||
@Value("${spring.mail.templates.path}")
|
||||
private String mailTemplatesPath;
|
||||
|
||||
@Bean
|
||||
public JavaMailSender getJavaMailSender() {
|
||||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
||||
|
||||
mailSender.setHost(mailServerHost);
|
||||
mailSender.setPort(mailServerPort);
|
||||
|
||||
mailSender.setUsername(mailServerUsername);
|
||||
mailSender.setPassword(mailServerPassword);
|
||||
|
||||
Properties props = mailSender.getJavaMailProperties();
|
||||
props.put("mail.transport.protocol", "smtp");
|
||||
props.put("mail.smtp.auth", mailServerAuth);
|
||||
props.put("mail.smtp.starttls.enable", mailServerStartTls);
|
||||
props.put("mail.debug", "true");
|
||||
|
||||
return mailSender;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleMailMessage templateSimpleMessage() {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setText("This is the test email template for your email:\n%s\n");
|
||||
return message;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SpringTemplateEngine thymeleafTemplateEngine(ITemplateResolver templateResolver) {
|
||||
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||
templateEngine.setTemplateResolver(templateResolver);
|
||||
templateEngine.setTemplateEngineMessageSource(emailMessageSource());
|
||||
return templateEngine;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ITemplateResolver thymeleafClassLoaderTemplateResolver() {
|
||||
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
|
||||
templateResolver.setPrefix(mailTemplatesPath + "/");
|
||||
templateResolver.setSuffix(".html");
|
||||
templateResolver.setTemplateMode("HTML");
|
||||
templateResolver.setCharacterEncoding("UTF-8");
|
||||
return templateResolver;
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public ITemplateResolver thymeleafFilesystemTemplateResolver() {
|
||||
// FileTemplateResolver templateResolver = new FileTemplateResolver();
|
||||
// templateResolver.setPrefix(mailTemplatesPath + "/");
|
||||
// templateResolver.setSuffix(".html");
|
||||
// templateResolver.setTemplateMode("HTML");
|
||||
// templateResolver.setCharacterEncoding("UTF-8");
|
||||
// return templateResolver;
|
||||
// }
|
||||
|
||||
@Bean
|
||||
public FreeMarkerConfigurer freemarkerClassLoaderConfig() {
|
||||
Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);
|
||||
TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), "/" + mailTemplatesPath);
|
||||
configuration.setTemplateLoader(templateLoader);
|
||||
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
|
||||
freeMarkerConfigurer.setConfiguration(configuration);
|
||||
return freeMarkerConfigurer;
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public FreeMarkerConfigurer freemarkerFilesystemConfig() throws IOException {
|
||||
// Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);
|
||||
// TemplateLoader templateLoader = new FileTemplateLoader(new File(mailTemplatesPath));
|
||||
// configuration.setTemplateLoader(templateLoader);
|
||||
// FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
|
||||
// freeMarkerConfigurer.setConfiguration(configuration);
|
||||
// return freeMarkerConfigurer;
|
||||
// }
|
||||
|
||||
@Bean
|
||||
public ResourceBundleMessageSource emailMessageSource() {
|
||||
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("mailMessages");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
|
||||
public class FreemarkerConfiguration {
|
||||
@Bean
|
||||
public FreeMarkerConfigurer freemarkerConfig() {
|
||||
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
|
||||
freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");
|
||||
return freeMarkerConfigurer;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FreeMarkerViewResolver freemarkerViewResolver() {
|
||||
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
|
||||
resolver.setCache(true);
|
||||
resolver.setPrefix("");
|
||||
resolver.setSuffix(".ftl");
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
|
||||
public class GroovyConfiguration {
|
||||
|
||||
@Bean
|
||||
public GroovyMarkupConfigurer groovyMarkupConfigurer() {
|
||||
GroovyMarkupConfigurer configurer = new GroovyMarkupConfigurer();
|
||||
configurer.setResourceLoaderPath("/WEB-INF/views/");
|
||||
return configurer;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GroovyMarkupViewResolver thymeleafViewResolver() {
|
||||
GroovyMarkupViewResolver viewResolver = new GroovyMarkupViewResolver();
|
||||
viewResolver.setSuffix(".tpl");
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import de.neuland.jade4j.JadeConfiguration;
|
||||
import de.neuland.jade4j.spring.template.SpringTemplateLoader;
|
||||
import de.neuland.jade4j.spring.view.JadeViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
|
||||
public class JadeTemplateConfiguration {
|
||||
@Bean
|
||||
public SpringTemplateLoader templateLoader() {
|
||||
SpringTemplateLoader templateLoader = new SpringTemplateLoader();
|
||||
templateLoader.setBasePath("/WEB-INF/views/");
|
||||
templateLoader.setSuffix(".jade");
|
||||
return templateLoader;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JadeConfiguration jadeConfiguration() {
|
||||
JadeConfiguration configuration = new JadeConfiguration();
|
||||
configuration.setCaching(false);
|
||||
configuration.setTemplateLoader(templateLoader());
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
JadeViewResolver viewResolver = new JadeViewResolver();
|
||||
viewResolver.setConfiguration(jadeConfiguration());
|
||||
return viewResolver;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
// needs HTTP 2 https://github.com/spring-projects/spring-framework/wiki/HTTP-2-support
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = "com.baeldung.spring.push.controller")
|
||||
public class PushConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public InternalResourceViewResolver jspViewResolver() {
|
||||
InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||
bean.setPrefix("/WEB-INF/views/");
|
||||
bean.setSuffix(".jsp");
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**")
|
||||
.addResourceLocations("/resources/");
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
|
||||
public class ThymeleafConfiguration {
|
||||
@Bean
|
||||
public SpringTemplateEngine templateEngine() {
|
||||
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
|
||||
return templateEngine;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SpringResourceTemplateResolver thymeleafTemplateResolver() {
|
||||
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
|
||||
templateResolver.setPrefix("/WEB-INF/views/");
|
||||
templateResolver.setSuffix(".html");
|
||||
templateResolver.setTemplateMode("HTML5");
|
||||
return templateResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ThymeleafViewResolver thymeleafViewResolver() {
|
||||
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||
viewResolver.setTemplateEngine(templateEngine());
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
public class WebInitializer implements WebApplicationInitializer {
|
||||
|
||||
public void onStartup(ServletContext container) throws ServletException {
|
||||
|
||||
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
|
||||
ctx.register(ApplicationConfiguration.class);
|
||||
// ctx.register(ThymeleafConfiguration.class);
|
||||
// ctx.register(FreemarkerConfiguration.class);
|
||||
// ctx.register(GroovyConfiguration.class);
|
||||
// ctx.register(JadeTemplateConfiguration.class);
|
||||
// ctx.register(PushConfiguration.class);
|
||||
ctx.register(EmailConfiguration.class);
|
||||
// ctx.setServletContext(container);
|
||||
|
||||
// Manage the lifecycle of the root application context
|
||||
container.addListener(new ContextLoaderListener(ctx));
|
||||
|
||||
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
|
||||
|
||||
servlet.setLoadOnStartup(1);
|
||||
servlet.addMapping("/");
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@Controller
|
||||
public class AnnotationMethodHandlerAdapterExample {
|
||||
@RequestMapping("/annotedName")
|
||||
public ModelAndView getEmployeeName() {
|
||||
ModelAndView model = new ModelAndView("Greeting");
|
||||
model.addObject("message", "Dinesh");
|
||||
return model;
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baeldung.spring.domain.Customer;
|
||||
import com.baeldung.spring.validator.CustomerValidator;
|
||||
|
||||
@Controller
|
||||
public class CustomerController {
|
||||
|
||||
@Autowired
|
||||
CustomerValidator validator;
|
||||
|
||||
@RequestMapping(value = "/customer", method = RequestMethod.GET)
|
||||
public ModelAndView showForm() {
|
||||
return new ModelAndView("customerHome", "customer", new Customer());
|
||||
}
|
||||
|
||||
@PostMapping("/addCustomer")
|
||||
public String submit(@Valid @ModelAttribute("customer") final Customer customer, final BindingResult result, final ModelMap model) {
|
||||
validator.validate(customer, result);
|
||||
if (result.hasErrors()) {
|
||||
return "customerHome";
|
||||
}
|
||||
model.addAttribute("customerId", customer.getCustomerId());
|
||||
model.addAttribute("customerName", customer.getCustomerName());
|
||||
model.addAttribute("customerContact", customer.getCustomerContact());
|
||||
model.addAttribute("customerEmail", customer.getCustomerEmail());
|
||||
return "customerView";
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baeldung.spring.domain.Employee;
|
||||
|
||||
@Controller
|
||||
public class EmployeeController {
|
||||
|
||||
Map<Long, Employee> employeeMap = new HashMap<>();
|
||||
|
||||
@RequestMapping(value = "/employee", method = RequestMethod.GET)
|
||||
public ModelAndView showForm() {
|
||||
return new ModelAndView("employeeHome", "employee", new Employee());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
|
||||
public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) {
|
||||
return employeeMap.get(Id);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
|
||||
public String submit(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
return "error";
|
||||
}
|
||||
model.addAttribute("name", employee.getName());
|
||||
model.addAttribute("contactNumber", employee.getContactNumber());
|
||||
model.addAttribute("id", employee.getId());
|
||||
employeeMap.put(employee.getId(), employee);
|
||||
return "employeeView";
|
||||
}
|
||||
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@Controller
|
||||
public class FileUploadController implements HandlerExceptionResolver {
|
||||
|
||||
@RequestMapping(value = "/uploadFile", method = RequestMethod.GET)
|
||||
public String getImageView() {
|
||||
return "file";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
|
||||
public ModelAndView uploadFile(MultipartFile file) throws IOException {
|
||||
ModelAndView modelAndView = new ModelAndView("file");
|
||||
|
||||
InputStream in = file.getInputStream();
|
||||
File currDir = new File(".");
|
||||
String path = currDir.getAbsolutePath();
|
||||
FileOutputStream f = new FileOutputStream(path.substring(0, path.length() - 1) + file.getOriginalFilename());
|
||||
int ch = 0;
|
||||
while ((ch = in.read()) != -1) {
|
||||
f.write(ch);
|
||||
}
|
||||
f.flush();
|
||||
f.close();
|
||||
|
||||
modelAndView.getModel()
|
||||
.put("message", "File uploaded successfully!");
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exc) {
|
||||
ModelAndView modelAndView = new ModelAndView("file");
|
||||
if (exc instanceof MaxUploadSizeExceededException) {
|
||||
modelAndView.getModel()
|
||||
.put("message", "File size exceeds limit!");
|
||||
}
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.spring.domain.MailObject;
|
||||
import com.baeldung.spring.mail.EmailService;
|
||||
|
||||
import freemarker.template.TemplateException;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/mail")
|
||||
public class MailController {
|
||||
|
||||
@Autowired
|
||||
public EmailService emailService;
|
||||
|
||||
@Value("${attachment.invoice}")
|
||||
private String attachmentPath;
|
||||
|
||||
private static final Map<String, Map<String, String>> labels;
|
||||
|
||||
static {
|
||||
labels = new HashMap<>();
|
||||
|
||||
//Simple email
|
||||
Map<String, String> props = new HashMap<>();
|
||||
props.put("headerText", "Send Simple Email");
|
||||
props.put("messageLabel", "Message");
|
||||
props.put("additionalInfo", "");
|
||||
labels.put("send", props);
|
||||
|
||||
//Email with template
|
||||
props = new HashMap<>();
|
||||
props.put("headerText", "Send Email Using Text Template");
|
||||
props.put("messageLabel", "Template Parameter");
|
||||
props.put("additionalInfo",
|
||||
"The parameter value will be added to the following message template:<br>" +
|
||||
"<b>This is the test email template for your email:<br>'Template Parameter'</b>"
|
||||
);
|
||||
labels.put("sendTemplate", props);
|
||||
|
||||
//Email with attachment
|
||||
props = new HashMap<>();
|
||||
props.put("headerText", "Send Email With Attachment");
|
||||
props.put("messageLabel", "Message");
|
||||
props.put("additionalInfo", "To make sure that you send an attachment with this email, change the value for the 'attachment.invoice' in the application.properties file to the path to the attachment.");
|
||||
labels.put("sendAttachment", props);
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public String showEmailsPage() {
|
||||
return "emails";
|
||||
}
|
||||
|
||||
@RequestMapping(value = {"/send", "/sendTemplate", "/sendAttachment"}, method = RequestMethod.GET)
|
||||
public String createMail(Model model,
|
||||
HttpServletRequest request) {
|
||||
String action = request.getRequestURL().substring(
|
||||
request.getRequestURL().lastIndexOf("/") + 1
|
||||
);
|
||||
Map<String, String> props = labels.get(action);
|
||||
Set<String> keys = props.keySet();
|
||||
Iterator<String> iterator = keys.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
String key = iterator.next();
|
||||
model.addAttribute(key, props.get(key));
|
||||
}
|
||||
|
||||
model.addAttribute("mailObject", new MailObject());
|
||||
return "mail/send";
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/send", method = RequestMethod.POST)
|
||||
public String createMail(Model model,
|
||||
@ModelAttribute("mailObject") @Valid MailObject mailObject,
|
||||
Errors errors) {
|
||||
if (errors.hasErrors()) {
|
||||
return "mail/send";
|
||||
}
|
||||
emailService.sendSimpleMessage(mailObject.getTo(),
|
||||
mailObject.getSubject(), mailObject.getText());
|
||||
|
||||
return "emails";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/sendTemplate", method = RequestMethod.POST)
|
||||
public String createMailWithTemplate(Model model,
|
||||
@ModelAttribute("mailObject") @Valid MailObject mailObject,
|
||||
Errors errors) {
|
||||
if (errors.hasErrors()) {
|
||||
return "mail/send";
|
||||
}
|
||||
emailService.sendSimpleMessageUsingTemplate(mailObject.getTo(),
|
||||
mailObject.getSubject(),
|
||||
mailObject.getText());
|
||||
|
||||
return "redirect:/mail";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/sendAttachment", method = RequestMethod.POST)
|
||||
public String createMailWithAttachment(Model model,
|
||||
@ModelAttribute("mailObject") @Valid MailObject mailObject,
|
||||
Errors errors) {
|
||||
if (errors.hasErrors()) {
|
||||
return "mail/send";
|
||||
}
|
||||
emailService.sendMessageWithAttachment(
|
||||
mailObject.getTo(),
|
||||
mailObject.getSubject(),
|
||||
mailObject.getText(),
|
||||
attachmentPath
|
||||
);
|
||||
|
||||
return "redirect:/mail";
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = {"/sendHtml"}, method = RequestMethod.GET)
|
||||
public String getHtmlMailView(Model model,
|
||||
HttpServletRequest request) {
|
||||
|
||||
Map<String, String> templateEngines = new HashMap<>();
|
||||
templateEngines.put("Thymeleaf", "Thymeleaf");
|
||||
templateEngines.put("Freemarker", "Freemarker");
|
||||
model.addAttribute("mailObject", new MailObject());
|
||||
model.addAttribute("templateEngines", templateEngines);
|
||||
return "mail/sendHtml";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/sendHtml", method = RequestMethod.POST)
|
||||
public String createHtmlMail(Model model,
|
||||
@ModelAttribute("mailObject") @Valid MailObject mailObject,
|
||||
Errors errors) throws IOException, MessagingException, TemplateException {
|
||||
if (errors.hasErrors()) {
|
||||
return "mail/send";
|
||||
}
|
||||
|
||||
Map<String, Object> templateModel = new HashMap<>();
|
||||
templateModel.put("recipientName", mailObject.getRecipientName());
|
||||
templateModel.put("text", mailObject.getText());
|
||||
templateModel.put("senderName", mailObject.getSenderName());
|
||||
|
||||
if (mailObject.getTemplateEngine().equalsIgnoreCase("thymeleaf")) {
|
||||
emailService.sendMessageUsingThymeleafTemplate(
|
||||
mailObject.getTo(),
|
||||
mailObject.getSubject(),
|
||||
templateModel);
|
||||
} else {
|
||||
emailService.sendMessageUsingFreemarkerTemplate(
|
||||
mailObject.getTo(),
|
||||
mailObject.getSubject(),
|
||||
templateModel);
|
||||
}
|
||||
|
||||
return "redirect:/mail";
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@Controller
|
||||
public class RequestMappingHandlerAdapterExample {
|
||||
@RequestMapping("/requestName")
|
||||
public ModelAndView getEmployeeName() {
|
||||
ModelAndView model = new ModelAndView("Greeting");
|
||||
model.addObject("message", "Madhwal");
|
||||
return model;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import com.baeldung.spring.domain.Employee;
|
||||
import com.baeldung.spring.exception.InvalidRequestException;
|
||||
import com.baeldung.spring.service.EmployeeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value="/api")
|
||||
public class RequestMethodController {
|
||||
|
||||
@Autowired
|
||||
EmployeeService service;
|
||||
|
||||
@RequestMapping(value = "/employees", produces = "application/json", method={RequestMethod.GET,RequestMethod.POST})
|
||||
public List<Employee> findEmployees()
|
||||
throws InvalidRequestException {
|
||||
return service.getEmployeeList();
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.mvc.AbstractController;
|
||||
|
||||
public class SimpleControllerHandlerAdapterExample extends AbstractController {
|
||||
@Override
|
||||
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
ModelAndView model = new ModelAndView("Greeting");
|
||||
model.addObject("message", "Dinesh Madhwal");
|
||||
return model;
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import com.baeldung.spring.domain.User;
|
||||
|
||||
@Controller
|
||||
public class UserController {
|
||||
|
||||
@GetMapping("/registration")
|
||||
public String getRegistration(Model model) {
|
||||
model.addAttribute("user", new User());
|
||||
return "registration";
|
||||
}
|
||||
|
||||
@GetMapping("/registration-thymeleaf")
|
||||
public String getRegistrationThymeleaf(Model model) {
|
||||
model.addAttribute("user", new User());
|
||||
return "registration-thymeleaf";
|
||||
}
|
||||
|
||||
@GetMapping("/registration-freemarker")
|
||||
public String getRegistrationFreemarker(Model model) {
|
||||
model.addAttribute("user", new User());
|
||||
return "registration-freemarker";
|
||||
}
|
||||
|
||||
@GetMapping("/registration-groovy")
|
||||
public String getRegistrationGroovy(Model model) {
|
||||
model.addAttribute("user", new User());
|
||||
return "registration-groovy";
|
||||
}
|
||||
|
||||
@GetMapping("/registration-jade")
|
||||
public String getRegistrationJade(Model model) {
|
||||
model.addAttribute("user", new User());
|
||||
return "registration-jade";
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
@ResponseBody
|
||||
public void register(User user){
|
||||
System.out.println(user.getEmail());
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.spring.controller.rss;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
public class Article implements Serializable {
|
||||
private String link;
|
||||
private String title;
|
||||
private String description;
|
||||
private Date publishedDate;
|
||||
private String author;
|
||||
|
||||
public String getLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
public void setLink(String link) {
|
||||
this.link = link;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Date getPublishedDate() {
|
||||
return publishedDate;
|
||||
}
|
||||
|
||||
public void setPublishedDate(Date publishedDate) {
|
||||
this.publishedDate = publishedDate;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Article{" +
|
||||
"link='" + link + '\'' +
|
||||
", title='" + title + '\'' +
|
||||
", description='" + description + '\'' +
|
||||
", publishedDate=" + publishedDate +
|
||||
", author='" + author + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.spring.controller.rss;
|
||||
|
||||
import com.rometools.rome.feed.rss.Channel;
|
||||
import com.rometools.rome.feed.rss.Description;
|
||||
import com.rometools.rome.feed.rss.Item;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.servlet.view.feed.AbstractRssFeedView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service("articleFeedView")
|
||||
public class ArticleFeedView extends AbstractRssFeedView {
|
||||
|
||||
protected Channel newFeed() {
|
||||
Channel channel = new Channel("rss_2.0");
|
||||
channel.setLink("http://localhost:8080/spring-mvc-simple/rss");
|
||||
channel.setTitle("Article Feed");
|
||||
channel.setDescription("Article Feed Description");
|
||||
channel.setPubDate(new Date());
|
||||
return channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Item> buildFeedItems(Map<String, Object> map, HttpServletRequest httpStRequest, HttpServletResponse httpStResponse) throws Exception {
|
||||
List list = new ArrayList<Item>();
|
||||
|
||||
Item item1 = new Item();
|
||||
item1.setLink("http://www.baeldung.com/netty-exception-handling");
|
||||
item1.setTitle("Exceptions in Netty");
|
||||
Description description1 = new Description();
|
||||
description1.setValue("In this quick article, we’ll be looking at exception handling in Netty.");
|
||||
item1.setDescription(description1);
|
||||
item1.setPubDate(new Date());
|
||||
item1.setAuthor("Carlos");
|
||||
|
||||
Item item2 = new Item();
|
||||
item2.setLink("http://www.baeldung.com/cockroachdb-java");
|
||||
item2.setTitle("Guide to CockroachDB in Java");
|
||||
Description description2 = new Description();
|
||||
description2.setValue("This tutorial is an introductory guide to using CockroachDB with Java.");
|
||||
item2.setDescription(description2);
|
||||
item2.setPubDate(new Date());
|
||||
item2.setAuthor("Baeldung");
|
||||
|
||||
list.add(item1);
|
||||
list.add(item2);
|
||||
return list;
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.spring.controller.rss;
|
||||
|
||||
import com.rometools.rome.feed.rss.Channel;
|
||||
import com.rometools.rome.feed.rss.Description;
|
||||
import com.rometools.rome.feed.rss.Item;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
public class ArticleRssController {
|
||||
|
||||
@GetMapping(value = "/rss1")
|
||||
public String articleMvcFeed() {
|
||||
return "articleFeedView";
|
||||
}
|
||||
|
||||
@GetMapping(value = "/rss2", produces = {"application/rss+xml", "application/rss+json"})
|
||||
@ResponseBody
|
||||
public Channel articleHttpFeed() {
|
||||
List<Article> items = new ArrayList<>();
|
||||
Article item1 = new Article();
|
||||
item1.setLink("http://www.baeldung.com/netty-exception-handling");
|
||||
item1.setTitle("Exceptions in Netty");
|
||||
item1.setDescription("In this quick article, we’ll be looking at exception handling in Netty.");
|
||||
item1.setPublishedDate(new Date());
|
||||
item1.setAuthor("Carlos");
|
||||
|
||||
Article item2 = new Article();
|
||||
item2.setLink("http://www.baeldung.com/cockroachdb-java");
|
||||
item2.setTitle("Guide to CockroachDB in Java");
|
||||
item2.setDescription("This tutorial is an introductory guide to using CockroachDB with Java.");
|
||||
item2.setPublishedDate(new Date());
|
||||
item2.setAuthor("Baeldung");
|
||||
|
||||
items.add(item1);
|
||||
items.add(item2);
|
||||
Channel channelData = buildChannel(items);
|
||||
|
||||
return channelData;
|
||||
}
|
||||
|
||||
private Channel buildChannel(List<Article> articles){
|
||||
Channel channel = new Channel("rss_2.0");
|
||||
channel.setLink("http://localhost:8080/spring-mvc-simple/rss");
|
||||
channel.setTitle("Article Feed");
|
||||
channel.setDescription("Article Feed Description");
|
||||
channel.setPubDate(new Date());
|
||||
|
||||
List<Item> items = new ArrayList<>();
|
||||
for (Article article : articles) {
|
||||
Item item = new Item();
|
||||
item.setLink(article.getLink());
|
||||
item.setTitle(article.getTitle());
|
||||
Description description1 = new Description();
|
||||
description1.setValue(article.getDescription());
|
||||
item.setDescription(description1);
|
||||
item.setPubDate(article.getPublishedDate());
|
||||
item.setAuthor(article.getAuthor());
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
channel.setItems(items);
|
||||
return channel;
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.spring.controller.rss;
|
||||
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class ArticleRssFeedViewResolver implements ViewResolver {
|
||||
|
||||
@Override
|
||||
public View resolveViewName(String s, Locale locale) throws Exception {
|
||||
ArticleFeedView articleFeedView = new ArticleFeedView();
|
||||
return articleFeedView;
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.spring.controller.rss;
|
||||
|
||||
import com.rometools.rome.feed.rss.Channel;
|
||||
import com.rometools.rome.io.FeedException;
|
||||
import com.rometools.rome.io.WireFeedOutput;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.XML;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.AbstractHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class JsonChannelHttpMessageConverter extends AbstractHttpMessageConverter<Channel> {
|
||||
public JsonChannelHttpMessageConverter(){
|
||||
super(new MediaType("application", "rss+json"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean supports(Class<?> aClass) {
|
||||
return Channel.class.isAssignableFrom(aClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Channel readInternal(Class<? extends Channel> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeInternal(Channel wireFeed, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
|
||||
WireFeedOutput feedOutput = new WireFeedOutput();
|
||||
|
||||
try {
|
||||
String xmlStr = feedOutput.outputString(wireFeed, true);
|
||||
JSONObject xmlJSONObj = XML.toJSONObject(xmlStr);
|
||||
String jsonPrettyPrintString = xmlJSONObj.toString(4);
|
||||
|
||||
outputMessage.getBody().write(jsonPrettyPrintString.getBytes());
|
||||
} catch (JSONException | FeedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.spring.controller.scribe;
|
||||
|
||||
import com.github.scribejava.apis.GitHubApi;
|
||||
import com.github.scribejava.core.builder.ServiceBuilder;
|
||||
import com.github.scribejava.core.model.*;
|
||||
import com.github.scribejava.core.oauth.OAuth20Service;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("github")
|
||||
public class GithubController {
|
||||
private OAuth20Service createService(String state) {
|
||||
return new ServiceBuilder("e1f8d4f1a5c71467a159")
|
||||
.apiSecret("4851597541a8f33a4f1bf1c70f3cedcfefbeb13b")
|
||||
.state(state)
|
||||
.callback("http://localhost:8080/spring-mvc-simple/github/callback")
|
||||
.build(GitHubApi.instance());
|
||||
}
|
||||
|
||||
@GetMapping(value = "/authorization")
|
||||
public RedirectView authorization(HttpServletRequest servletReq) throws InterruptedException, ExecutionException, IOException {
|
||||
String state = String.valueOf(new Random().nextInt(999_999));
|
||||
OAuth20Service githubService = createService(state);
|
||||
servletReq.getSession().setAttribute("state", state);
|
||||
|
||||
String authorizationUrl = githubService.getAuthorizationUrl();
|
||||
RedirectView redirectView = new RedirectView();
|
||||
redirectView.setUrl(authorizationUrl);
|
||||
return redirectView;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/callback", produces = "text/plain")
|
||||
@ResponseBody
|
||||
public String callback(HttpServletRequest servletReq, @RequestParam("code") String code, @RequestParam("state") String state) throws InterruptedException, ExecutionException, IOException {
|
||||
String initialState = (String) servletReq.getSession().getAttribute("state");
|
||||
if(initialState.equals(state)) {
|
||||
OAuth20Service githubService = createService(initialState);
|
||||
OAuth2AccessToken accessToken = githubService.getAccessToken(code);
|
||||
|
||||
OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.github.com/user");
|
||||
githubService.signRequest(accessToken, request);
|
||||
Response response = githubService.execute(request);
|
||||
|
||||
return response.getBody();
|
||||
}
|
||||
return "Error";
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.spring.controller.scribe;
|
||||
|
||||
import com.github.scribejava.apis.TwitterApi;
|
||||
import com.github.scribejava.core.builder.ServiceBuilder;
|
||||
import com.github.scribejava.core.model.*;
|
||||
import com.github.scribejava.core.oauth.OAuth10aService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("twitter")
|
||||
public class TwitterController {
|
||||
|
||||
private OAuth10aService createService() {
|
||||
return new ServiceBuilder("PSRszoHhRDVhyo2RIkThEbWko")
|
||||
.apiSecret("prpJbz03DcGRN46sb4ucdSYtVxG8unUKhcnu3an5ItXbEOuenL")
|
||||
.callback("http://localhost:8080/spring-mvc-simple/twitter/callback")
|
||||
.build(TwitterApi.instance());
|
||||
}
|
||||
|
||||
@GetMapping(value = "/authorization")
|
||||
public RedirectView authorization(HttpServletRequest servletReq) throws InterruptedException, ExecutionException, IOException {
|
||||
OAuth10aService twitterService = createService();
|
||||
|
||||
OAuth1RequestToken requestToken = twitterService.getRequestToken();
|
||||
String authorizationUrl = twitterService.getAuthorizationUrl(requestToken);
|
||||
servletReq.getSession().setAttribute("requestToken", requestToken);
|
||||
|
||||
RedirectView redirectView = new RedirectView();
|
||||
redirectView.setUrl(authorizationUrl);
|
||||
return redirectView;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/callback", produces = "text/plain")
|
||||
@ResponseBody
|
||||
public String callback(HttpServletRequest servletReq, @RequestParam("oauth_verifier") String oauthV) throws InterruptedException, ExecutionException, IOException {
|
||||
OAuth10aService twitterService = createService();
|
||||
OAuth1RequestToken requestToken = (OAuth1RequestToken) servletReq.getSession().getAttribute("requestToken");
|
||||
OAuth1AccessToken accessToken = twitterService.getAccessToken(requestToken, oauthV);
|
||||
|
||||
OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json");
|
||||
twitterService.signRequest(accessToken, request);
|
||||
Response response = twitterService.execute(request);
|
||||
|
||||
return response.getBody();
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.spring.domain;
|
||||
|
||||
public class Customer {
|
||||
private String customerId;
|
||||
private String customerName;
|
||||
private String customerContact;
|
||||
private String customerEmail;
|
||||
|
||||
public Customer() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(String customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
|
||||
public String getCustomerContact() {
|
||||
return customerContact;
|
||||
}
|
||||
|
||||
public void setCustomerContact(String customerContact) {
|
||||
this.customerContact = customerContact;
|
||||
}
|
||||
|
||||
public String getCustomerEmail() {
|
||||
return customerEmail;
|
||||
}
|
||||
|
||||
public void setCustomerEmail(String customerEmail) {
|
||||
this.customerEmail = customerEmail;
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.spring.domain;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private long id;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 5)
|
||||
private String name;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 7)
|
||||
private String contactNumber;
|
||||
|
||||
public Employee() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getContactNumber() {
|
||||
return contactNumber;
|
||||
}
|
||||
|
||||
public void setContactNumber(final String contactNumber) {
|
||||
this.contactNumber = contactNumber;
|
||||
}
|
||||
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.spring.domain;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
/**
|
||||
* Created by Olga on 7/20/2016.
|
||||
*/
|
||||
public class MailObject {
|
||||
@Email
|
||||
@NotNull
|
||||
@Size(min = 1, message = "Please, set an email address to send the message to it")
|
||||
private String to;
|
||||
private String recipientName;
|
||||
private String subject;
|
||||
private String text;
|
||||
private String senderName;
|
||||
private String templateEngine;
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getRecipientName() {
|
||||
return recipientName;
|
||||
}
|
||||
|
||||
public void setRecipientName(String recipientName) {
|
||||
this.recipientName = recipientName;
|
||||
}
|
||||
|
||||
public String getSenderName() {
|
||||
return senderName;
|
||||
}
|
||||
|
||||
public void setSenderName(String senderName) {
|
||||
this.senderName = senderName;
|
||||
}
|
||||
|
||||
public String getTemplateEngine() {
|
||||
return templateEngine;
|
||||
}
|
||||
|
||||
public void setTemplateEngine(String templateEngine) {
|
||||
this.templateEngine = templateEngine;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user