初学SpringBoot之访问页面

一、前言

    这是我学习SpringBoot的记录的第三篇文章。

    本片主要记录SpringBoot写一个接口进行测试,之后再访问一个html页面。

二、先写一个接口进行测试

    2.1引入jar包

        <!--spring boot 集成Spring MVC-->
        <!--spring-webmvc + autoconfigure + logback + yaml + tomcat-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

    2.2编写接口

    1、创建一个TestController类,写一个接口

    项目接口如下:

    类详情如下:

@RestController
public class TestController {
    @RequestMapping("/test")
    public String  test(){
        return "Hello World !!!";
    }
}

    2、执行

    在浏览器中输入地址:http://localhost:8080/test

    结果如下:

三、访问一个HTML页面

    3.1在resources下建立static、templates两个文件夹;在static文件夹下建立world.html;在templates文件夹下建立hello.html

    结构如下:

    hello.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello</title>
</head>
<body>
        <h1>这是hello.html页面</h1>
</body>
</html>

    world.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>world</title>
</head>
<body>
        <h1>这是world.html页面</h1>
</body>
</html>

    3.2访问http://localhost:8080/world.html  和  http://localhost:8080/hello.html

    结果如下:

    我们访问不到hello.html

    3.3通过TestController访问两个html

    1、将TestController的注解改成@Controller,并添加4个方法

    @RestController注解,相当于@Controller+@ResponseBody两个注解的结合, @Responsebody后,返回结果直接写入HTTP response body中,不会被解析为跳转路径,所以是打印字符串的效果,不是跳转效果。

@Controller
public class TestController {
    @RequestMapping("/test")
    @ResponseBody
    public String  test(){
        return "Hello World !!!";
    }


    @RequestMapping("/tohello")
    public String  tohello(){
        return "hello.html";
    }
    @RequestMapping("/toworld")
    public String  toworld(){
        return "world.html";
    }



    @RequestMapping("/tohello2")
    public String  tohello2(){
        return "hello";
    }
    @RequestMapping("/toworld2")
    public String  toworld2(){
        return "world";
    }
}

    2、 访问  http://localhost:8080/tohello、http://localhost:8080/tohello2

    结果:都访问不到页面

    3、访问  http://localhost:8080/toworld、http://localhost:8080/toworld2

    结果:toworld可以访问到页面,toworld2不可以访问到页面

    SpringBoot里面没有我们之前常规web开发的WebContent(WebApp),它只有src目录,在src/main/resources下面有两个文件夹,[static]和[templates],springboot默认static中放静态页面,而templates中放动态页面。静态页面可以直接访问,在当我们没有做相关配置的时候,返回值要是“world.html”,单独一个“world”,是找不到的。动态页面需要先请求服务器,访问后台应用程序,然后再转向到页面。

    4、再pom文件中添加jar包

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

   5、引入jar包,重新启动服务,再次访问2、3步骤的地址

    tohello、tohello2 结果:均访问到页面

    toworld、toworld2 结果:不可访问到页面

    静态页面的return默认是跳转到/static/index.html,当在pom.xml中引入了thymeleaf组件之后,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/index.html。动态跳转有或没有html后缀都可以。

四、参考

https://blog.csdn.net/jintingbo/article/details/81633615

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值