minor cleanup work

This commit is contained in:
eugenp 2016-09-25 15:18:08 +03:00
parent db3a847240
commit a351667adc
7 changed files with 318 additions and 324 deletions

View File

@ -1,189 +1,189 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>spring-thymeleaf</artifactId> <artifactId>spring-thymeleaf</artifactId>
<version>0.1-SNAPSHOT</version> <version>0.1-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<properties> <properties>
<java-version>1.7</java-version> <java-version>1.7</java-version>
<!-- spring --> <!-- spring -->
<org.springframework-version>4.3.3.RELEASE</org.springframework-version> <org.springframework-version>4.3.3.RELEASE</org.springframework-version>
<javax.servlet-version>3.0.1</javax.servlet-version> <javax.servlet-version>3.0.1</javax.servlet-version>
<!-- logging --> <!-- logging -->
<org.slf4j.version>1.7.12</org.slf4j.version> <org.slf4j.version>1.7.12</org.slf4j.version>
<logback.version>1.1.3</logback.version> <logback.version>1.1.3</logback.version>
<!-- thymeleaf --> <!-- thymeleaf -->
<org.thymeleaf-version>2.1.4.RELEASE</org.thymeleaf-version> <org.thymeleaf-version>2.1.4.RELEASE</org.thymeleaf-version>
<!-- validation --> <!-- validation -->
<javax.validation-version>1.1.0.Final</javax.validation-version> <javax.validation-version>1.1.0.Final</javax.validation-version>
<org.hibernate-version>5.1.2.Final</org.hibernate-version> <org.hibernate-version>5.1.2.Final</org.hibernate-version>
<!-- Maven plugins --> <!-- Maven plugins -->
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version> <maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version> <maven-war-plugin.version>2.6</maven-war-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version> <cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
</properties> </properties>
<dependencies> <dependencies>
<!-- Spring --> <!-- Spring -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version> <version>${org.springframework-version}</version>
<exclusions> <exclusions>
<!-- Exclude Commons Logging in favor of SLF4j --> <!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion> <exclusion>
<groupId>commons-logging</groupId> <groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId> <artifactId>commons-logging</artifactId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version> <version>${org.springframework-version}</version>
</dependency> </dependency>
<!-- Spring Security --> <!-- Spring Security -->
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId> <artifactId>spring-security-web</artifactId>
<version>4.1.3.RELEASE</version> <version>4.1.3.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId> <artifactId>spring-security-config</artifactId>
<version>4.1.3.RELEASE</version> <version>4.1.3.RELEASE</version>
</dependency> </dependency>
<!-- Thymeleaf --> <!-- Thymeleaf -->
<dependency> <dependency>
<groupId>org.thymeleaf</groupId> <groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId> <artifactId>thymeleaf</artifactId>
<version>${org.thymeleaf-version}</version> <version>${org.thymeleaf-version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.thymeleaf</groupId> <groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId> <artifactId>thymeleaf-spring4</artifactId>
<version>${org.thymeleaf-version}</version> <version>${org.thymeleaf-version}</version>
</dependency> </dependency>
<!-- Logging --> <!-- Logging -->
<!-- logging --> <!-- logging -->
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version> <version>${org.slf4j.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<version>${logback.version}</version> <version>${logback.version}</version>
<!-- <scope>runtime</scope> --> <!-- <scope>runtime</scope> -->
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId> <artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version> <version>${org.slf4j.version}</version>
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl --> <!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
</dependency> </dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly --> <dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId> <artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version> <version>${org.slf4j.version}</version>
</dependency> </dependency>
<!-- Servlet --> <!-- Servlet -->
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-version}</version> <version>${javax.servlet-version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- Validation --> <!-- Validation -->
<dependency> <dependency>
<groupId>javax.validation</groupId> <groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId> <artifactId>validation-api</artifactId>
<version>${javax.validation-version}</version> <version>${javax.validation-version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId> <artifactId>hibernate-validator</artifactId>
<version>${org.hibernate-version}</version> <version>${org.hibernate-version}</version>
</dependency> </dependency>
<!-- test scoped --> <!-- test scoped -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
<version>4.1.3.RELEASE</version> <version>4.1.3.RELEASE</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test --> <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId> <artifactId>spring-security-test</artifactId>
<version>4.1.3.RELEASE</version> <version>4.1.3.RELEASE</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/junit/junit --> <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version> <version>${maven-compiler-plugin.version}</version>
<configuration> <configuration>
<source>${java-version}</source> <source>${java-version}</source>
<target>${java-version}</target> <target>${java-version}</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId> <artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version> <version>${maven-war-plugin.version}</version>
<configuration> <configuration>
<failOnMissingWebXml>false</failOnMissingWebXml> <failOnMissingWebXml>false</failOnMissingWebXml>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version> <version>${maven-surefire-plugin.version}</version>
<configuration> <configuration>
<excludes> <excludes>
</excludes> </excludes>
<systemPropertyVariables> <systemPropertyVariables>
</systemPropertyVariables> </systemPropertyVariables>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.codehaus.cargo</groupId> <groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId> <artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo-maven2-plugin.version}</version> <version>${cargo-maven2-plugin.version}</version>
<configuration> <configuration>
<wait>true</wait> <wait>true</wait>
<container> <container>
<containerId>jetty8x</containerId> <containerId>jetty8x</containerId>
<type>embedded</type> <type>embedded</type>
<systemProperties> <systemProperties>
</systemProperties> </systemProperties>
</container> </container>
<configuration> <configuration>
<properties> <properties>
<cargo.servlet.port>8082</cargo.servlet.port> <cargo.servlet.port>8082</cargo.servlet.port>
</properties> </properties>
</configuration> </configuration>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -9,28 +9,28 @@ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatche
*/ */
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer { public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
public WebApp() { public WebApp() {
super(); super();
} }
@Override @Override
protected Class<?>[] getRootConfigClasses() { protected Class<?>[] getRootConfigClasses() {
return null; return null;
} }
@Override @Override
protected Class<?>[] getServletConfigClasses() { protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }; return new Class<?>[] { WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class };
} }
@Override @Override
protected String[] getServletMappings() { protected String[] getServletMappings() {
return new String[] { "/" }; return new String[] { "/" };
} }
@Override @Override
protected void customizeRegistration(final Dynamic registration) { protected void customizeRegistration(final Dynamic registration) {
super.customizeRegistration(registration); super.customizeRegistration(registration);
} }
} }

