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

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

在build.gradle中添加依赖

buildscript {

repositories {

mavenCentral()

}

dependencies {

classpath('org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE')

}

}

group "com.li"

version "1.0-SNAPSHOT"

apply plugin: "java" //java 插件

apply plugin: "org.springframework.boot" //spring boot 插件

apply plugin: "io.spring.dependency-management"

apply plugin: "application" //应用

//mainClassName = "Main.Application"

sourceCompatibility = 1.8

repositories {

mavenCentral()

}8:

dependencies {

compile("org.springframework.boot:spring-boot-starter-web",

"org.springframework.boot:spring-boot-starter-test",

// "org.springframework.boot:spring-boot-starter-activemq:1.5.9.RELEASE",

"org.springframework.boot:spring-boot-starter-cache",

"org.springframework.boot:spring-boot-devtools",

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

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

// 'org.apache.commons:commons-lang3:3.4',

// 'org.apache.commons:commons-pool2',

// 'org.apache.logging.log4j:log4j-core:2.7',

// 'org.springframework.boot:spring-boot-starter-security',

"org.springframework.boot:spring-boot-starter-thymeleaf",

"net.sourceforge.nekohtml:nekohtml"

)

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

// runtime ("org.apache.tomcat.embed:tomcat-embed-jasper")

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

}

2: 在application.yml中进行配置

spring:

thymeleaf:

mode: LEGACYHTML5

cache: false

datasource:

driver-class-name: com.mysql.jdbc.Driver

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

username: root

password: 1367356

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

packagecom.li.controller;public classParameter {privateString id;privateString name;publicString getId() {returnid;

}public voidsetId(String id) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}

}

4: controller层

packagecom.li.controller;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;

@Controller

@RequestMapping("/home")public classHomeController {

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

model.addAttribute("param1", 3);

Parameter parameter=newParameter();

parameter.setId("111111");

parameter.setName("liyafei");

model.addAttribute("parame", parameter);return "home";

}

}

5:启动类

packagecom.li;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;

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

SpringApplication.run(SpringBootModelAndThymeleaf.class, args);

}

}

6:前端展示home.html

${param1}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值