diff --git a/build.gradle b/build.gradle index 02a13b5..7198a5c 100644 --- a/build.gradle +++ b/build.gradle @@ -13,10 +13,6 @@ apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' -bootJar { - baseName = 'gs-serving-web-content' - version = '0.1.0' -} repositories { mavenCentral() @@ -27,9 +23,13 @@ targetCompatibility = 17 dependencies { implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web' - implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' implementation group: 'org.springframework.boot', name: 'spring-boot-devtools' + implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa' + implementation group: 'com.h2database', name: 'h2' + implementation group: 'javax.xml.bind', name: 'jaxb-api' + implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final' + implementation group: 'javassist', name: 'javassist', version: '3.12.1.GA' implementation group: 'junit', name: 'junit' } diff --git a/src/main/java/email/EmailController.java b/src/main/java/email/EmailController.java index 1249fc4..6d1fbf3 100644 --- a/src/main/java/email/EmailController.java +++ b/src/main/java/email/EmailController.java @@ -1,6 +1,8 @@ package email; +import email.model.Email; import email.model.EmailForm; +import email.service.EmailService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -9,6 +11,11 @@ import org.springframework.web.bind.annotation.PostMapping; @Controller public class EmailController { + private final EmailService emailService; + + public EmailController(EmailService emailService) { + this.emailService = emailService; + } @GetMapping("/") public String indexForm(Model model) { @@ -22,7 +29,14 @@ public class EmailController { model.addAttribute("error", "'Кому' не должно быть пустым"); return "index"; } - return "result"; + emailService.save(new Email(emailForm.getTo(), emailForm.getSubject(), emailForm.getMessage())); + model.addAttribute("emails", emailService.getAllEmails()); + return "list"; } + @GetMapping("/list") + public String list(Model model) { + model.addAttribute("emails", emailService.getAllEmails()); + return "list"; + } } diff --git a/src/main/java/email/configuration/MvcConfiguration.java b/src/main/java/email/configuration/MvcConfiguration.java new file mode 100644 index 0000000..6f12a16 --- /dev/null +++ b/src/main/java/email/configuration/MvcConfiguration.java @@ -0,0 +1,49 @@ +package email.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.i18n.CookieLocaleResolver; +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; + +@Configuration +public class MvcConfiguration implements WebMvcConfigurer { + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/{articlename:\\w+}"); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry + .addResourceHandler("/webjars/**") + .addResourceLocations("/webjars/"); + } + + @Bean + public LocaleResolver localeResolver() { + return new CookieLocaleResolver(); + } + + @Bean + public LocaleChangeInterceptor localeInterceptor() { + LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor(); + localeInterceptor.setParamName("lang"); + return localeInterceptor; + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(localeInterceptor()); + } + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**"); + } +} diff --git a/src/main/java/email/core/BaseEntity.java b/src/main/java/email/core/BaseEntity.java new file mode 100644 index 0000000..fced11c --- /dev/null +++ b/src/main/java/email/core/BaseEntity.java @@ -0,0 +1,83 @@ +package email.core; + +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; +import javax.persistence.Version; +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +@MappedSuperclass +public abstract class BaseEntity implements Serializable, Comparable { + @Id + @GeneratedValue(strategy = GenerationType.TABLE) + private Integer id; + + @Version + private Integer version; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getVersion() { + return version; + } + + public void setVersion(Integer version) { + this.version = version; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!getClass().isAssignableFrom(obj.getClass())) { + return false; + } + BaseEntity other = (BaseEntity) obj; + if (id == null) { + if (other.id != null) { + return false; + } + } else if (!id.equals(other.id)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (id == null ? 0 : id.hashCode()); + return result; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "{" + + "id=" + id + + ", version=" + version + + '}'; + } + + @Override + public int compareTo(@NotNull BaseEntity o) { + return id != null ? id.compareTo(o.getId()) : -1; + } + + public void reset() { + this.id = null; + this.version = null; + } +} \ No newline at end of file diff --git a/src/main/java/email/model/Email.java b/src/main/java/email/model/Email.java new file mode 100644 index 0000000..d2eefe3 --- /dev/null +++ b/src/main/java/email/model/Email.java @@ -0,0 +1,46 @@ +package email.model; + +import email.core.BaseEntity; + +import javax.persistence.Entity; + +@Entity +public class Email extends BaseEntity { + private String to; + private String subject; + private String message; + + public Email() { + + } + + public Email(String to, String subject, String message) { + this.to = to; + this.subject = subject; + this.message = message; + } + + 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 getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/main/java/email/repository/EmailRepository.java b/src/main/java/email/repository/EmailRepository.java new file mode 100644 index 0000000..80be2c6 --- /dev/null +++ b/src/main/java/email/repository/EmailRepository.java @@ -0,0 +1,10 @@ +package email.repository; + +import email.model.Email; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface EmailRepository extends JpaRepository { +} + diff --git a/src/main/java/email/service/EmailService.java b/src/main/java/email/service/EmailService.java new file mode 100644 index 0000000..237a6ea --- /dev/null +++ b/src/main/java/email/service/EmailService.java @@ -0,0 +1,33 @@ +package email.service; + +import email.model.Email; +import email.repository.EmailRepository; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class EmailService { + private final EmailRepository emailRepository; + + public EmailService(EmailRepository emailRepository) { + this.emailRepository = emailRepository; + } + + public List getAllEmails() { + return emailRepository.findAll(); + } + + public Email save(Email email) { + return emailRepository.save(email); + } + + public Email getEmailById(Integer id) { + return emailRepository.getOne(id); + } + + public void delete(Integer setId) { + emailRepository.deleteById(setId); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..fc200ed --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,14 @@ +# Server Settings +spring.main.banner-mode=off +server.port=8080 +# Log settings (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF) +logging.level.ru.ulstu=DEBUG +#JPA +# go to http://localhost:8080/h2-console +spring.datasource.url=jdbc:h2:file:./data/emails +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.h2.console.enabled=true +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file diff --git a/src/main/resources/public/img/logo.png b/src/main/resources/public/img/logo.png new file mode 100644 index 0000000..da902fa Binary files /dev/null and b/src/main/resources/public/img/logo.png differ diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index f37aa8f..16df476 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -2,29 +2,77 @@ Простая обработка формы на Spring MVC - + + + -

Форма

-
-

- - - - - - - - - - - -
Тема:
Кому:
Сообщение: