Skip to content

Commit

Permalink
add web beetl & context listener module
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Aug 22, 2019
1 parent 9e24676 commit 61bd9d7
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 0 deletions.
25 changes: 25 additions & 0 deletions spring-boot/012-context-listener/pom.xml
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>
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);
}

}
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();
}

}
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;
}
}
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! ======");
}
}
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;
}

}
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();
}

}
10 changes: 10 additions & 0 deletions spring-boot/206-web-beetl/README.md
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/)
26 changes: 26 additions & 0 deletions spring-boot/206-web-beetl/pom.xml
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>
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);
}

}
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";
}
}
8 changes: 8 additions & 0 deletions spring-boot/206-web-beetl/src/main/resources/application.yml
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 spring-boot/206-web-beetl/src/main/resources/static/index.css
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 spring-boot/206-web-beetl/src/main/resources/templates/index.btl
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> &nbsp;&nbsp;&nbsp;&nbsp;
<a href="show2">传参3测试</a>
</div>
</body>
</html>
19 changes: 19 additions & 0 deletions spring-boot/206-web-beetl/src/main/resources/templates/show1.btl
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 spring-boot/206-web-beetl/src/main/resources/templates/show2.btl
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>
2 changes: 2 additions & 0 deletions spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<module>131-influxdb-java</module>
<module>204-web-freemaker</module>
<module>205-web-thymeleaf</module>
<module>012-context-listener</module>
<module>206-web-beetl</module>
</modules>

<artifactId>spring-boot</artifactId>
Expand Down

0 comments on commit 61bd9d7

Please sign in to comment.