栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于springboot完整创建一个简单的前后端交互项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于springboot完整创建一个简单的前后端交互项目

1、首先创建一个springboot项目





2、项目创建完成 1、项目初始目录

2、尽量设置为自己的maven配置

3、添加依赖

            org.springframework.boot
            spring-boot-starter-web
            2.5.0
        
4、在项目中创建了这两个文件 static目录


IndexController

package com.wei.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/")
public class IndexController {

    @GetMapping
    public String index(){

        return "index.html";
    }
}

index.html




    
    Title


    

Hello,World!

3、启动项目

打开浏览器访问地址:
1、 http://localhost:8080/
2、http://localhost:8080/index.html 直接访问
上面两个地址都可以访问访问

templates目录(动态) 1、添加依赖

            org.springframework.boot
            spring-boot-starter-thymeleaf
            2.6.4
        
2、在resources建立目录


hello.html




    
    Title


  

hello,wei!

3、修改indexController文件
package com.wei.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping
public class IndexController {

    @GetMapping("/")
    public String index(){

        return "index.html";
    }
    
    @GetMapping("/hellodemo")
    public String hello(){
        return "hello";
    }
}

访问地位为:http://localhost:8080/hellodemo
动态页面不能直接访问

总结

静态页面的return默认是跳转到/static/目录下,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/下,注意看两者return代码也有区别,动态没有html后缀。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/945125.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号