好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Boot应用配置常用相关视图解析器详解

springboot的自动装配装配了视图解析器了吗?

我们可以看到springboot自动装配的webmvcautoconfiguration类中,装配了以下关于viewresolver(视图解析器)的类。可以看到springboot已经自动装配了internalresourceviewresolver类,又是通过外部资源配置的方式来配置此视图解析器 this.mvcproperties.getview().getprefix() ,所以我们可以在 application.properties 文件配置此视图解析器用于解析jsp。

?

1

2

3

4

5

6

7

8

@bean

@conditionalonmissingbean

public internalresourceviewresolver defaultviewresolver() {

  internalresourceviewresolver resolver = new internalresourceviewresolver();

  resolver.setprefix( this .mvcproperties.getview().getprefix());

  resolver.setsuffix( this .mvcproperties.getview().getsuffix());

  return resolver;

}

springboot使用jsp

springboot在自动装配的时候默认就已经将jsp的视图解析器internalresourceviewresolver装配。所以我们只需要进行配置使用即可。在springboot中使用jsp比较麻烦一点,或许是我的个人理解存在什么误区,如果有朋友知道更好的配置方法,请留言给我。

第一步:创建自定义webapp目录,如下所示

第二步:将此文件夹配置成项目的web模块

第三步:导入jsp相关依赖

?

1

2

3

4

5

6

7

8

9

<dependency>

  <groupid>org.apache.tomcat.embed</groupid>

  <artifactid>tomcat-embed-jasper</artifactid>

  <scope>provided</scope>

</dependency>

<dependency>

  <groupid>javax.servlet</groupid>

  <artifactid>jstl</artifactid>

</dependency>

第四步:在springboot的属性文件 application.properties 中配置jsp的路由

?

1

2

spring.mvc.view.prefix=/

spring.mvc.view.suffix=.jsp

第五步:修改maven的pom.xml文件打包方式改成war(默认打包jar,打包jar包的方式使用idea启动是没什么问题,如果单独运行jar包就找不到jsp文件,如果改成war包即可)

?

1

<packaging>war</packaging>

springboot中使用thymeleaf

springboot官方是推荐使用thymeleaf作为优选的视图解析器,所以springboot对thymeleaf的支持非常好,这里仅仅演示springboot如何选用thymeleaf作用默认视图解析器。

第一步:导入thymeleaf的依赖

?

1

2

3

4

<dependency>

  <groupid>org.springframework.boot</groupid>

  <artifactid>spring-boot-starter-thymeleaf</artifactid>

</dependency>

第二步:创建存放thymeleaf模板文件夹,在resources目录下创建templates目录

这个文件夹的名字可不是我么随便命名的啊,是springboot在自动装配thymeleaf视图解析器的时候就已经预定义好了,我们看一下它的定义源码。

?

1

2

3

4

5

6

7

8

9

@configurationproperties (prefix = "spring.thymeleaf" )

public class thymeleafproperties {

 

  private static final charset default_encoding = standardcharsets.utf_8;

 

  public static final string default_prefix = "classpath:/templates/" ;

 

  public static final string default_suffix = ".html" ;

  }

springboot中使用freemark

第一步:导入maven依赖

?

1

2

3

4

<dependency>

    <groupid>org.springframework.boot</groupid>

    <artifactid>spring-boot-starter-freemarker</artifactid>

   </dependency>

第二步:创建存放freemark模板文件夹,在resources目录下创建templates目录

?

1

2

3

4

5

6

7

8

9

@configurationproperties (prefix = "spring.freemarker" )

public class freemarkerproperties extends abstracttemplateviewresolverproperties {

 

  public static final string default_template_loader_path = "classpath:/templates/" ;

 

  public static final string default_prefix = "" ;

 

  public static final string default_suffix = ".ftl" ;

  }

我们可以看到springboot在自动装配freemarker视图解析器默认是将模板文件放在classpath:/templates/路径内,我们同样可以在springboot的配置文件中自行配置。

小提示:我在写freemark视图解析器的时候并没有将第一个jsp内部资源解析器给删除掉,所以他们是并存的,所以我们可以知道springboot在装配他们的时候给予设定了优先级顺序。从下图可以看到他们的优先级顺序;freemarker>thymeleaf>internalresourceviewresolver`

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:http://HdhCmsTestcnblogs测试数据/jimisun/p/10092137.html

查看更多关于Spring Boot应用配置常用相关视图解析器详解的详细内容...

  阅读:24次