SpringBoot视图层:(2)三种页面jsp,freemarker,thymeleaf共存问题

1.在springboot的配置文件pom.xml里面如果三者共存,会让jsp页面访问不到,直接报错,但是另外两个不错

三者pom.xml的配置文件:

   <!-- thymeleaf的引用坐标-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

  <!-- freemarker的引用坐标-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
     <!-- jstl的引用坐标-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

2.当jsp和freemarker共存时

application.properties配置文件:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

controller层:

@Controller
public class UserController{
    @RequestMapping("/showUser")
    public String showUser(Model model){
        List<User> list = new ArrayList<User>();
        list.add(new User(1,"张三",20);
        list.add(new User(2,"张三",20);
        list.add(new User(3,"张三",20);
        model.addAttribute("list",list);
        return "userList";
    }
     @RequestMapping("/showUserFtl")
    public String showUser(Model model){
        List<User> list = new ArrayList<User>();
        list.add(new User(1,"张三",20);
        list.add(new User(2,"张三",20);
        list.add(new User(3,"张三",20);
        model.addAttribute("list",list);
        return "index";
    }
}

注意此时访问时:

http://127.0.0.1:8080/showUser
跳转的是userList.jsp页面
http://127.0.0.1:8080/showUserFtl
跳转的是index.ftl页面(注意,我在webapp下面建的有index.jsp文件)

如果controller层返回的是两个一样的userList字符串,并且在src/main/resources/templates/文件下有userList.ftl文件,那么无论访问的是showUser还是showUserFtl最终都是走的userList.ftl文件,因为freemarker的优先级高于jsp的优先级。

3.当thymeleaf和freemarker共存时,假如有相同的名字:index.html,index.ftl时,优先的是freemarker,还是因为freemarker的优先级比thymeleaf高

4.当jsp和thymeleaf共存时,访问jsp直接报错,但是访问thymeleaf没问题,也不会报错

猜你喜欢

转载自blog.csdn.net/u012060033/article/details/88965123