springboot 跳转html
程序员文章站
2022-06-26 14:44:28
项目目录结构pom文件 org.springframework.boot spring-boot-starter-thymeleaf
项目目录结构
pom文件
<dependencies><!--pom坐标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!--jdk-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
application.properties 配置文件
spring.thymeleaf.prefix=classpath:/templates/
index.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"/><title>主页</title></head>
<body><h1>主页</h1></body>
</html>
rule.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"/><title>规则</title></head>
<body><h1>规则</h1></body>
</html>
DemoApplication
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);//初始化服务
}
}
MyController
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping("/index")
public String test(){//localhost:8080/index 访问templates下的页面 index.html
return "index";
}
@RequestMapping("/rule")
public String test1(){//localhost:8080/rule 访问templates下的页面 rule/rule.html
return "rule/rule";
}
@ResponseBody
@RequestMapping("/test")
public String test2(){//localhost:8080/test ResponseBody返回字符串
return "这是一个字符串";
}
}
访问页面效果
本文地址:https://blog.csdn.net/weixin_42660202/article/details/112025827
推荐阅读
-
IOS开发之tableView点击行跳转并带有“显示”更多功能
-
javascript+HTML5 canvas绘制时钟功能示例
-
location.replace算不算跳转(js删除history指定记录)
-
JavaScript+HTML5 canvas实现放大镜效果完整示例
-
asp.net动态生成HTML表单的方法
-
html自学网站有哪些(html5零基础入门教程)
-
springboot框架简介(关系型数据库和非关系型区别)
-
jsp response.sendRedirect不跳转的原因分析及解决
-
html转jsp之后样式变了(js类型转换的使用方法)
-
手把手教学Android用jsoup解析html实例