View File

@ -24,51 +24,51 @@ import com.baeldung.thymeleaf.formatter.NameFormatter;
*/ */
public class WebMVCConfig extends WebMvcConfigurerAdapter { public class WebMVCConfig extends WebMvcConfigurerAdapter {
@Bean @Bean
@Description("Thymeleaf Template Resolver") @Description("Thymeleaf Template Resolver")
public ServletContextTemplateResolver templateResolver() { public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html"); templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5"); templateResolver.setTemplateMode("HTML5");
return templateResolver; return templateResolver;
} }
@Bean @Bean
@Description("Thymeleaf Template Engine") @Description("Thymeleaf Template Engine")
public SpringTemplateEngine templateEngine() { public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine(); SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver()); templateEngine.setTemplateResolver(templateResolver());
return templateEngine; return templateEngine;
} }
@Bean @Bean
@Description("Thymeleaf View Resolver") @Description("Thymeleaf View Resolver")
public ThymeleafViewResolver viewResolver() { public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine()); viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1); viewResolver.setOrder(1);
return viewResolver; return viewResolver;
} }
@Bean @Bean
@Description("Spring Message Resolver") @Description("Spring Message Resolver")
public ResourceBundleMessageSource messageSource() { public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages"); messageSource.setBasename("messages");
return messageSource; return messageSource;
} }
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/"); registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
} }
@Override @Override
@Description("Custom Conversion Service") @Description("Custom Conversion Service")
public void addFormatters(FormatterRegistry registry) { public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(new NameFormatter()); registry.addFormatter(new NameFormatter());
} }
} }

View File

@ -37,13 +37,7 @@ public class WebMVCSecurity extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(final HttpSecurity http) throws Exception { protected void configure(final HttpSecurity http) throws Exception {
http http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic()
;
} }
} }

View File

