forked from xkcoding/spring-boot-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
325 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# spring-boot-demo-template-freemarker | ||
|
||
依赖 [spring-boot-demo-parent](../spring-boot-demo-parent) 、`spring-boot-starter-freemarker` | ||
|
||
### pom.xml | ||
|
||
```xml | ||
<?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> | ||
|
||
<artifactId>spring-boot-demo-template-freemarker</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>war</packaging> | ||
|
||
<name>spring-boot-demo-template-freemarker</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>com.xkcoding</groupId> | ||
<artifactId>spring-boot-demo-parent</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-freemarker</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>spring-boot-demo-template-freemarker</finalName> | ||
</build> | ||
|
||
</project> | ||
``` | ||
|
||
### SpringBootDemoTemplateFreemarkerApplication.java | ||
|
||
```java | ||
@Controller | ||
@SpringBootApplication | ||
public class SpringBootDemoTemplateFreemarkerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringBootDemoTemplateFreemarkerApplication.class, args); | ||
} | ||
|
||
@GetMapping({"","/"}) | ||
public ModelAndView home(){ | ||
return new ModelAndView("index"); | ||
} | ||
} | ||
``` | ||
|
||
### UserController.java | ||
|
||
```java | ||
/** | ||
* 用户 Controller | ||
* | ||
* @package: com.xkcoding.springbootdemotemplatefreemarker.controller | ||
* @description: 用户 Controller | ||
* @author: yangkai.shen | ||
* @date: Created in 2017/11/26 下午6:47 | ||
* @copyright: Copyright (c) 2017 | ||
* @version: 0.0.1 | ||
* @modified: yangkai.shen | ||
*/ | ||
@Controller | ||
@RequestMapping("/user") | ||
@Slf4j | ||
public class UserController { | ||
|
||
@PostMapping("/login") | ||
public ModelAndView login(User user, HttpServletRequest request) { | ||
ModelAndView mv = new ModelAndView(); | ||
|
||
mv.addObject(user); | ||
mv.setViewName("home"); | ||
|
||
request.getSession().setAttribute("user", user); | ||
return mv; | ||
} | ||
|
||
@GetMapping("/login") | ||
public ModelAndView login(HttpServletRequest request) { | ||
ModelAndView mv = new ModelAndView(); | ||
|
||
User user = (User) request.getSession().getAttribute("user"); | ||
if (user == null) { | ||
mv.setViewName("redirect:/"); | ||
} else { | ||
mv.setViewName("home"); | ||
} | ||
|
||
return mv; | ||
} | ||
} | ||
``` | ||
|
||
### home.ftl | ||
|
||
```html | ||
<!doctype html> | ||
<html lang="en"> | ||
<#include "./common/header.ftl"> | ||
<body> | ||
<div id="app" style="margin: 20px 20%"> | ||
欢迎登录,${user.name}! | ||
</div> | ||
</body> | ||
</html> | ||
``` | ||
|
||
### index.ftl | ||
|
||
```html | ||
<!doctype html> | ||
<html lang="en"> | ||
<#include "./common/header.ftl"> | ||
<body> | ||
<div id="app" style="margin: 20px 20%"> | ||
<form action="/demo/user/login" method="post"> | ||
用户名<input type="text" name="name" placeholder="用户名"/> | ||
密码<input type="password" name="password" placeholder="密码"/> | ||
<input type="submit" value="登录"> | ||
</form> | ||
</div> | ||
|
||
</body> | ||
</html> | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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> | ||
|
||
<artifactId>spring-boot-demo-template-freemarker</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>war</packaging> | ||
|
||
<name>spring-boot-demo-template-freemarker</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>com.xkcoding</groupId> | ||
<artifactId>spring-boot-demo-parent</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-freemarker</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>spring-boot-demo-template-freemarker</finalName> | ||
</build> | ||
|
||
</project> |
21 changes: 21 additions & 0 deletions
21
...kcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.xkcoding.springbootdemotemplatefreemarker; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
@Controller | ||
@SpringBootApplication | ||
public class SpringBootDemoTemplateFreemarkerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringBootDemoTemplateFreemarkerApplication.class, args); | ||
} | ||
|
||
@GetMapping({"","/"}) | ||
public ModelAndView home(){ | ||
return new ModelAndView("index"); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...rc/main/java/com/xkcoding/springbootdemotemplatefreemarker/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.xkcoding.springbootdemotemplatefreemarker.controller; | ||
|
||
import com.xkcoding.springbootdemotemplatefreemarker.domain.User; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* 用户 Controller | ||
* | ||
* @package: com.xkcoding.springbootdemotemplatefreemarker.controller | ||
* @description: 用户 Controller | ||
* @author: yangkai.shen | ||
* @date: Created in 2017/11/26 下午6:47 | ||
* @copyright: Copyright (c) 2017 | ||
* @version: 0.0.1 | ||
* @modified: yangkai.shen | ||
*/ | ||
@Controller | ||
@RequestMapping("/user") | ||
@Slf4j | ||
public class UserController { | ||
|
||
@PostMapping("/login") | ||
public ModelAndView login(User user, HttpServletRequest request) { | ||
ModelAndView mv = new ModelAndView(); | ||
|
||
mv.addObject(user); | ||
mv.setViewName("home"); | ||
|
||
request.getSession().setAttribute("user", user); | ||
return mv; | ||
} | ||
|
||
@GetMapping("/login") | ||
public ModelAndView login(HttpServletRequest request) { | ||
ModelAndView mv = new ModelAndView(); | ||
|
||
User user = (User) request.getSession().getAttribute("user"); | ||
if (user == null) { | ||
mv.setViewName("redirect:/"); | ||
} else { | ||
mv.setViewName("home"); | ||
} | ||
|
||
return mv; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...e-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/domain/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.xkcoding.springbootdemotemplatefreemarker.domain; | ||
|
||
import lombok.Data; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* 用户实体类 | ||
* | ||
* @package: com.xkcoding.springbootdemotemplatefreemarker | ||
* @description: 用户实体类 | ||
* @author: yangkai.shen | ||
* @date: Created in 2017/11/26 下午6:50 | ||
* @copyright: Copyright (c) 2017 | ||
* @version: 0.0.1 | ||
* @modified: yangkai.shen | ||
*/ | ||
@Data | ||
public class User implements Serializable { | ||
private String name; | ||
private String password; | ||
} |
3 changes: 3 additions & 0 deletions
3
spring-boot-demo-template-freemarker/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
server: | ||
port: 8080 | ||
context-path: /demo |
7 changes: 7 additions & 0 deletions
7
spring-boot-demo-template-freemarker/src/main/resources/templates/common/header.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" | ||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>spring-boot-demo-template-thymeleaf</title> | ||
</head> |
9 changes: 9 additions & 0 deletions
9
spring-boot-demo-template-freemarker/src/main/resources/templates/home.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<#include "./common/header.ftl"> | ||
<body> | ||
<div id="app" style="margin: 20px 20%"> | ||
欢迎登录,${user.name}! | ||
</div> | ||
</body> | ||
</html> |
14 changes: 14 additions & 0 deletions
14
spring-boot-demo-template-freemarker/src/main/resources/templates/index.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<#include "./common/header.ftl"> | ||
<body> | ||
<div id="app" style="margin: 20px 20%"> | ||
<form action="/demo/user/login" method="post"> | ||
用户名<input type="text" name="name" placeholder="用户名"/> | ||
密码<input type="password" name="password" placeholder="密码"/> | ||
<input type="submit" value="登录"> | ||
</form> | ||
</div> | ||
|
||
</body> | ||
</html> |
16 changes: 16 additions & 0 deletions
16
...ng/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.xkcoding.springbootdemotemplatefreemarker; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class SpringBootDemoTemplateFreemarkerApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
} | ||
|
||
} |