springboot重定向到页面_springboot 使用model重定向到html模板,对数据进行展示

时间:2025-03-20 10:18:26

1:使用springboot, ,html使用thymeleaf,nekohtml模板

在中添加依赖

buildscript {

repositories {

mavenCentral()

}

dependencies {

classpath(':spring-boot-gradle-plugin:1.5.')

}

}

group ""

version "1.0-SNAPSHOT"

apply plugin: "java" //java 插件

apply plugin: "" //spring boot 插件

apply plugin: "-management"

apply plugin: "application" //应用

//mainClassName = ""

sourceCompatibility = 1.8

repositories {

mavenCentral()

}8:

dependencies {

compile(":spring-boot-starter-web",

":spring-boot-starter-test",

// ":spring-boot-starter-activemq:1.5.",

":spring-boot-starter-cache",

":spring-boot-devtools",

// ":mybatis-spring-boot-starter:1.3.0",

"mysql:mysql-connector-java:5.1.35",

// ':commons-lang3:3.4',

// ':commons-pool2',

// '.log4j:log4j-core:2.7',

// ':spring-boot-starter-security',

":spring-boot-starter-thymeleaf",

":nekohtml"

)

// compile('commons-net:commons-net:3.1')

// runtime (":tomcat-embed-jasper")

testCompile group: 'junit', name: 'junit', version: '4.12'

}

2: 在中进行配置

spring:

thymeleaf:

mode: LEGACYHTML5

cache: false

datasource:

driver-class-name:

url: jdbc:mysql://localhost:3306/youhuigou

username: root

password: 1367356

3:定义一个对象,存放数据

;public classParameter {privateString id;privateString name;publicString getId() {returnid;

}public voidsetId(String id) { =id;

}publicString getName() {returnname;

}public voidsetName(String name) { =name;

}

}

4: controller层

;;;;

@Controller

@RequestMapping("/home")public classHomeController {

@RequestMapping("/home1")publicString home1(Model model) {

("param1", 3);

Parameter parameter=newParameter();

("111111");

("liyafei");

("parame", parameter);return "home";

}

}

5:启动类

;;;

@SpringBootApplicationpublic classSpringBootModelAndThymeleaf {public static voidmain(String[] args){

(, args);

}

}

6:前端展示

${param1}