Springboot2.x配置thymeleaf和jsp双视图解析器

一、Springboot1.x中的配置不再适用

在Springboot2.x中,使用JSP时,打成jar包时运行,JSP会出现404,问题主要来源于Springboot对jsp的支持限制。因此想要Springboot项目中的jsp和thymeleaf同时生效必须将Springboot以war包的方式部署运行。

二、springboot以war包部署

首先必须去除starter-web中的内置tomcat,并引入外部的tomcat包。下面是配置视图渲染和web的pom配置。


	
		org.springframework.boot
		spring-boot-starter-web
		
			
				org.springframework.boot
				spring-boot-starter-tomcat
			
		
	
	
		org.springframework.boot
		spring-boot-starter-tomcat
		provided
	
	
		org.springframework.boot
		spring-boot-starter-thymeleaf
	
	
		org.apache.tomcat.embed
		tomcat-embed-jasper
		provided
	
	
		javax.servlet
		jstl
		provided
	
	
		javax.servlet
		javax.servlet-api
		provided
	 


	
		
			org.springframework.boot
			spring-boot-maven-plugin
			
				
					
						repackage
					
				
			
		
	

三、修改启动类

以war部署必须实现org.springframework.boot.web.servlet.support.SpringBootServletInitializer的configure方法

//1.方式一,启动类继承SpringBootServletInitializer实现configure:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(Application.class);
	}
}
2.方式二,新增加一个类继承SpringBootServletInitializer实现configure:
public class ServletInitializer extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		//此处的Application.class为带有@SpringBootApplication注解的启动类
		return builder.sources(Application.class);
	}
}

注意事项:
使用外部Tomcat部署访问的时候,application.properties(或者application.yml)中配置的
server.port=
server.servlet.context-path=
将失效,如有需要可在外部tomcat中设置,否则会以默认端口8080,和默认初始请求/访问项目

四、配置多视图解析器

	@Configuration
	@EnableWebMvc
	@ComponentScan
	public class WebViewConfig implements WebMvcConfigurer {
    /**
     * @Description: 注册jsp视图解析器
     */
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/"); //配置放置jsp文件夹
        resolver.setSuffix(".jsp");
        resolver.setViewNames("jsp/*");  //重要 setViewNames 通过它识别为jsp页面引擎
        resolver.setOrder(2);
        return resolver;
    }
    /**
     * @Description: 注册html视图解析器
     */
    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setTemplateMode("HTML");
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setCharacterEncoding("utf-8");
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    /**
     * @Description: 将自定义tml视图解析器添加到模板引擎并主持到ioc
     */
    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        return templateEngine;
    }
    /**
     * @Description: Thymeleaf视图解析器配置
     */
    @Bean
    public ThymeleafViewResolver viewResolverThymeLeaf() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("utf-8");
        viewResolver.setViewNames(new String[]{"thymeleaf"});
        viewResolver.setOrder(1);
        return viewResolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * @Description: 配置静态文件映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/WEB-INF/static/");
    }

配置完毕后即可使jsp和thymeleaf同时生效了
注意:
1.为了符合spring-web和springboot对于视图访问的标准,通过以上配置,我们仍然将html文件放置于src\main\resources\templates文件夹下,而jsp文件放置于webapp\WEN-INF\jsp文件夹下,如下图
Springboot2.x配置thymeleaf和jsp双视图解析器_第1张图片
2.视图所使用的静态资源需要放置在各自的静态资源区

五、验证

首先写出Controller

@Controller
public class TestController {
    @RequestMapping("/")
    public String index() {
        return "jsp/index";
    }

    @RequestMapping("/jspindex")
    public String jspindex() {
        return "jsp/jspindex";
    }

    @RequestMapping(value = {"/thymeleafindex.htm"})
    public String index(Model model) {
        return "thymeleafindex";
    }
}

接下来写三个简单的页面用于验证
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>



    
    Insert title here


Welome to springboot with jsp and thymeleaf!

jsp
thymeleaf

jspindex.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>



    
    Insert title here


Welome to jsp Page!

thymeleafindex.html




    
    
    
    查询结果


Welome to thymeleaf Page!

运行结果如下:
项目刚启动弹出的页面
Springboot2.x配置thymeleaf和jsp双视图解析器_第2张图片
分别点击链接的页面
Springboot2.x配置thymeleaf和jsp双视图解析器_第3张图片
Springboot2.x配置thymeleaf和jsp双视图解析器_第4张图片
到此Springboot2.x配置thymeleaf和jsp双视图解析器就结束了!

你可能感兴趣的:(Springboot2.x配置thymeleaf和jsp双视图解析器)