Spring Boot
1、建立文件
打开eclipse ->上面一栏file ->new ->Spring Starter project
然后next
只勾选Spring Web ->Finish
2、创建Controller
在demo文件夹下创建文件夹controller
在controller文件夹下,建立文件testController.class文件
代码如下:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class testController {
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
}
打开demo下的文件 DidididiApplication
右键->run as ->Spring boot App
在浏览器上输入:http://127.0.0.1:8080/hello
配置成功。
3、打开页面尝试
在pom.xml添加thymeleaf依赖
<!--使用thymeleaf所需依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
ps:在<dependencies> </dependencies>
内部加入
创建一个新的controller,起名helloController
代码:
package com.example.demo.controller;
import java.util.HashMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class indexController {
@RequestMapping("/index")
public String getIndex(HashMap<String,Object> map,Model model) {
model.addAttribute("hello", "你好小明");
map.put("name", "小明");
map.put("password", "1234");
return "hello";
}
}
创建一个html文件(起名hello.html,与helloController中return对应):
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<p th:text='${name}'></p>
<p th:text='${password}'></p>
<p th:text='${hello}'></p>
</div>
</body>
</html>
重新启动服务器,用浏览器打开:http://localhost:8080/index。