-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add web beetl & context listener module
- Loading branch information
Showing
17 changed files
with
356 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>spring-boot</artifactId> | ||
<groupId>com.git.hui.boot</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>012-context-listener</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
21 changes: 21 additions & 0 deletions
21
spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/Application.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.git.hui.boot.listener; | ||
|
||
import com.git.hui.boot.listener.demo.DemoBean; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* Created by @author yihui in 08:47 19/8/21. | ||
*/ | ||
@SpringBootApplication | ||
public class Application { | ||
|
||
public Application(DemoBean demoBean) { | ||
System.out.println("init: " + demoBean.getNum()); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/AutoConfig.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,30 @@ | ||
package com.git.hui.boot.listener; | ||
|
||
import com.git.hui.boot.listener.context.MyContextAppHolder; | ||
import com.git.hui.boot.listener.context.MyContextListener; | ||
import com.git.hui.boot.listener.demo.DemoBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Created by @author yihui in 08:55 19/8/21. | ||
*/ | ||
@Configuration | ||
public class AutoConfig { | ||
|
||
@Bean | ||
public MyContextAppHolder myContextAppHolder() { | ||
return new MyContextAppHolder(); | ||
} | ||
|
||
@Bean | ||
public DemoBean demoBean(MyContextAppHolder myContextAppHolder) throws Exception { | ||
return myContextAppHolder.getObject(); | ||
} | ||
|
||
@Bean | ||
public MyContextListener myContextListener() { | ||
return new MyContextListener(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextAppHolder.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,34 @@ | ||
package com.git.hui.boot.listener.context; | ||
|
||
import com.git.hui.boot.listener.demo.DemoBean; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Created by @author yihui in 08:49 19/8/21. | ||
*/ | ||
public class MyContextAppHolder implements FactoryBean<DemoBean>, ApplicationContextAware { | ||
private ApplicationContext applicationContext; | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
@Override | ||
public DemoBean getObject() throws Exception { | ||
System.out.println("init bean!" + UUID.randomUUID().toString()); | ||
MyContextListener listener = (MyContextListener) applicationContext.getBean("myContextListener"); | ||
return new DemoBean(listener.getNum()); | ||
} | ||
|
||
@Override | ||
public Class<?> getObjectType() { | ||
return DemoBean.class; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...2-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextListener.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,25 @@ | ||
package com.git.hui.boot.listener.context; | ||
|
||
import lombok.Getter; | ||
|
||
import javax.servlet.ServletContextEvent; | ||
import javax.servlet.ServletContextListener; | ||
|
||
/** | ||
* Created by @author yihui in 08:48 19/8/21. | ||
*/ | ||
public class MyContextListener implements ServletContextListener { | ||
@Getter | ||
private volatile int num = 0; | ||
|
||
@Override | ||
public void contextInitialized(ServletContextEvent servletContextEvent) { | ||
System.out.println("-------> context loader!"); | ||
num += 1; | ||
} | ||
|
||
@Override | ||
public void contextDestroyed(ServletContextEvent servletContextEvent) { | ||
System.out.println("===== context destroyed! ======"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/DemoBean.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.git.hui.boot.listener.demo; | ||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* Created by @author yihui in 08:52 19/8/21. | ||
*/ | ||
public class DemoBean { | ||
@Getter | ||
private int num; | ||
|
||
public DemoBean(int num) { | ||
this.num = num; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...ot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/IndexController.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,20 @@ | ||
package com.git.hui.boot.listener.demo; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* Created by @author yihui in 08:59 19/8/21. | ||
*/ | ||
@RestController | ||
public class IndexController { | ||
@Autowired | ||
private DemoBean demoBean; | ||
|
||
@GetMapping(path = {"/", "/index"}) | ||
public String show() { | ||
return "ans: " + demoBean.getNum(); | ||
} | ||
|
||
} |
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,10 @@ | ||
## web-beetl | ||
### 项目说明 | ||
|
||
本项目主要基于springboot + beetl,用于演示基本的web项目构建 | ||
|
||
### 博文说明 | ||
|
||
本项目对应的博文内容为 | ||
|
||
- [190822-SpringBoot系列教程web篇之Beetl环境搭建/](http://spring.hhui.top/spring-blog/2019/08/22/190822-SpringBoot%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8Bweb%E7%AF%87%E4%B9%8BBeetl%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/) |
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,26 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>spring-boot</artifactId> | ||
<groupId>com.git.hui.boot</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>206-web-beetl</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.ibeetl</groupId> | ||
<artifactId>beetl-framework-starter</artifactId> | ||
<version>1.2.12.RELEASE</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
16 changes: 16 additions & 0 deletions
16
spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/Application.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.git.hui.boot.beetl; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* Created by @author yihui in 18:50 19/8/22. | ||
*/ | ||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/rest/IndexController.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,44 @@ | ||
package com.git.hui.boot.beetl.rest; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Random; | ||
|
||
/** | ||
* Created by @author yihui in 14:44 19/8/16. | ||
*/ | ||
@Controller | ||
public class IndexController { | ||
|
||
@GetMapping(path = {"", "/", "/index"}) | ||
public ModelAndView index() { | ||
Map<String, Object> data = new HashMap<>(2); | ||
data.put("name", "YiHui Beetl"); | ||
data.put("now", LocalDateTime.now().toString()); | ||
return new ModelAndView("index.btl", data); | ||
} | ||
|
||
private static String[] contents = | ||
("绿蚁浮觞香泛泛,黄花共荐芳辰。\n清霜天宇净无尘。\n登高宜有赋,拈笔戏成文。\n可奈园林摇落尽,悲秋意与谁论。\n眼中相识几番新。\n龙山高会处,落帽定何人。").split("\n"); | ||
private static Random random = new Random(); | ||
|
||
@GetMapping(path = "show1") | ||
public String showOne(Model model) { | ||
model.addAttribute("title", "临江仙"); | ||
model.addAttribute("content", contents[random.nextInt(6)]); | ||
return "show1.btl"; | ||
} | ||
|
||
@GetMapping(path = "show2") | ||
public String showTow(Map<String, Object> data) { | ||
data.put("name", "Show2---->"); | ||
data.put("now", LocalDateTime.now().toString()); | ||
return "show2.btl"; | ||
} | ||
} |
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,8 @@ | ||
server: | ||
port: 8080 | ||
|
||
beetl: | ||
enabled: true | ||
suffix: btl | ||
beetl-beetlsql: | ||
dev: true # 即自动检查模板变化 |
16 changes: 16 additions & 0 deletions
16
spring-boot/206-web-beetl/src/main/resources/static/index.css
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 @@ | ||
.title { | ||
color: #c00; | ||
font-weight: normal; | ||
font-size: 2em; | ||
} | ||
|
||
.content { | ||
color: darkblue; | ||
font-size: 1.2em; | ||
} | ||
|
||
.sign { | ||
color: lightgray; | ||
font-size: 0.8em; | ||
font-style: italic; | ||
} |
25 changes: 25 additions & 0 deletions
25
spring-boot/206-web-beetl/src/main/resources/templates/index.btl
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,25 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="description" content="SpringBoot Beetl"/> | ||
<meta name="author" content="YiHui"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
<title>YiHui's SpringBoot Beetl Demo</title> | ||
<link rel="stylesheet" href="index.css"/> | ||
</head> | ||
<body> | ||
|
||
<div> | ||
<div class="title">hello world!</div> | ||
<br/> | ||
<div class="content">欢迎访问 ${name}</div> | ||
<br/> | ||
<div class="sign">当前时间 ${now}</div> | ||
<br/> | ||
<a href="show1">传参2测试</a> | ||
<a href="show2">传参3测试</a> | ||
</div> | ||
</body> | ||
</html> |
19 changes: 19 additions & 0 deletions
19
spring-boot/206-web-beetl/src/main/resources/templates/show1.btl
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,19 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="description" content="SpringBoot Beetl"/> | ||
<meta name="author" content="YiHui"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
<title>YiHui's SpringBoot Beetl Demo</title> | ||
<link rel="stylesheet" href="index.css"/> | ||
</head> | ||
<body> | ||
|
||
<div> | ||
<div class="title">${title}</div> | ||
<div class="content">${content}</div> | ||
</div> | ||
</body> | ||
</html> |
19 changes: 19 additions & 0 deletions
19
spring-boot/206-web-beetl/src/main/resources/templates/show2.btl
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,19 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="description" content="SpringBoot Beetl"/> | ||
<meta name="author" content="YiHui"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
<title>YiHui's SpringBoot Beetl Demo</title> | ||
<link rel="stylesheet" href="index.css"/> | ||
</head> | ||
<body> | ||
|
||
<div> | ||
<div class="title">${name}</div> | ||
<div class="content">${now}</div> | ||
</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