Spring boot+ maven + thymeleaf + HTML 实现简单的web项目
2018-01-26 14:31
633 查看
第一步: 创建一个SpringBoot应用
第二步: 创建一个实体,用来存储数据,在src/main/java/com/example/first下创建包entity , 在entity下创建Person.java文件
第三步: 创建resource, 在src/main/java/com/example/first下创建包resource, resource下创建PersonResource.java
第三步:创建index.html文件
第四步:在application.properties中添加模板引擎
第五步: 在pom文件中添加依赖
第五步:启动前目录结构
第六步: 配置启动
第七步:启动SpringbootApplication
第八步:在地址栏输入http://localhost:8080/person/show?name=zhang&age=23
第九步: 显示效果
第二步: 创建一个实体,用来存储数据,在src/main/java/com/example/first下创建包entity , 在entity下创建Person.java文件
package com.example.first.entity; public class Person { private String name; private int age; //seter , getter,toString方法 }
第三步: 创建resource, 在src/main/java/com/example/first下创建包resource, resource下创建PersonResource.java
package com.example.first.resource; import com.example.first.entity.Person; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/person") public class PersonResource { @RequestMapping(value = "/show",method = RequestMethod.GET) /**请求地址: localhost:8080/person/show?name=zhang&age=23 请求方式是get*/ private String show(@RequestParam("name")String name,@RequestParam("age")int age,ModelMap map){ /**@RequestParam("name")绑定请求地址中的name到参数name中 ModelMap map 存放返回内容*/ Person person = new Person(); person.setName(name); person.setAge(age); map.addAttribute("person",person); return "index"; /**返回的是显示数据的html的文件名*/ } }
第三步:创建index.html文件
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" lang="zh"> <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> <span th:text="${person.name}"></span><hr/> <span th:text="${person.age}"></span><hr/> <span th:text="${person}"></span> </body> </html>
第四步:在application.properties中添加模板引擎
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode = HTML5
第五步: 在pom文件中添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.first</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> 4000 <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第五步:启动前目录结构
第六步: 配置启动
第七步:启动SpringbootApplication
package com.example.first; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } }
第八步:在地址栏输入http://localhost:8080/person/show?name=zhang&age=23
第九步: 显示效果
相关文章推荐
- thymeleaf :springboot整合mybatis的web项目中使用html而不是jsp当页面渲染
- idea+maven+springboot搭建web简单项目经历小计(RestController)
- SpringBoot+Maven项目实战(6):整合Log4j和Aop,实现简单的日志记录
- 基于SpringBoot + Mybatis实现SpringMVC Web项目
- 基于SpringBoot + Mybatis实现SpringMVC Web项目【原创】
- 基于Maven的SpringBoot项目实现热部署的两种方式
- spring boot+thymeleaf+bootstrap 简单实现后台管理系统界面
- SpringBoot + Thymeleaf + JPA创建一个web项目
- maven项目 spring-boot 整合 mybatis 实现查询功能demo
- maven+Spring简单web项目demo
- Spring Boot + Thymeleaf 创建web项目
- SpringBoot+SpringData实现SpringMVC简单项目
- 一个非常简单的spring-boot web项目(一)
- Spring Boot 学习二、基于SpringBoot + Mybatis实现SpringMVC Web项目
- (3)Spring-boot学习 做一个简单的带访问数据库的web项目
- Maven构建一个最简单的Spring Boot + Spring MVC项目
- 基于SpringBoot + Mybatis实现SpringMVC Web项目【原创】
- 基于SpringBoot + Mybatis实现SpringMVC Web项目【原创】
- eclipse 使用maven 构建springbooot+mysql +freemarker 简单web 项目