spring4与Thymeleaf整合一个简单示例
来源:互联网 发布:淘宝吧 编辑:程序博客网 时间:2024/06/16 02:11
1、spring使用的版本是spring4.0.0:从spring4.0.0里解压出来的libs文件里的所有jar包。
2、thymeleaf版本是thymeleaf-3.0.9.RELEASE:从thymeleaf-3.0.9.RELEASE解压出来后,在lib文件夹里找到slf4j-api-1.7.25.jar、attoparser-2.0.4.RELEASE.jar包和从dist文件夹下找到thymeleaf-3.0.9.RELEASE.jar和thymeleaf-spring4-3.0.9.RELEASE.jar包。
3、其他包unbescape-1.0.jar,是一个转义库包。
1、这里使用idea建立一个web工程项目如下图:
下一步输入项目名称:SprAThy。完成。新建的工程如下:
2、建立一下工程存放类和jar包的文件夹最终结果如下:
3、添加jar包。把spring的jar包放到lib文件夹里,然后有机lib文件夹——add as library,就把lib添加到工程里了。Thymeleaf的几个jar包也一样添加进来,
unbescape-1.0.jar包也一起放到lib文件夹里。下面的一些包不属于spring和thymeleaf需要自己下载
最终如下右图:
4、建立类文件RootConfig、WebAppInitializer、WebConfig三个类。如下图
RootConfig.java文件如下:
package main.com.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.web.servlet.config.annotation.EnableWebMvc;/** * Created by IBM on 2017/12/18. */@Configuration@ComponentScan(basePackages={"main.com"}, excludeFilters={ //@Filter(type=FilterType.CUSTOM, value=RootConfig.WebPackage.class) @ComponentScan.Filter(type= FilterType.ANNOTATION, value=EnableWebMvc.class) })public class RootConfig {}
WebAppInitialzer.java文件如下:
package main.com.config;import main.com.web.WebConfig;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;/** * Created by IBM on 2017/12/18. */public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { RootConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { WebConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; }}
WebConfig.java文件如下:
package main.com.web;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.context.ContextLoader;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.thymeleaf.spring4.SpringTemplateEngine;import org.thymeleaf.spring4.view.ThymeleafViewResolver;import org.thymeleaf.templateresolver.ITemplateResolver;import org.thymeleaf.templateresolver.ServletContextTemplateResolver;/** * Created by IBM on 2017/12/11. * Adapter适配器 * ViewResolver视图解析器 * InternalResourceViewResolver网络视图解析器 */@Configuration@EnableWebMvc//启用SpringMVC@ComponentScan(basePackages = "main.com.web")//启动自动装配beanpublic class WebConfig extends WebMvcConfigurerAdapter { //配置静态资源的处理 @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean // 配置生成模板解析器 public ITemplateResolver templateResolver() { WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); // ServletContextTemplateResolver需要一个ServletContext作为构造参数,可通过<span style="font-family:Arial, Helvetica, sans-serif;">WebApplicationContext 的方法获得</span> ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver( webApplicationContext.getServletContext()); templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setSuffix(".html");// templateResolver.setCharacterEncoding("UTF-8"); // 设置模板模式,也可用字符串"HTML"代替,此处不建议使用HTML5,原因看下图源码// templateResolver.setTemplateMode(TemplateMode.HTML); templateResolver.setTemplateMode("HTML5"); return templateResolver; } @Bean // 生成模板引擎并为模板引擎注入模板解析器 public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver); return templateEngine; } @Bean // 生成视图解析器并未解析器注入模板引擎 public ViewResolver viewResolver(SpringTemplateEngine templateEngine) { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setContentType("text/html; charset=utf-8"); viewResolver.setTemplateEngine(templateEngine); return viewResolver; }}
5、编写控制器类HomeController.java,防治web文件夹下,与WebConfig.java文件在同一文件夹。
HomeController.java文件如下:
package main.com.web;import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import static org.springframework.web.bind.annotation.RequestMethod.GET;@Configuration@Controller//声明为一个控制器/** * Created by IBM on 2017/12/18. */public class HomeController { @RequestMapping(value = "/home",method = GET)//处理对“/”的GET请求。请求这个视图:http://localhost:8080/home public String home() { return "home"; }}
6、在WEB-INF文件夹下建立views文件夹,这个文件夹用于存放视图html文件,和WebConfig.java中templateResolver()函数中配置的路径一样。
新建home.html文件
home.html文件如下
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><!--声明thymeleaf命名空间--><link><meta charset="UTF-8"><title>Welcome to Home</title></head><body><h1>Welcome to Home</h1><h1>Welcome to Home</h1></body></html>
整个项目完成对目录如下:
7、配置tomcat,然后运行项目
如下图:
示例下载地址:
http://download.csdn.net/download/u014572215/10163214
- spring4与Thymeleaf整合一个简单示例
- Spring4-Thymeleaf-MyBatis整合Context文件
- springboot thymeleaf简单整合
- thymeleaf与spring整合
- SpringBoot与Thymeleaf整合
- Spring4 MVC 简单示例
- Spring4整合Hibernate4详细示例
- Spring4整合Hibernate4详细示例
- springBoot thymeleaf与Spring整合
- 一个Spring4 WebSocket群发示例
- Spring4.0整合Redis实用示例
- 关于Spring4与Hibernate4整合
- Spring4与Hibernate4整合配置
- Thymeleaf 首发教程 与 spring 整合
- Thymeleaf 首发教程 与 spring 整合
- JQuery与Thymeleaf整合:[[${someObject.someValue}]]
- SpringMVC+Spring4.0+Hibernate 简单的整合
- SpringMVC+Spring4.0+Hibernate 简单的整合
- FreeBASIC学习笔记——8.2 指针的类型与运算
- ASP.NET C#根据HTML页面导出PDF
- logback之四:输出日志到不同文件
- 请放下你的无效社交
- NIFI 系统属性 System Properties之ZooKeeper Properties
- spring4与Thymeleaf整合一个简单示例
- Redis常用数据类型
- 小朋友学C++(10):子类构造函数调用父类构造函数
- 蓝桥杯ALGO-1区间K大数
- windows bat 进入或跳转到其它目录命令
- 用jQuery的tab实现简单仿微博网页
- codeforces 546A Soldier and Bananas
- spring实现在一个类中调用另一个类的方法
- 小朋友学C++(11):“箭头(->)”和“点号(.)”操作符的区别