SpringBoot创建MVC网站
来源:互联网 发布:中兴网络机顶盒怎么用 编辑:程序博客网 时间:2024/06/01 10:43
使用springboot构建MVC项目,并进行表单提交
1.准备
1.1开发工具:Intellij Idea
1.2新建项目,勾选web ,thymleaf依赖,项目结构如下:
2.开发
2.1pom.xml内容:
<?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</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</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> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
2.2MainController类内容:
package com.example.demo;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class MainController { @RequestMapping("/") public String index(User user, Model model){ model.addAttribute("user",user); return "index"; }}
2.3User类内容:
package com.example.demo;public class User { private String name; private String sex; public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public String getName() { return name; } public String getSex() { return sex; }}
2.4DemoApplication类内容:
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
2.5 index.html内容:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"/> <title>Title</title></head><body><form action="/" method="post" th:object="${user}"> <input th:field="*{name}" placeholder="请输入您的名字"/> <input th:field="*{sex}" placeholder="请输入您的性别"/> <button type="submit">提交</button> <p th:text="'您的名字:'+${user.name}"></p> <p th:text="'您的性别:'+${user.sex}"></p></form></body></html>
阅读全文
1 0
- SpringBoot创建MVC网站
- springboot mvc 处理异常
- SpringBoot--MVC相关配置
- 使用ASP.NET MVC框架创建电子商务网站
- SpringBoot动态创建Bean
- 创建一个springboot项目
- 创建第一个SpringBoot
- SpringBoot创建Web项目
- 创建Springboot程序
- SpringBoot创建web工程
- springboot项目创建问题
- 使用Maven创建SpringBoot
- Gradle创建springboot项目
- idea创建springboot项目
- springboot项目创建
- SpringBoot学习-项目创建
- 创建一个springboot项目
- Springboot创建数据库
- Python —— 杂记
- Java学习第十九天
- dd
- CSS中的单位
- java设计模式之单例模式
- SpringBoot创建MVC网站
- 快速排序
- 可视化CNN
- Unity3d 物体速度快直接穿透墙体没有发生碰撞怎么办?
- CAS 交互流程
- hbase 和 hive 学习
- 计算机网络通讯
- HDU 4614 线段树(区间更新)+二分
- hdu 4635(tarjan)