直接上项目地址请查看
1.为什么是beetl?
首先本项目为什么选型beetl这个渲染引擎呢?因为没有那么多为什么,纯粹是顺手,但是像其它老大哥模板像freemarker,thymeleaf将会一一推出教程,有人说beetl效率高得吓人,其实做项目更多时候除了效率还得看稳定度、普及度、易用性。当然这些都是题外话,废话少说,马上进入主题。
首先这次使用的是springboot2.1.5,springboot1.x和2.x差距甚大,也是前人踩坑,后人乘凉的一个框架,经历过1.x的坑,在2.x可以说迎来开发者爆发增长的时段,随着设计模式选型日渐成熟,一代又一代的迭代后,所以我选择了2.1.5,也不是说2.1.5之前版本变化很大,当然中庸选择也算是比较符合现在的趋势,你选2.1.0甚至选2.0.5也没多大关系。一句话只要稳定就好。
代码部分
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>${beetl.version}</version>
</dependency>
</dependencies>
这可以说都是最新的版本。指的是beetl。
package org.yick.i18n.config;
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
@EnableWebMvc
public class WebConfigure implements WebMvcConfigurer{
@Override
public void addInterceptors(InterceptorRegistry registry) {
//添加国际化拦截器
registry.addInterceptor(localeChangeInterceptor());
}
/**
* 国际化切换拦截器
*
* @return 国际化切换拦截器
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
//自定义拦截参数名称
interceptor.setParamName("lang");
return interceptor;
}
/**
* 国际化处理器
*
* @return 国际化处理器
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
//设置默认区域
slr.setDefaultLocale(Locale.CHINA);
return slr;
}
}
package org.yick.i18n.config;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.yick.i18n.beetl.BeetlGroupConfiguration;
@Configuration
public class BeetlConfigure {
@Value("${beetl.templatesPath}")
String templatesPath;// 模板根目录 ,比如 "templates"
@Value("${beetl.configPath}")
String configPath;
@Autowired
private ResourceLoader resourceLoader;
@Bean(initMethod = "init")
public BeetlGroupUtilConfiguration beetlConfiguration() {
BeetlGroupConfiguration beetlGroupUtilConfiguration = new BeetlGroupConfiguration();
// 获取SpringBoot 的ClassLoader
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = BeetlConfigure.class.getClassLoader();
}
Resource resource = resourceLoader.getResource(configPath);
beetlGroupUtilConfiguration.setConfigFileResource(resource);
ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader, templatesPath);
beetlGroupUtilConfiguration.setResourceLoader(cploder);
return beetlGroupUtilConfiguration;
}
@Bean
public BeetlSpringViewResolver beetlViewResolver() {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlConfiguration());
return beetlSpringViewResolver;
}
}
详细代码到csdn上看https://download.csdn.net/download/yixiaohui54321/11492303