1.引入依赖
<!--beetl-->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.9.3</version>
</dependency>
2.创建BeetlConfig配置类
package com.etone.etonenetcard.config;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* web 配置类
*
* @author fengshuonan
* @date 2016年11月12日 下午5:03:32
*/
@Configuration
public class BeetlConfig {
@Autowired
BeetlProperties beetlProperties;
/**
* beetl的配置
*/
@Bean(initMethod = "init")
public BeetlConfiguration beetlConfiguration() {
BeetlConfiguration beetlConfiguration = new BeetlConfiguration();
beetlConfiguration.setResourceLoader(new ClasspathResourceLoader(BeetlConfig.class.getClassLoader(), beetlProperties.getPrefix()));
beetlConfiguration.setConfigProperties(beetlProperties.getProperties());
return beetlConfiguration;
}
/**
* beetl的视图解析器
*/
@Bean
public BeetlSpringViewResolver beetlViewResolver() {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setConfig(beetlConfiguration());
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
return beetlSpringViewResolver;
}
}
2.BeetlProperties属性映射类
package com.etone.etonenetcard.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import java.util.Properties;
/**
* beetl配置(如果需要配置别的配置可参照这个形式自己添加)
*
* @author fengshuonan
* @date 2017-05-24 20:37
*/
@Configuration
@ConfigurationProperties(prefix = BeetlProperties.BEETLCONF_PREFIX)
public class BeetlProperties {
public static final String BEETLCONF_PREFIX = "beetl";
private String delimiterStatementStart;
private String delimiterStatementEnd;
private String resourceTagroot;
private String resourceTagsuffix;
private String resourceAutoCheck;
@Value("${spring.mvc.view.prefix}")
private String prefix;
public Properties getProperties() {
Properties properties = new Properties();
if (!StringUtils.isEmpty(delimiterStatementStart)) {
if (delimiterStatementStart.startsWith("\\")) {
delimiterStatementStart = delimiterStatementStart.substring(1);
}
properties.setProperty("DELIMITER_STATEMENT_START", delimiterStatementStart);
}
if (!StringUtils.isEmpty(delimiterStatementEnd)) {
properties.setProperty("DELIMITER_STATEMENT_END", delimiterStatementEnd);
} else {
properties.setProperty("DELIMITER_STATEMENT_END", "null");
}
if (!StringUtils.isEmpty(resourceTagroot)) {
properties.setProperty("RESOURCE.tagRoot", resourceTagroot);
}
if (!StringUtils.isEmpty(resourceTagsuffix)) {
properties.setProperty("RESOURCE.tagSuffix", resourceTagsuffix);
}
if (!StringUtils.isEmpty(resourceAutoCheck)) {
properties.setProperty("RESOURCE.autoCheck", resourceAutoCheck);
}
return properties;
}
public String getPrefix() {
return prefix;
}
public String getDelimiterStatementStart() {
return delimiterStatementStart;
}
public void setDelimiterStatementStart(String delimiterStatementStart) {
this.delimiterStatementStart = delimiterStatementStart;
}
public String getDelimiterStatementEnd() {
return delimiterStatementEnd;
}
public void setDelimiterStatementEnd(String delimiterStatementEnd) {
this.delimiterStatementEnd = delimiterStatementEnd;
}
public String getResourceTagroot() {
return resourceTagroot;
}
public void setResourceTagroot(String resourceTagroot) {
this.resourceTagroot = resourceTagroot;
}
public String getResourceTagsuffix() {
return resourceTagsuffix;
}
public void setResourceTagsuffix(String resourceTagsuffix) {
this.resourceTagsuffix = resourceTagsuffix;
}
public String getResourceAutoCheck() {
return resourceAutoCheck;
}
public void setResourceAutoCheck(String resourceAutoCheck) {
this.resourceAutoCheck = resourceAutoCheck;
}
}
BeetlConfiguration
package com.stylefeng.guns.core.beetl;
import com.stylefeng.guns.core.util.KaptchaUtil;
import com.stylefeng.guns.core.util.ToolUtil;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
/**
* beetl拓展配置,绑定一些工具类,方便在模板中直接调用
*
* @author stylefeng
* @Date 2018/2/22 21:03
*/
public class BeetlConfiguration extends BeetlGroupUtilConfiguration {
@Override
public void initOther() {
groupTemplate.registerFunctionPackage("shiro", new ShiroExt());
groupTemplate.registerFunctionPackage("tool", new ToolUtil());
groupTemplate.registerFunctionPackage("kaptcha", new KaptchaUtil());
}
}
3.yml
server:
servlet:
context-path: /netcard
##########################################################
################## 所有profile共有的配置 #################
##########################################################
################### beetl配置 ###################
beetl:
delimiter-statement-start: \@ #开始结束标签(yaml不允许@开头)
delimiter-statement-end: null
resource-tagroot: common/tags #自定义标签文件Root目录和后缀
resource-tagsuffix: tag
resource-auto-check: true #是否检测文件变化,开发用true合适,但线上要改为false
################### spring配置 ###################
spring:
profiles:
active: @profileActive@
mvc:
static-path-pattern: /static/**
view:
prefix: /WEB-INF/view
http:
converters:
preferred-json-mapper: fastjson
devtools:
restart:
enabled: false #是否开启开发者工具(true/false)
additional-paths: src/main/java
exclude: static/**,WEB-INF/view/**
aop:
proxy-target-class: true #false为启用jdk默认动态代理,true为cglib动态代理
################## mybatis配置 ###################
mybatis:
mapper-locations: classpath:mapping/**/*.xml
type-aliases-package: com.etone.etonenetcard.controller
4.创建webapp文件夹,并设置为根目录
5.controller
package com.etone.etonenetcard.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class CollectionInformation {
/**
* 跳转到主页
*/
@RequestMapping(value = "/",method = RequestMethod.GET)
public String index(){
return "/informationCollection.html";
}
/**
* 跳转到信息收集页面
*/
@RequestMapping(value="/information",method = RequestMethod.GET)
public String collectionInformation(){
return "/informationCollection.html";
}
}
7.HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>员工信息收集</title>
<link rel="stylesheet" href="${ctxPath}/static/common/bootstrap/css/bootstrap.css">
<link rel="stylesheet" href="${ctxPath}/static/common/bootstrapValidator/css/bootstrapValidator.css">
<script src="${ctxPath}/static/common/jquery/jquery-1.10.2.min.js"></script>
<script src="${ctxPath}/static/common/bootstrap/js/bootstrap.min.js"></script>
<script src="${ctxPath}/static/common/bootstrapValidator/js/bootstrapValidator.min.js"></script>
</head>
<body>
<!--.container 类用于固定宽度并支持响应式布局的容器。-->
<div class="container-fluid">
<form action="/netcard/qrcode/create" method="get" id="employeeInformation">
<div class="form-group">
<label>姓名</label>
<input type="text" class="form-control" name="name" id="name" placeholder="请输入姓名">
</div>
<div class="form-group">
<label>工号</label>
<input type="text" class="form-control" name="job_number" id="job_number" placeholder="请输入工号">
</div>
<div class="form-group">
<label>手机号</label>
<input type="text" class="form-control" name="phone" id="phone" placeholder="请输入手机号">
</div>
<div class="row">
<div class="col-xs-4">
<div class="form-group">
<label>验证码</label>
<input type="text" class="form-control" name="code" id="code" placeholder="请输入验证码">
</div>
</div>
<span>
<button type="button" class="btn btn-warning">获取验证码</button>
</span>
</div>
<button type="submit" class="btn btn-default">生成二维码</button>
</form>
</div>
</body>
</html>
8.引入静态资源的方式
2.搭建之后就可以用model向html页面传值,然后用${item}取值了
//用户是唯一的,所以根据openid可以区分出不同的用户,和推广员绑定
model.addAttribute("openid",openid);
return "/handleOnlineCard.html";