【springboot初学】用Controller注解显示页面

    科技2025-10-11  16

    1、新建新建spring Initializr

    只是在选择依赖时有不同

    多了一个模版引擎thymeleaf

    2、新建controller包、controller类

    package com.example.controller.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class controller { @GetMapping("/register") @RequestMapping("/register") public String register(){ return "register"; } }

    这里用@Controller注解,@Controller在Springboot中一定要配置前端模板,Thymeleaf、bootStrap等。

    register类返回register,对应的在resources->templates->新建register.html的register对应

    <!DOCTYPE html> <html xmlns:th="http://thymeleaf.org"> <head> <meta charset="UTF-8"> <title>注册页面</title> </head> <body> <h2>欢迎来到注册页面</h2> </body> </html>

    这里需要改成thymeleaf的框架。

    3、运行程序

    Processed: 0.011, SQL: 9