jsp & thymeleaf 整合springboot 的双模板
由于之前的系统写了大量的jsp 要将模板升级到thymeleaf会造成工作繁琐 所以整合成了双模板 这里对双模板的整合做详细记录 一边以后查看。
一、项目结构
二、application.properties
三、java配置(展示视图解析的配置)
/**
*
*/
package cn.smarthse.admin.bootstrap;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import cn.smarthse.admin.interprecetor.HomeInteceptor;
import cn.smarthse.admin.interprecetor.LogInterceptor;
import cn.smarthse.modules.platform.core.framework.interceptor.repeatform.RepeatUrlFormInterceptor;
import cn.smarthse.modules.platform.core.framework.mapper.JsonMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafView;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
/**
* 《Web配置器》
*/
@Component
@Configuration
@PropertySources({
@PropertySource("classpath:smarthsetech.properties"),
@PropertySource("classpath:application.properties")})
public class WebConfiguration extends WebMvcConfigurationSupport implements ServletContextListener{
private Logger logger = LogManager.getLogger(this.getClass());
// #===============================#
// #===== 视图文件配置 settings =====#
// #===============================#
@Value("${web.view.prefix}")
private String web_view_prefix = "/WEB-INF/";
@Value("${web.view.suffix}")
private String web_view_suffix = ".jsp";
@Value("${web.view.order}")
private int web_view_order = 10;
/**
* thymeleaf配置
*/
@Value("${spring.thymeleaf.encoding}")
private String characterEncoding;
@Value("${spring.thymeleaf.content-type}")
private String contentType;
@Value("${spring.thymeleaf.mode}")
private String mode;
@Value("${spring.thymeleaf.prefix}")
private String prefix;
@Value("${spring.thymeleaf.suffix}")
private String suffix;
public void addResourceHandlers(ResourceHandlerRegistry registry) {
logger.info("配置 静态资源访问");
registry.addResourceHandler("/static/**").addResourceLocations("/static/").setCachePeriod(3600);
registry.addResourceHandler("/upload/**").addResourceLocations("/upload/").setCachePeriod(3600);
registry.addResourceHandler("/versionLog.txt").addResourceLocations("/versionLog.txt").setCachePeriod(3600);
super.addResourceHandlers(registry);
}
/**
* 配置jsp请求视图映射
* @return
*/
@Bean
public InternalResourceViewResolver resourceViewResolver()
{
logger.info("配置 InternalResourceViewResolver prefix:{},suffix:{}",web_view_prefix,web_view_suffix);
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
//请求视图文件的前缀地址
internalResourceViewResolver.setPrefix(web_view_prefix);
//请求视图文件的后缀
internalResourceViewResolver.setSuffix(web_view_suffix);
internalResourceViewResolver.setOrder(web_view_order);
internalResourceViewResolver.setViewClass(JstlView.class);
internalResourceViewResolver.setViewNames("views/*");
return internalResourceViewResolver;
}
/**
* 配置thymeleaf视图
*/
@Bean
public SpringResourceTemplateResolver getTemplateResolver(){
//模板解析器
logger.info("配置ServletContextTemplateResolver,prefix:{},suffix:{}",prefix,suffix);
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setSuffix(suffix);
templateResolver.setPrefix(prefix);
templateResolver.setTemplateMode(mode);
templateResolver.setCacheable(false);
templateResolver.setCharacterEncoding(characterEncoding);
templateResolver.setOrder(0);
return templateResolver;
}
@Bean
public SpringTemplateEngine getTemplateEngine(){
logger.info("配置SpringTemplateEngine");
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(getTemplateResolver());
return templateEngine;
}
@Bean //Thymeleaf视图解析器
public ThymeleafViewResolver getThymeleafViewResolver(){
logger.info("配置ThymeleafViewResolver");
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(getTemplateEngine());
viewResolver.setViewClass(ThymeleafView.class);
viewResolver.setContentType(contentType);
viewResolver.setViewNames(new String[] {"thymeleaf/*"});
viewResolver.setCache(false);
return viewResolver;
}
}
如图所示,使用thymeleaf模板的html文件我会放在resource/templates/thymeleaf下,jsp文件我会放在webapp/WEB-INF/views/路径下。
四、Controller
使用jsp模板:
@GetMapping(produces = "text/plain;charset=utf-8")
public String page_List(Model model) {
return "views/admin/sys/legal/legal_list";
}
使用thymeleaf模板:
@GetMapping(produces = "text/plain;charset=utf-8")
public String page_List(Model model) {
model.addAttribute("msg", "thymeleaf欢迎你");
return "thymeleaf/index";
}
五、静态页面及展示
jsp页面及展示:
thymeleaf页面及展示: