springboot跳转页面

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";
    }
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值