Springboot-web入门开发之使用Eclipse搭建我的第一个spring-boot-web项目

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章