Spring Boot实现页面前后台交互Web功能

编程知识 更新时间:2023-04-05 04:47:29

Spring boot可实现Web开发功能,简化框架的配置,可以快速开发,以下是精简的步骤:
1.使用Maven构建项目,Artifact Id选择maven-archetype-quickstart
2.添加pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>
<dependency>

3.新建src/main/resources/templates包结构
4.Spring boot启动,页面前后台交互

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@SpringBootApplication
public class App 
{
    @RequestMapping("/getPage")
    public String getMyPage() {
        return "myPage";
    }
    public static void main( String[] args )
    {
        new SpringApplication(App.class).run(args);
    }
}

main方法启动Spring boot项目,getMyPage方法返回myPage字符串,thymeleaf会直接找到templates包下myPage.html页面。
5.最终效果
项目启动后,访问http://localhost:8080/getPage,返回myPage.html页面信息

更多推荐

Spring Boot实现页面前后台交互Web功能

本文发布于:2023-04-05 04:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/656e0c58f10491b7b55f2a26fbd939f9.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   页面   功能   Spring   Boot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 44852文章数
  • 14阅读数
  • 0评论数