minor cleanup work
This commit is contained in:
parent
db3a847240
commit
a351667adc
@ -1,189 +1,189 @@
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-thymeleaf</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<properties>
|
||||
<java-version>1.7</java-version>
|
||||
<!-- spring -->
|
||||
<org.springframework-version>4.3.3.RELEASE</org.springframework-version>
|
||||
<javax.servlet-version>3.0.1</javax.servlet-version>
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.12</org.slf4j.version>
|
||||
<logback.version>1.1.3</logback.version>
|
||||
<!-- thymeleaf -->
|
||||
<org.thymeleaf-version>2.1.4.RELEASE</org.thymeleaf-version>
|
||||
<!-- validation -->
|
||||
<javax.validation-version>1.1.0.Final</javax.validation-version>
|
||||
<org.hibernate-version>5.1.2.Final</org.hibernate-version>
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-thymeleaf</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<properties>
|
||||
<java-version>1.7</java-version>
|
||||
<!-- spring -->
|
||||
<org.springframework-version>4.3.3.RELEASE</org.springframework-version>
|
||||
<javax.servlet-version>3.0.1</javax.servlet-version>
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.12</org.slf4j.version>
|
||||
<logback.version>1.1.3</logback.version>
|
||||
<!-- thymeleaf -->
|
||||
<org.thymeleaf-version>2.1.4.RELEASE</org.thymeleaf-version>
|
||||
<!-- validation -->
|
||||
<javax.validation-version>1.1.0.Final</javax.validation-version>
|
||||
<org.hibernate-version>5.1.2.Final</org.hibernate-version>
|
||||
|
||||
<!-- Maven plugins -->
|
||||
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
||||
</properties>
|
||||
<!-- Maven plugins -->
|
||||
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${org.springframework-version}</version>
|
||||
<exclusions>
|
||||
<!-- Exclude Commons Logging in favor of SLF4j -->
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${org.springframework-version}</version>
|
||||
</dependency>
|
||||
<!-- Spring Security -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-web</artifactId>
|
||||
<version>4.1.3.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-config</artifactId>
|
||||
<version>4.1.3.RELEASE</version>
|
||||
</dependency>
|
||||
<!-- Thymeleaf -->
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-spring4</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
<!-- Logging -->
|
||||
<!-- logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
<!-- <scope>runtime</scope> -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<!-- Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Validation -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${org.hibernate-version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependencies>
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${org.springframework-version}</version>
|
||||
<exclusions>
|
||||
<!-- Exclude Commons Logging in favor of SLF4j -->
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${org.springframework-version}</version>
|
||||
</dependency>
|
||||
<!-- Spring Security -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-web</artifactId>
|
||||
<version>4.1.3.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-config</artifactId>
|
||||
<version>4.1.3.RELEASE</version>
|
||||
</dependency>
|
||||
<!-- Thymeleaf -->
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-spring4</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
<!-- Logging -->
|
||||
<!-- logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
<!-- <scope>runtime</scope> -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<!-- Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Validation -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${org.hibernate-version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>4.1.3.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>4.1.3.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<version>4.1.3.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<version>4.1.3.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java-version}</source>
|
||||
<target>${java-version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
</excludes>
|
||||
<systemPropertyVariables>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven2-plugin</artifactId>
|
||||
<version>${cargo-maven2-plugin.version}</version>
|
||||
<configuration>
|
||||
<wait>true</wait>
|
||||
<container>
|
||||
<containerId>jetty8x</containerId>
|
||||
<type>embedded</type>
|
||||
<systemProperties>
|
||||
</systemProperties>
|
||||
</container>
|
||||
<configuration>
|
||||
<properties>
|
||||
<cargo.servlet.port>8082</cargo.servlet.port>
|
||||
</properties>
|
||||
</configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java-version}</source>
|
||||
<target>${java-version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
</excludes>
|
||||
<systemPropertyVariables>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven2-plugin</artifactId>
|
||||
<version>${cargo-maven2-plugin.version}</version>
|
||||
<configuration>
|
||||
<wait>true</wait>
|
||||
<container>
|
||||
<containerId>jetty8x</containerId>
|
||||
<type>embedded</type>
|
||||
<systemProperties>
|
||||
</systemProperties>
|
||||
</container>
|
||||
<configuration>
|
||||
<properties>
|
||||
<cargo.servlet.port>8082</cargo.servlet.port>
|
||||
</properties>
|
||||
</configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
@ -9,28 +9,28 @@ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatche
|
||||
*/
|
||||
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
public WebApp() {
|
||||
super();
|
||||
}
|
||||
public WebApp() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class<?>[] { WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class };
|
||||
}
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class<?>[] { WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customizeRegistration(final Dynamic registration) {
|
||||
super.customizeRegistration(registration);
|
||||
}
|
||||
@Override
|
||||
protected void customizeRegistration(final Dynamic registration) {
|
||||
super.customizeRegistration(registration);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,51 +24,51 @@ import com.baeldung.thymeleaf.formatter.NameFormatter;
|
||||
*/
|
||||
public class WebMVCConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Bean
|
||||
@Description("Thymeleaf Template Resolver")
|
||||
public ServletContextTemplateResolver templateResolver() {
|
||||
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
||||
templateResolver.setPrefix("/WEB-INF/views/");
|
||||
templateResolver.setSuffix(".html");
|
||||
templateResolver.setTemplateMode("HTML5");
|
||||
@Bean
|
||||
@Description("Thymeleaf Template Resolver")
|
||||
public ServletContextTemplateResolver templateResolver() {
|
||||
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
||||
templateResolver.setPrefix("/WEB-INF/views/");
|
||||
templateResolver.setSuffix(".html");
|
||||
templateResolver.setTemplateMode("HTML5");
|
||||
|
||||
return templateResolver;
|
||||
}
|
||||
return templateResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Thymeleaf Template Engine")
|
||||
public SpringTemplateEngine templateEngine() {
|
||||
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||
templateEngine.setTemplateResolver(templateResolver());
|
||||
@Bean
|
||||
@Description("Thymeleaf Template Engine")
|
||||
public SpringTemplateEngine templateEngine() {
|
||||
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||
templateEngine.setTemplateResolver(templateResolver());
|
||||
|
||||
return templateEngine;
|
||||
}
|
||||
return templateEngine;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Thymeleaf View Resolver")
|
||||
public ThymeleafViewResolver viewResolver() {
|
||||
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||
viewResolver.setTemplateEngine(templateEngine());
|
||||
viewResolver.setOrder(1);
|
||||
return viewResolver;
|
||||
}
|
||||
@Bean
|
||||
@Description("Thymeleaf View Resolver")
|
||||
public ThymeleafViewResolver viewResolver() {
|
||||
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||
viewResolver.setTemplateEngine(templateEngine());
|
||||
viewResolver.setOrder(1);
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Spring Message Resolver")
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("messages");
|
||||
return messageSource;
|
||||
}
|
||||
@Bean
|
||||
@Description("Spring Message Resolver")
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("messages");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
|
||||
}
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Description("Custom Conversion Service")
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addFormatter(new NameFormatter());
|
||||
}
|
||||
@Override
|
||||
@Description("Custom Conversion Service")
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addFormatter(new NameFormatter());
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
|
||||
@EnableWebSecurity
|
||||
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
||||
public class WebMVCSecurity extends WebSecurityConfigurerAdapter {
|
||||
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public AuthenticationManager authenticationManagerBean() throws Exception {
|
||||
@ -37,13 +37,7 @@ public class WebMVCSecurity extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(final HttpSecurity http) throws Exception {
|
||||
http
|
||||
.authorizeRequests()
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.httpBasic()
|
||||
;
|
||||
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,50 +20,50 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
@Controller
|
||||
public class StudentController {
|
||||
|
||||
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
|
||||
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
|
||||
if (!errors.hasErrors()) {
|
||||
// get mock objects
|
||||
List<Student> students = buildStudents();
|
||||
// add current student
|
||||
students.add(student);
|
||||
model.addAttribute("students", students);
|
||||
}
|
||||
return ((errors.hasErrors()) ? "addStudent" : "listStudents");
|
||||
}
|
||||
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
|
||||
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
|
||||
if (!errors.hasErrors()) {
|
||||
// get mock objects
|
||||
List<Student> students = buildStudents();
|
||||
// add current student
|
||||
students.add(student);
|
||||
model.addAttribute("students", students);
|
||||
}
|
||||
return ((errors.hasErrors()) ? "addStudent" : "listStudents");
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
|
||||
public String addStudent(Model model) {
|
||||
model.addAttribute("student", new Student());
|
||||
return "addStudent";
|
||||
}
|
||||
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
|
||||
public String addStudent(Model model) {
|
||||
model.addAttribute("student", new Student());
|
||||
return "addStudent";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
|
||||
public String listStudent(Model model) {
|
||||
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
|
||||
public String listStudent(Model model) {
|
||||
|
||||
model.addAttribute("students", buildStudents());
|
||||
model.addAttribute("students", buildStudents());
|
||||
|
||||
return "listStudents";
|
||||
}
|
||||
return "listStudents";
|
||||
}
|
||||
|
||||
private List<Student> buildStudents() {
|
||||
List<Student> students = new ArrayList<Student>();
|
||||
private List<Student> buildStudents() {
|
||||
List<Student> students = new ArrayList<Student>();
|
||||
|
||||
Student student1 = new Student();
|
||||
student1.setId(1001);
|
||||
student1.setName("John Smith");
|
||||
student1.setGender('M');
|
||||
student1.setPercentage(Float.valueOf("80.45"));
|
||||
Student student1 = new Student();
|
||||
student1.setId(1001);
|
||||
student1.setName("John Smith");
|
||||
student1.setGender('M');
|
||||
student1.setPercentage(Float.valueOf("80.45"));
|
||||
|
||||
students.add(student1);
|
||||
students.add(student1);
|
||||
|
||||
Student student2 = new Student();
|
||||
student2.setId(1002);
|
||||
student2.setName("Jane Williams");
|
||||
student2.setGender('F');
|
||||
student2.setPercentage(Float.valueOf("60.25"));
|
||||
Student student2 = new Student();
|
||||
student2.setId(1002);
|
||||
student2.setName("Jane Williams");
|
||||
student2.setGender('F');
|
||||
student2.setPercentage(Float.valueOf("60.25"));
|
||||
|
||||
students.add(student2);
|
||||
return students;
|
||||
}
|
||||
students.add(student2);
|
||||
return students;
|
||||
}
|
||||
}
|
||||
|
@ -12,49 +12,49 @@ import javax.validation.constraints.NotNull;
|
||||
*/
|
||||
public class Student implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -8582553475226281591L;
|
||||
private static final long serialVersionUID = -8582553475226281591L;
|
||||
|
||||
@NotNull(message = "Student ID is required.")
|
||||
@Min(value = 1000, message = "Student ID must be at least 4 digits.")
|
||||
private Integer id;
|
||||
@NotNull(message = "Student ID is required.")
|
||||
@Min(value = 1000, message = "Student ID must be at least 4 digits.")
|
||||
private Integer id;
|
||||
|
||||
@NotNull(message = "Student name is required.")
|
||||
private String name;
|
||||
@NotNull(message = "Student name is required.")
|
||||
private String name;
|
||||
|
||||
@NotNull(message = "Student gender is required.")
|
||||
private Character gender;
|
||||
@NotNull(message = "Student gender is required.")
|
||||
private Character gender;
|
||||
|
||||
private Float percentage;
|
||||
private Float percentage;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Character getGender() {
|
||||
return gender;
|
||||
}
|
||||
public Character getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Character gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
public void setGender(Character gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public Float getPercentage() {
|
||||
return percentage;
|
||||
}
|
||||
public Float getPercentage() {
|
||||
return percentage;
|
||||
}
|
||||
|
||||
public void setPercentage(Float percentage) {
|
||||
this.percentage = percentage;
|
||||
}
|
||||
public void setPercentage(Float percentage) {
|
||||
this.percentage = percentage;
|
||||
}
|
||||
}
|
||||
|
@ -30,17 +30,17 @@ import com.baeldung.thymeleaf.config.WebMVCSecurity;
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class })
|
||||
public class CsrfEnabledIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
|
||||
@Autowired
|
||||
WebApplicationContext wac;
|
||||
@Autowired
|
||||
MockHttpSession session;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
|
||||
@Autowired
|
||||
private Filter springSecurityFilterChain;
|
||||
|
||||
|
||||
protected RequestPostProcessor testUser() {
|
||||
return user("user1").password("user1Pass").roles("USER");
|
||||
}
|
||||
@ -52,12 +52,12 @@ public class CsrfEnabledIntegrationTest {
|
||||
|
||||
@Test
|
||||
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
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user