帅气咕杂货间

在springBoot中整合JSP

Word count: 1,476 / Reading time: 8 min
2018/08/21 Share

前言

SpringBoot支持使用JSP,但官方并不推荐。

配置application.properties

1
2
3
4
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/view/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp

加入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- jstl支持-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--添加对tomcat的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!--对jsp的支持-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

新建jsp文件

新建 webapp - WEB-INF - view文件夹,将jsp文件放入其中

image

index.jsp

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<%--
Created by IntelliJ IDEA.
User: mao
Date: 17-9-9
Time: 下午4:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>招新报名表</title>
<base href="/">
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="四川大学信息安全与网络攻防协会网站"/>
<meta name="keywords" content="网络攻防,信息安全,四川大学"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/css/materialize.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row card" style="padding: 20px;">
<div class="row center-align">
<img class="responsive-img col s12 l4" src="https://scuisdc.org/assets/logo_dark.png">
<h4 class="col s12 l8">招新报名表</h4>
</div>
<form class="col s12" action="" method="post">
<div class="row">
<div class="input-field col s12">
<input placeholder="王大锤" id="name" type="text" required name="name"
class="validate">
<label for="name">姓名</label>
</div>
<div class="input-field col s12">
<input placeholder="2017141463XXX" id="stuId" minlength="13" maxlength="13" type="number" required
name="stuId"
class="validate">
<label for="stuId">学号</label>
</div>
</div>
<div class="row">
<div class="col s12 l6">
性别:
<input name="gender" type="radio" id="male" value="male" checked/>
<label for="male">男</label>
<input name="gender" type="radio" id="female" value="female"/>
<label for="female">女</label>
<input name="gender" type="radio" id="transgender" value="transgender"/>
<label for="transgender">其他</label>
</div>
<div class="col s12 l6">
民族:
<input name="nationality" type="radio" id="chinese" value="chinese" checked/>
<label for="chinese">汉族</label>
<input name="nationality" type="radio" id="others" value="others"/>
<label for="others">其他</label>
</div>
</div>

<div class="row">
<div class="input-field col s6">
<input placeholder="仅用于后续通知" id="phone" type="number" class="validate" required name="tel">
<label for="phone">手机号</label>
</div>
<div class="input-field col s6">
<input placeholder="保证不外泄:)" id="email" type="text" class="validate" required name="email">
<label for="email">邮箱</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<textarea id="introduce" class="materialize-textarea" placeholder="兴趣爱好,性格特点,技术基础,为什么想要加入社团,etc."
required name="introduce"
length="100"></textarea>
<label for="introduce">简单介绍一下自己吧!</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<textarea id="description" class="materialize-textarea" placeholder="或许你可以夸一夸自己" name="description"
length="200"></textarea>
<label for="description">还有什么想说的吗</label>
</div>
</div>
<div class="row center-align">
<button class="btn-large waves-effect waves-light">提交</button>
</div>
</form>
</div>
</div>

<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js"></script>
<script>

</script>
</body>
</html>

success.jsp

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%--
Created by IntelliJ IDEA.
User: mao
Date: 17-9-9
Time: 下午4:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>报名成功</title>
<base href="/">
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="四川大学信息安全与网络攻防协会网站"/>
<meta name="keywords" content="网络攻防,信息安全,四川大学"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/css/materialize.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row card center-align" style="padding: 20px;">
<div class="row center-align">
<img class="responsive-img col s12 l4" src="https://scuisdc.org/assets/logo_dark.png">
</div>
<div class="row s6 center-align">
<img class="responsive-img" src="https://o11vcttoe.qnssl.com/qqgroup.jpg">
</div>
<div class="row center-align">
<p>欢迎加入ISDC社团群,群号码:346784599</p>
</div>
</div>
</div>

<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js"></script>
<script>

</script>
</body>
</html>

新建实体类

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@Table(name = "`application_form`")
public class ApplicationForm {
@Id
@Column(name = "`id`")
@GeneratedValue(generator = "JDBC")
private Integer id;

@Column(name = "`name`")
private String name;

@Column(name = "`openid`")
private String openid;

@Column(name = "`stu_id`")
private Long stuId;

@Column(name = "`create_date`")
private Date createDate;

@Column(name = "`description`")
private String description;

@Column(name = "`email`")
private String email;

@Column(name = "`gender`")
private String gender;

@Column(name = "`introduce`")
private String introduce;

@Column(name = "`nationality`")
private String nationality;

@Column(name = "`tel`")
private Long tel;

@Column(name = "`interview`")
private Date interview;

@Column(name = "`block`")
private Boolean block;

@Column(name = "`impression_score`")
private Double impressionScore;

@Column(name = "`integrated_score`")
private Double integratedScore;

@Column(name = "`interviewer`")
private String interviewer;

@Column(name = "`judge`")
private String judge;

@Column(name = "`pass`")
private Boolean pass;

@Column(name = "`reason`")
private String reason;

@Column(name = "`tech_score`")
private Double techScore;
//此次省略 getter and setter

新建Controller类

JoinController

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@Controller
@RequestMapping("join/{openid}")
public class JoinController {

private final JoinService joinService;

@Autowired
public JoinController(JoinService joinService) {
this.joinService = joinService;
}

@GetMapping
public String join(@PathVariable String openid) {
Optional<ApplicationForm> applicationForm = joinService.queryForm(openid);
if (applicationForm.isPresent() && applicationForm.get().getName() != null) {
return "success";//此句为返回success.jsp对应界面
}
return "index";
}

@PostMapping
public String submit(@PathVariable String openid, @RequestParam("name") String name, @RequestParam("stuId") Long stuId, @RequestParam("gender") String gender, @RequestParam("nationality") String nationality, @RequestParam("tel") Long tel, @RequestParam("email") String email, @RequestParam("introduce") String introduce, @RequestParam("description") String description) {
Optional<ApplicationForm> optional = joinService.queryForm(openid);
if (optional.isPresent() && optional.get().getName() == null) {
ApplicationForm applicationForm = optional.get();
applicationForm.setOpenid(openid);
applicationForm.setDescription(description);
applicationForm.setName(name);
applicationForm.setIntroduce(introduce);
applicationForm.setTel(tel);
applicationForm.setEmail(email);
applicationForm.setCreateDate(new Date());
applicationForm.setStuId(stuId);
applicationForm.setGender(gender);
applicationForm.setNationality(nationality);
joinService.submit(applicationForm);//service层进一步处理
}
return "success";
}
}
CATALOG
  1. 1. 前言
  2. 2. 配置application.properties
  3. 3. 加入依赖
  4. 4. 新建jsp文件
  5. 5. 新建实体类
  6. 6. 新建Controller类