使用IntelliJ IDEA一键初始化一个web应用
在打开新建项目后选择spring initializr,然后next
在type选择gradle project,language选择java,然后next
勾选web,随后next
输入项目名称和项目路径就可以finish啦,这里我使用默认的demo名称
下面就进入spring boot的世界啦,
在DemoApplication文件中已经自动写好了程序入口,这里不用改动了1
2
3
4
5
6
7
8
9
10
11
12
13
14package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// SpringBootApplication用来注解应用类,在里面的main函数中启动web服务
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在DemoApplication.java文件同级目录下新建controller包,用来存放路由类,,这里我创建一个第一个路由类文件firstController.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31package com.example.demo.controller;
import com.example.demo.user.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// RestController注解用来声明后面的类里面有路由信息
public class firstController {
// RequestMapping注解声明后面的方法是一个路由方法
"hello") (
public String Hello(User user){
return "Hello World!" + user.getUsername();
}
// 使用HttpServletResponse类型的参数可以直接通过这个response写入响应信息
"hello1") (
public void Hello1(HttpServletResponse response){
try {
response.getWriter().write("hello1");
} catch (IOException e) {
e.printStackTrace();
}
}
}
请求转发
secondController.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// Controller注解会根据返回的字符串找到对应的静态文件返回,RestController注解会直接返回字符串作为响应体
public class secondController {
"dispatcher") (
public String dispatcher(){
return "dispatcher1.html";
}
"dispatcher1") (
public void dispatcher1(HttpServletRequest request, HttpServletResponse response){
try {
request.getRequestDispatcher("dispatcher.html").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这里dispatcher和dispatcher1是一个效果,都会转发到另外一个页面(dispatcher转发到dispatcher1.html,dispatcher1转发到dispatcher.html)
重定向
thirdController.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
public class thirdController {
"redirect", method = RequestMethod.GET) (value =
public String redirectd(){
// 返回"dispatcher.html",会默认按照"forward:dispatcher.html",进行转发
// "redirect:dispatcher.html",redirect表示重定向,重定向和转发是有区别的,重定向会改变浏览器显示的url地址,转发不会改变
return "redirect:dispatcher.html";
}
// 在RequestMapping注解后紧跟ResponseBody注解,和RestController注解类起到相同的效果,即直接将字符串作为响应
"controller2rescontroller") (
public String contrl(){
return "this is a string";
}
}
模版
使用thymeleaf实现模版渲染,在build.gradle 文件里添加插件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// ------------添加thymeleaf插件-------start
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.thymeleaf:thymeleaf:3.0.9.RELEASE')
compile('org.thymeleaf:thymeleaf-spring4:3.0.9.RELEASE')
compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.2.2')
// ------------添加thymeleaf插件-------end
}
添加好插件后就可以渲染模版了,
templateController.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
public class templateController {
"testTemplate") (
public String testTemplate(Model model){
model.addAttribute("username", "storm");
return "index";
}
}
这里直接返回index,框架会自动去templates文件夹下寻找对应的index.html文件,同时在html文件里可以使用model的username变量,
index.html1
2
3
4
5
6
7
8
9
10<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>index</title>
</head>
<body>
hello! <span th:text="${username}"></span>
</body>
</html>
在html标签中写入:xmlns:th=”http://www.thymeleaf.org,就可以通过th来使用model的变量了,使用方式为th:text="${变量名}",或者input标签可以使用 th:value=”${变量名}”
thymeleaf会严格检查html的完整性,如果html存在标签不完整会报错,
最后,贴一下整个项目的文件结构图