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