PrivateWen


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

学习springboot过程中的心得

发表于 2021-04-10 | 更新于: 2023-02-06

springboot与servlet两者封装对象的方式

springboot的Controller层当中

1
2
3
4
5
6
@PostMapping("/add")
public String addSuccess(Employee employee){
System.out.println("add-->"+employee);
employeeDao.addNewEmployee(employee);
return "redirect:/emp";
}

addSuccess方法中使用Employee对象作为入参,在前端html代码的form表单中

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
<form th:action="@{/add}" method="post">
<div class="form-group">
<label class="col-sm-2 control-label">名字</label>
<div class="col-sm-10">
<input type="text" class="form-control" placeholder="请输入名字" name="name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">邮箱</label>
<div class="col-sm-10">
<input type="text" class="form-control" placeholder="请输入邮箱" name="email">
</div>
</div>
<div class="form-group">
<label>部门</label>
<!-- 因为department是一个对象,传参的时候不能传一个对象,故先传对象的id值,然后后端接受了id值后,通过id值来查询department,并将查询到的department赋值给employee -->
<select class="form-control" name="department.id">
<option th:text="${department.getDepartmentName()}" th:each="department:${departments}" th:value="${department.getId()}"></option>
</select>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">日期</label>
<div class="col-sm-10">
<input type="text" class="form-control" placeholder="请输入日期" name="birth">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">添加</button>
</div>
</div>
</form>

阅读全文 »

springboot

发表于 2021-03-30 | 更新于: 2023-02-06

springboot的两个注解形式给属性赋值

类型 @ConfigurationProperties @Value
注值方式 批量注入 单个
松散语法 √ ×
SpEL √ √
JSR303数据校验 √ ×
注入复杂类型 √ ×

松散语法即将驼峰式命名的属性拆解为-: public int HomeAddress->home-address
SpEL即: @Value("${student.username})
JSR303即: 特殊的属性例如 电子邮件有专门的注解形式,若违反了该规定,则报错 @Email
复杂类型:例如对象类型

<1…101112…15>

29 日志
2 分类
11 标签
GitHub Gitee LeetCode
© 2025 Pvtwen
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.3