添加员工信息
1. 将添加员工信息改为超链接
<!--添加员工按钮-->
<h2><a class="btn btn-sm btn-success" th:href="@{/toAddPage}">增加员工</a></h2>
2.编写对应的controller
//to员工添加页面
@GetMapping("/emp")
public String toAddPage(){
return "emp/add";
}
3.添加前端页面;复制list页面,修改即可
bootstrap官网文档 : https://v4.bootcss.com/docs/4.0/components/forms/ , 我们去可以里面找自己喜欢的样式!
<!--新增员工的表单 -->
<form th:action="@{/addEmp}" method="post">
<div class="form-group">
<label>LastName</label>
<input name="lastName" type="text" class="form-control" placeholder="kuangshen">
</div>
<div class="form-group">
<label>Email</label>
<input name="email" type="email" class="form-control" placeholder="24736743@qq.com">
</div>
<div class="form-group">
<label>Gender</label><br/>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="1">
<label class="form-check-label">男</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="0">
<label class="form-check-label">女</label>
</div>
</div>
<!--部门,应该从后台查询显示,用户选择-->
<div class="form-group">
<label>department</label>
<select class="form-control" name="department.id">
<!--each 遍历这个标签-->
<option th:each="dept:${department}"th:text="${dept.departmentName}" th:value="${dept.id}">1</option>
</select>
</div>
<div class="form-group">
<label>Birth</label>
<input name="birth" type="text" class="form-control" placeholder="kuangstudy">
</div>
<button type="submit" class="btn btn-primary">添加</button>
</form>
4.部门信息下拉框应该选择的是我们提供的数据,所以我们要修改一下前端和后端
EmpController
//toAddPage
@RequestMapping("/toAddPage")
public String toAddPage(Model model){
Collection<Department> department= departmentDao.getDepartments();
model.addAttribute("department" ,department);
return "emp/add";
}
前端页面
<!--部门,应该从后台查询显示,用户选择-->
<div class="form-group">
<label>department</label>
<select class="form-control" name="department.id">
<!--each 遍历这个标签-->
<option th:each="dept:${department}"th:text="${dept.departmentName}" th:value="${dept.id}">1</option>
</select>
</div>
1.修改add页面form表单提交地址和方式
<form th:action="@{/emp}" method="post">
2.编写controller;
//addEmp
@RequestMapping("/addEmp")
public String addEmp(Employee employee){
System.out.println("测试信息:"+employee);
//保存到数据库
employeeDao.save(employee);
//回到员工列表,转发,重定向
return "redirect:/emplist";
}
重定向和转发 以及 /的问题?
原理探究 : ThymeleafViewResolver
public static final String REDIRECT_URL_PREFIX = "redirect:";
public static final String FORWARD_URL_PREFIX = "forward:";
protected View createView(String viewName, Locale locale) throws Exception {
if (!this.alwaysProcessRedirectAndForward && !this.canHandle(viewName, locale)) {
vrlogger.trace("[THYMELEAF] View \"{}\" cannot be handled by ThymeleafViewResolver. Passing on to the next resolver in the chain.", viewName);
return null;
} else {
String forwardUrl;
if (viewName.startsWith("redirect:")) {
vrlogger.trace("[THYMELEAF] View \"{}\" is a redirect, and will not be handled directly by ThymeleafViewResolver.", viewName);
forwardUrl = viewName.substring("redirect:".length(), viewName.length());
RedirectView view = new RedirectView(forwardUrl, this.isRedirectContextRelative(), this.isRedirectHttp10Compatible());
return (View)this.getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName);
} else if (viewName.startsWith("forward:")) {
vrlogger.trace("[THYMELEAF] View \"{}\" is a forward, and will not be handled directly by ThymeleafViewResolver.", viewName);
forwardUrl = viewName.substring("forward:".length(), viewName.length());
return new InternalResourceView(forwardUrl);
} else if (this.alwaysProcessRedirectAndForward && !this.canHandle(viewName, locale)) {
vrlogger.trace("[THYMELEAF] View \"{}\" cannot be handled by ThymeleafViewResolver. Passing on to the next resolver in the chain.", viewName);
return null;
} else {
vrlogger.trace("[THYMELEAF] View {} will be handled by ThymeleafViewResolver and a {} instance will be created for it", viewName, this.getViewClass().getSimpleName());
return this.loadView(viewName, locale);
}
}
}
我们要接收前端传过来的属性,将它封装成为对象!首先需要将前端页面空间的name属性编写完毕!
编写controller接收调试打印
//addEmp
@RequestMapping("/addEmp")
public String addEmp(Employee employee){
System.out.println("测试信息:"+employee);
//保存到数据库
employeeDao.save(employee);
//回到员工列表,转发,重定向
return "redirect:/emplist";
}
此时的前端页面如下图所示:
注册完成后,页面跳转显示正常,但是时间格式为
当提交为下图是就会报400错
设置时间格式,使其可以为默认的格式:
在webmvc的自动配置文件;找到一个日期格式化的方法,我们可以看一下
@Bean
public FormattingConversionService mvcConversionService() {
WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat());
this.addFormatters(conversionService);
return conversionService;
}
调用了 getDateFormat 方法;
public String getDateFormat() {
return this.dateFormat;
}
这个在配置类中,所以我们可以自定义的去修改这个时间格式化问题,我们在我们的配置文件中修改一下;
spring.mvc.date-format=yyyy-MM-dd
这样时间格式设置就完成了