UndertowServer+SpringMVC+Thymeleaf模板引擎构建轻量级的web项目
这两周需要写一个页面来请求另一个服务中的接口,服务器采用了超轻量级的undertow,模板引擎采用的是Thymeleaf,在寻找页面资源位置这个地方难住了我。下面分享一下,这方面的代码。
SpringWebConfig方面:
- public class SpringWebConfig extends WebMvcConfigurerAdapter {
private static boolean initialized = false;- @Bean
public static PropertySourcesPlaceholderConfigurer config() {
return SpringConfigHelper.createPlaceholderConfigBean("classpath:prime.cfg");
}- /**
* Spring容器初始化完成后回调该函数
* 通过该机制可以动态扩展bean实例配置
* 用于工作服务框架加载工作服务实现相关bean
*
* @param basePackage 扩展bean扫描路径
* @return ApplicationListener实例
*/
@Bean
public ApplicationListener<ContextRefreshedEvent> initializedEvent(
@Value("${Optimus.Prime.ServiceBasePackage:}") String basePackage) {
return new ApplicationListener<ContextRefreshedEvent>() {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (!initialized) {
initialized = true;
SpringConfigHelper.appendBeanFromPackage(
(AnnotationConfigWebApplicationContext) event.getApplicationContext(),
basePackage);
}
}
};
}- /**
* 模板解析器
* @return 解析器
*/
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setPrefix("classpath:/template/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCacheable(false);
resolver.setCharacterEncoding("UTF-8");
return resolver;
}- /**
* 解析器
* @param templateResolver 解析器
* @return
*/
@Bean
public SpringTemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver);- return engine;
}
/**
* 视图解析器
* @param templateEngine 解析器
* @return 视图解析
*/
@Bean
public ThymeleafViewResolver viewResolver(SpringTemplateEngine templateEngine) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine);
resolver.setCharacterEncoding("UTF-8");- return resolver;
}
}- UndertowServer 的启动方法:
- public int start() {
DeploymentInfo servletBuilder = Servlets.deployment()
.setClassLoader(UndertowServer.class.getClassLoader())
.setContextPath("/TAE/rest/v1")
.setDeploymentName("test.war")
.addServletContextAttribute(
SpringContextListener.PARENT_CONTEXT, context)
.addListeners(
Servlets.listener(SpringContextListener.class))
.addServlets(
Servlets.servlet("resource", DispatcherServlet.class)
.addInitParam("contextClass",
"org.springframework.web.context.support"
+ ".AnnotationConfigWebApplicationContext")
.addInitParam("contextConfigLocation",
configClass)
.addMapping("/*")
.setLoadOnStartup(1)
)
.addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME,
new WebSocketDeploymentInfo()
.setBuffers(new DefaultByteBufferPool(true, 100))
);- DeploymentManager manager = Servlets.defaultContainer()
.addDeployment(servletBuilder);
manager.deploy();- try {
PathHandler path = Handlers.path(Handlers.redirect("/"))
.addPrefixPath("/", manager.start());- server = Undertow.builder()
.addHttpListener(port, host)
.setHandler(path)
.build();- server.start();
} catch (Exception e) {
log.fatal("cannot start web server", e);
return CommonError.FATAL_FAIL;
}
return CommonError.SUCCESS;
}
UndertowServer+SpringMVC+Thymeleaf模板引擎构建轻量级的web项目的更多相关文章
- 三、thymeleaf模板引擎构建前台html, 后台使用 ModelAndView 和 Model 模型
项目源码:https://github.com/y369q369/springBoot.git -> thymeleaf 私聊QQ: 1486866853 1.pom.xml中 ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- Thymeleaf模板引擎的初步使用
在springboot中,推荐使用的模板引擎是Thymeleaf模板引擎,它提供了完美的Spring MVC的支持.下面就简单的介绍一下Thymeleaf模板引擎的使用. 在controller层中, ...
- spring boot: thymeleaf模板引擎使用
spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...
- SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图
在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...
- (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用
一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...
- SpringBoot使用thymeleaf模板引擎
(1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- Spring Boot 2.0 整合Thymeleaf 模板引擎
本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...
- SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染
在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...
随机推荐
- 使用Node.js 搭建http服务器 http-server 模块
1. 安装 http-server 模块 npm install http-server -g 全局安装 2.在需要的文件夹 启动 http-server 默认的端口是8080 可以使 ...
- USACO 2.3.4 Money Systems 货币系统(完全背包)
Description 母牛们不但创建了他们自己的政府而且选择了建立了自己的货币系统. [In their own rebellious way],,他们对货币的数值感到好奇. 传统地,一个货币系统是 ...
- Beta周第8次Scrum会议(11/17)【王者荣耀交流协会】
一.小组信息 队名:王者荣耀交流协会 小组成员 队长:高远博 成员:王超,袁玥,任思佳,王磊,王玉玲,冉华 小组照片 今天拍照的人是王磊.因此他没有出现在照片中. 二.开会信息 时间:2017/11/ ...
- pandas中DataFrame的ix,loc,iloc索引方式的异同
pandas中DataFrame的ix,loc,iloc索引方式的异同 1.loc: 按照标签索引,范围包括start和end 2.iloc: 在位置上进行索引,不包括end 3.ix: 先在inde ...
- 软工实践第八次作业(课堂实战)- 项目UML设计(第五组)
本次作业博客 团队信息 队名:起床一起肝活队 原组长: 白晨曦(101) 原组员: 李麒 (123) 陈德斌(104) 何裕捷(214) 黄培鑫(217) 王焕仁(233) 林志华(128) 乐忠豪( ...
- XCode 6.4 Alcatraz 安装的插件不可用
升级Xcode 6.4后插件都不可用了,解决办法: 1.在 Alcatraz中删除插件并退出Xcode: 2.重新打开Xcode 并安装: 3.退出Xcode: 4.进入Xcode,会提示如图,点击 ...
- HDU 5661 Claris and XOR 贪心
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5661 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- TFTP服务 简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,默认端口号为69
(1)yum安装:tftp.tftp-server (2)启动tftp CentOS 6 service xinetd restart chkconfig tftp on CentOS 7 sys ...
- MySQL 查询缓存机制(MySQL数据库调优)
查询缓存机制:缓存的是查询语句的整个查询结果,是一个完整的select语句的缓存结果 哪些查询可能不会被缓存 :查询中包含UDF.存储函数.用户自定义变量.临时表.mysql库中系统表.或者包含列级别 ...
- bond下改变网卡
浪潮服务器打开控制台 用ip addr查看哪个网卡是绑定的,eth2和eth4是绑定状态 用mv命令,更改网卡名称 并将每个网卡里的信息更改 reboot,重启 ip addr查看,eth6和eth8 ...