SpringBoot里面只有src目录,在src/main/resources下面有两个文件夹,[static]和[templates],springboot默认static中放静态页面,而templates中放动态页面

themleaf和freemarker的依赖不会产生矛盾,配置这两个依赖作用是controller返回的视图解析;
搭配application中配置的jsp的前后缀和themleafcalsspath都会影响返回的指向页面路径,所以选择哪种页面解析不要轻易更改。看到好多说thymeleaf是官方推荐,其实freemarker才是,下面基本比较文章中有说明

关于FreemarkerThymeleaf
框架基本的比较
性能比较

springboot跳转jsp

  1. 加入依赖 servlet-api 的 和 tomcat-embed-jasper的如果需要jsp页面的表达式支持还需要jstl的依赖
		<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

  
  
    1. 添加正确路径的webapp,新建自己的文件夹名称,添加web的设置

    添加webapp的文件夹,注意与java,resources平级,
    在file–>project stucture中添加web模块并指明路径:看是否有web,没有添加(下图红色箭头添加web),然后在右侧右下角web resource Directionies增加+指向该项目的webapp目录,
    在这里插入图片描述
    然后就可以看到webapp文件夹上有蓝点代表成功,在这里就可以new->jsp;没有添加web这一步是new中没有jsp选项的
    在这里插入图片描述
    3. 配置application.yml或者properties 中寻址的前后缀,
    在application中添加

    spring.mvc.view.prefix=/
    spring.mvc.view.suffix=.jsp
    
      1. controller的编写
        跳转指向具体的位置,是与配置中的前后缀拼接后是否指向正确路径
        类上的注释 必须是**@controller**,如果是 @RestController是不能返回页面的
        @GetMapping(value = "/index")
          public String indexw() {
              System.out.println("====this is index");
              return "index";
          }
      
       
       
        需要注意的一点

        这种方法必须用mvn spring-boot:run这种方式启动才可以访问得到(IDEA右侧有Maven->plugin中有run的选项)
        而直接启动main方法,看起来是启动无异常,但是会404找不到具体的页面
        详细原因,这版讲的很清楚
        大致原因是不同的启动方式有不同的calsspath会导致找不到具体的jsp页面


        springboot跳转html

        除了以下的方法通过controller跳转,也可直接访问localhost:8080/xxx.html这种也是可以的(但是默认静态页面放在static中才有效果跳转的默认指向templates)

        1. 增加thymeleaf的依赖
        		<dependency>
        			<groupId>org.springframework.boot</groupId>
        			<artifactId>spring-boot-starter-thymeleaf</artifactId>
        		</dependency>
        
         
         
          1. 配置前缀在application中 ,作用为知名返回的路径和这个前缀拼接可以找到具体的文件夹,并页面放在这之下
          spring.thymeleaf.prefix=classpath:/templates/
          
           
           
            1. 配置页面,新建项目一般已经有了具体文件夹了
              在这里插入图片描述
            2. 配置具体的跳转controller
                @RequestMapping("/feng")
                public String getHtml(){
                    System.out.println("====");
                    return "feng";
                }
            

              直接跳转成功显示。


              springboot跳转Thymeleaf

              1. 依赖
                <dependency>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-starter-thymeleaf</artifactId>
                </dependency>
              
               
               
                1. 新建them.html 在templates目录下
                <!DOCTYPE html>
                <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <title>themleaf</title>
                </head>
                <body>
                <p>welcome to <span th:text="${key}"></span>.</p>
                </body>
                </html>
                
                1. controller中添加属性,return返回页面,并不需要配置前后缀默认跳转templates,前后缀如果有需要在application中配置 spring.thymeleaf.prefix=classpath:/templates/
                    @RequestMapping("/themleaf")
                    public String themleaf(Model model) {
                        System.out.println("themleaf----->");
                        model.addAttribute("key", "三坛海会大神哪吒");
                        return "them";
                    }
                

                springboot跳转FreeMarker

                1. 增加依赖,去掉tomcat-embed-jasper的依赖,如果同时存在freemaker和jsp的页面解析,优先返回freemarker
                		<dependency>
                			<groupId>org.springframework.boot</groupId>
                			<artifactId>spring-boot-starter-freemarker</artifactId>
                		</dependency>
                
                1. application 中配置端口,其他不需要
                2. 在templates文件夹下配置页面,新建chu.ftl模板文件
                <html>
                <head>
                    <title></title>
                </head>
                <body>
                hello welcome, ${name}
                </body>
                </html>
                
                1. 配置controller跳转页面
                  @RequestMapping("/home")
                    public String home1(Model model) {
                        System.out.println("chu====》");
                        model.addAttribute("name", "老铁");
                        return "chu";
                    }
                
                 
                 
                Logo

                华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

                更多推荐