这两周需要写一个页面来请求另一个服务中的接口,服务器采用了超轻量级的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项目的更多相关文章

  1. 三、thymeleaf模板引擎构建前台html, 后台使用 ModelAndView 和 Model 模型

    项目源码:https://github.com/y369q369/springBoot.git      ->     thymeleaf 私聊QQ: 1486866853 1.pom.xml中 ...

  2. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  3. Thymeleaf模板引擎的初步使用

    在springboot中,推荐使用的模板引擎是Thymeleaf模板引擎,它提供了完美的Spring MVC的支持.下面就简单的介绍一下Thymeleaf模板引擎的使用. 在controller层中, ...

  4. spring boot: thymeleaf模板引擎使用

    spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...

  5. SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

    在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...

  6. (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  7. SpringBoot使用thymeleaf模板引擎

    (1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  8. Spring Boot 2.0 整合Thymeleaf 模板引擎

    本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...

  9. SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染

    在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...

随机推荐

  1. php序列化问题

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...

  2. input value="值栈的值"

    <input type="text" value="<s:property value="myp.begintime"/>" ...

  3. linshi12

    #include<iostream> using namespace std; int main(){ int a[50]; a[1]=5; int i; for(i=2;;i++){ a ...

  4. HDU 5170 GTY's math problem 水题

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5170 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  5. 设计 Azure SQL 数据库,并使用 C# 和 ADO.NET 进行连接

    标题:设计 Azure SQL 数据库,并使用 C# 和 ADO.NET 进行连接 里面有使用C#使用SqlServer的例子.

  6. 继承&构造函数

    子父类中的构造函数的特点. 在子类构造对象时,发现,访问子类构造函数时,父类也运行了. 为什么呢? 原因是:在子类的构造函数中第一行有一个默认的隐式语句. super(); 子类的实例化过程:子类中所 ...

  7. React.js + LiveReload配置详解

    一.介绍一下LiveReload: LiveReload monitors changes in the file system. As soon as you save a file, it is ...

  8. ViewPager、Fragment、Matrix综合使用实现Tab滑页效果

    原文地址:http://www.cnblogs.com/kross/p/3372987.html 我们实现一个上面是一个可以左右滑动的页面,下面是三个可点击切换的tab按钮,tab按钮上还有一个激活条 ...

  9. Thread的run()与start()的区别

    java的线程是通过java.lang.Thread类来实现的.VM启动时会有一个由主方法所定义的线程.可以通过创建Thread的实例来创建新的线程.每个线程都是通过某个特定Thread对象所对应的方 ...

  10. asp.net 中使用 pagedlist 分页并具有查询功能的实现方法

    用pagedlist在项目中做分页已N次了,今天再次用实例来实现一个带查询功能的分页例子. 1.在view代码: @using PagedList.Mvc@model BGZS.Models.User ...