本人为转载:原文为: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/