@ -20,50 +20,50 @@ import org.springframework.web.bind.annotation.RequestMethod;
@Controller @Controller
public class StudentController { public class StudentController {
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST) @RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) { public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
if (!errors.hasErrors()) { if (!errors.hasErrors()) {
// get mock objects // get mock objects
List<Student> students = buildStudents(); List<Student> students = buildStudents();
// add current student // add current student
students.add(student); students.add(student);
model.addAttribute("students", students); model.addAttribute("students", students);
} }
return ((errors.hasErrors()) ? "addStudent" : "listStudents"); return ((errors.hasErrors()) ? "addStudent" : "listStudents");
} }
@RequestMapping(value = "/addStudent", method = RequestMethod.GET) @RequestMapping(value = "/addStudent", method = RequestMethod.GET)
public String addStudent(Model model) { public String addStudent(Model model) {
model.addAttribute("student", new Student()); model.addAttribute("student", new Student());
return "addStudent"; return "addStudent";
} }
@RequestMapping(value = "/listStudents", method = RequestMethod.GET) @RequestMapping(value = "/listStudents", method = RequestMethod.GET)
public String listStudent(Model model) { public String listStudent(Model model) {
model.addAttribute("students", buildStudents()); model.addAttribute("students", buildStudents());
return "listStudents"; return "listStudents";
} }
private List<Student> buildStudents() { private List<Student> buildStudents() {
List<Student> students = new ArrayList<Student>(); List<Student> students = new ArrayList<Student>();
Student student1 = new Student(); Student student1 = new Student();
student1.setId(1001); student1.setId(1001);
student1.setName("John Smith"); student1.setName("John Smith");
student1.setGender('M'); student1.setGender('M');
student1.setPercentage(Float.valueOf("80.45")); student1.setPercentage(Float.valueOf("80.45"));
students.add(student1); students.add(student1);
Student student2 = new Student(); Student student2 = new Student();
student2.setId(1002); student2.setId(1002);
student2.setName("Jane Williams"); student2.setName("Jane Williams");
student2.setGender('F'); student2.setGender('F');
student2.setPercentage(Float.valueOf("60.25")); student2.setPercentage(Float.valueOf("60.25"));
students.add(student2); students.add(student2);
return students; return students;
} }
} }

View File

@ -12,49 +12,49 @@ import javax.validation.constraints.NotNull;
*/ */
public class Student implements Serializable { public class Student implements Serializable {
private static final long serialVersionUID = -8582553475226281591L; private static final long serialVersionUID = -8582553475226281591L;
@NotNull(message = "Student ID is required.") @NotNull(message = "Student ID is required.")
@Min(value = 1000, message = "Student ID must be at least 4 digits.") @Min(value = 1000, message = "Student ID must be at least 4 digits.")
private Integer id; private Integer id;
@NotNull(message = "Student name is required.") @NotNull(message = "Student name is required.")
private String name; private String name;
@NotNull(message = "Student gender is required.") @NotNull(message = "Student gender is required.")
private Character gender; private Character gender;
private Float percentage; private Float percentage;
public Integer getId() { public Integer getId() {
return id; return id;
} }
public void setId(Integer id) { public void setId(Integer id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Character getGender() { public Character getGender() {
return gender; return gender;
} }
public void setGender(Character gender) { public void setGender(Character gender) {
this.gender = gender; this.gender = gender;
} }
public Float getPercentage() { public Float getPercentage() {
return percentage; return percentage;
} }
public void setPercentage(Float percentage) { public void setPercentage(Float percentage) {
this.percentage = percentage; this.percentage = percentage;
} }
} }

View File

@ -31,7 +31,7 @@ import com.baeldung.thymeleaf.config.WebMVCSecurity;
@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }) @ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class })
public class CsrfEnabledIntegrationTest { public class CsrfEnabledIntegrationTest {
@Autowired @Autowired
WebApplicationContext wac; WebApplicationContext wac;
@Autowired @Autowired
MockHttpSession session; MockHttpSession session;
@ -52,12 +52,12 @@ public class CsrfEnabledIntegrationTest {
@Test @Test
public void addStudentWithoutCSRF() throws Exception { public void addStudentWithoutCSRF() throws Exception {
mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser())).andExpect(status().isForbidden()); mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser())).andExpect(status().isForbidden());
} }
@Test @Test
public void addStudentWithCSRF() throws Exception { public void addStudentWithCSRF() throws Exception {
mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser()).with(csrf())).andExpect(status().isOk()); mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser()).with(csrf())).andExpect(status().isOk());
} }
} }