BAEL-1221: How Spring MVC Really Works (#2757)
* BAEL-1221: How Spring MVC Really Works * BAEL-1221: Moved the project to the guest directory
This commit is contained in:
parent
f92aa19c37
commit
3d7a395b37
59
guest/spring-mvc/pom.xml
Normal file
59
guest/spring-mvc/pom.xml
Normal file
@ -0,0 +1,59 @@
|
||||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.forketyfork.guest</groupId>
|
||||
<artifactId>spring-mvc</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-mvc</name>
|
||||
<description>Spring MVC sample project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.M5</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,15 @@
|
||||
package com.forketyfork.guest.springmvc;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = {"com.forketyfork.guest.springmvc"})
|
||||
public class Spring5Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Spring5Application.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.forketyfork.guest.springmvc.model;
|
||||
|
||||
public class LoginData {
|
||||
|
||||
private String login;
|
||||
|
||||
private String password;
|
||||
|
||||
public LoginData() {
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.forketyfork.guest.springmvc.web;
|
||||
|
||||
import com.forketyfork.guest.springmvc.model.LoginData;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Controller
|
||||
public class InternalsController {
|
||||
|
||||
private static final String LOGIN = "jack";
|
||||
private static final String PASSWORD = "halloween";
|
||||
|
||||
@GetMapping("/")
|
||||
public String hello() {
|
||||
return "login";
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ModelAndView login(LoginData loginData) {
|
||||
if (LOGIN.equals(loginData.getLogin()) && PASSWORD.equals(loginData.getPassword())) {
|
||||
return new ModelAndView("success", Collections.singletonMap("login", loginData.getLogin()));
|
||||
} else {
|
||||
return new ModelAndView("failure", Collections.singletonMap("login", loginData.getLogin()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
10
guest/spring-mvc/src/main/resources/templates/failure.html
Normal file
10
guest/spring-mvc/src/main/resources/templates/failure.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Login Failure</title>
|
||||
</head>
|
||||
<p>
|
||||
User with login <span th:text="${login}"></span> not found, please <a href="/">try again</a>.
|
||||
</p>
|
||||
</html>
|
13
guest/spring-mvc/src/main/resources/templates/login.html
Normal file
13
guest/spring-mvc/src/main/resources/templates/login.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head><title>Login Form</title></head>
|
||||
<body>
|
||||
|
||||
<form method="post" action="/login">
|
||||
<input type="text" placeholder="Login" name="login"/>
|
||||
<input type="password" placeholder="Password" name="password"/>
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
10
guest/spring-mvc/src/main/resources/templates/success.html
Normal file
10
guest/spring-mvc/src/main/resources/templates/success.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Login Success</title>
|
||||
</head>
|
||||
<p>
|
||||
Hello, <span th:text="${login}"></span>!
|
||||
</p>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user