本人爲轉載:原文爲:https://blog.csdn.net/weixin_40550726/article/details/80675911
如有侵權請聯繫本人刪除。
我這邊是對其進行一些修改以及說明。
開發demo下載:請點擊
注意訪問路徑(springboot不用項目名訪問的):
添加鏈接描述http://localhost:8080/
**
搭建步驟:
**
1.右擊新建一個springboot項目
2.項目名自定義,其他的可以默認
3.選擇這兩項,然後點擊完成即可
4.項目搭建好的具體結構爲:
5.打開最重要的文件,這個文件的目錄必須位於其他有註解文件的上級或者同一個文件夾,不可以分在不同的文件夾裏?
6.創建Person類
7.貼代碼:
package com.example.demo.Person;
public class Person {
private String name;
private Integer age;
public Person() {
super();
}
public Person(String name,Integer age) {
super();
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
8.創建controller層,右擊新建
9.貼代碼:
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.Person.Person;
@Controller
public class controller {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("userLogin")
public String userLogin(Model model) {
Person p=new Person("yxc",20);
model.addAttribute("user", p);
return "userLogin";
}
}
10.在src/main/resources文件夾下的templates?創建index.html和userLogin.html
11.貼代碼
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1> this is my first springboot project</h1>
<a href="/userLogin">west world</a>
</body>
</html>
userLogin.html:
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
welcome to west world
<span th:text="${user.name}"></span>
</body>
</html>
12.運行,訪問:http://localhost:8080/