SpringBoot创建web项目
本人使用的是eclipse平台,安装SpringBoot插件即可。下面介绍如何使用springboot创建web项目。
1.创建项目工程
File->New->Project...->Spring Boot->Spring Starter Project,点击Next按钮
填写相关信息,点击Next按钮
选择Spring Web,点击Finish按钮
web主要通过启动类 + POM配置 + 控制器+ 视图 + 项目配置实现,完整目录结构如下:
启动类: WebP1Application.java
POM配置: pom.xml
控制器:controller文件夹下的.java文件
视图:templates文件夹下的.html文件
项目配置:application.properties文件或application.yml文件(直接改后缀名为.yml即可)
2.启动类WebP1Application.java(本文不需要任何改动,可根据自己的需求改动)
package web.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebP1Application {
public static void main(String[] args) {
SpringApplication.run(WebP1Application.class, args);
}
}
3.pom.xml配置(POM文件用于maven构建,这里这种添加spring-boot-starter-web和spring-boot-starter-thymeleaf依赖,其中thymeleaf是用于html页面的code)
完整的文件内容如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>web.demo</groupId>
<artifactId>WebP1</artifactId>
<version>1.0</version>
<name>WebP1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version> <!-- JDK版本 -->
</properties>
<dependencies>
<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>
<!-- 添加模板依赖thymeleaf(spring官方不推荐使用JSP,默认thymeleaf作为模板) -->
<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> <!-- 使用maven插件编译和打包 -->
</plugin>
</plugins>
</build>
</project>
4.添加控制器controller(controller文件夹下的User.java文件定义的User类是UserController.java要用到的数据)
注意控制器文件夹一定和启动类在一个目录(我的当初不在一个目录,总是不能成功访问页面)
UserController.java内容如下:
package web.demo.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserController {
@RequestMapping(value="/user", method = RequestMethod.GET) //网址
public String Info(Model model) {
List<User> users=new ArrayList<User>();
users.add(new User("李雷", "男", "爬山"));
users.add(new User("韩梅梅", "女", "游泳"));
users.add(new User("小明", "男", "踢皮球"));
model.addAttribute("users", users);
model.addAttribute("result", "成功显示学生信息");
return "user"; //返回user.html
}
}
User.java内容如下:
package web.demo.controller;
public class User {
private String name;
private String sex;
private String hobby;
public User() {
super();
}
public User(String name, String sex, String hobby) {
super();
this.name = name;
this.sex = sex;
this.hobby = hobby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
5.添加视图文件,在resources文件夹的templates文件夹下创建.html文件
User.html内容如下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Java测试</title>
</head>
<body>
<div th:text="${result}"></div>
<table>
<tr>
<td>姓名</td>
<td>性别</td>
<td>爱好</td>
</tr>
<tr th:each="user:${users}">
<td th:text="${user.name}"></td>
<td th:text="${user.sex}"></td>
<td th:text="${user.hobby}"></td>
</tr>
</table>
</body>
</html>
6.配置项目信息
application.properties内容如下:
server.servlet.context-path=/
server.port=${port:8888}
server.tomcat.uri-encoding=UTF-8
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
如果是application.yml文件,内容如下:
server:
port: 8888
tomcat:
uri-encoding: UTF-8
servlet:
context-path: /
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
cache: false
7.启动项目, 右键项目->Run As->Spring Boot App,没有报错就表示正常run起来了
访问网页http://localhost:8888/user即可正确浏览到页面