0
点赞
收藏
分享

微信扫一扫

springboot之restfulcrud--来到添加页面

芷兮离离 2022-11-30 阅读 170
mvchtmljava


<h2><a  href="#" th:href="@{/emp}" class="btn btn-success">添加按钮</a></h2>

a标签默认get方式

在EmployeeController中添加

//来到员工添加页面
@GetMapping("/emp")
public String toAddPage(){
//来到添加页面
return "emp/add";
}

springboot之restfulcrud--来到添加页面_java

alt+Enter

springboot之restfulcrud--来到添加页面_java_02

修改EmployeeController中的toAddPage方法

//来到员工添加页面
@GetMapping("/emp")
public String toAddPage(Model model){
//来到添加页面,查出所有的部门,在页面显示
Collection<Department> departments = departmentDao.getDepartments();
model.addAttribute("depts",departments);
return "emp/add";
}

add.html中的

<form th:action="@{/emp}" method="post">中

<div class="form-group">
<label >department</label>
<!--提交的是部门id-->
<select class="form-control" name="department.id" >
<option th:each="dept:${depts}" th:value="${dept.id}" th:text="${dept.departmentName}"></option>
</select>
</div>

value值保存好

EmployeeController添加方法

//员工添加
//springmvc自动将请求参数和入参对象的属性进行一一绑定
//要求:请求参数的名字和javabean入参的对象里面的属性名是一样的
@PostMapping("/emp")
public String addEmp(Employee employee){
//保存员工
employeeDao.save(employee);

//来到员工列表页面
//thymeleaf默认就会拼串
//classpath:/templates/返回值.html
//redirect:表示重定向到一个地址( / 代表当前项目路径),forword:表示转发到一个地址
return "redirect:/emps";
}

ctrl+N进入ThymeleafViewResolver类,找到createView方法,if (viewName.startsWith("redirect:")) {,创建

RedirectView对象,进入RedirectView类,找到renderMergedOutputModel方法,调用了

sendRedirect(request, response, targetUrl, this.http10Compatible);进入此方法,可以看到

response.sendRedirect(encodedURL);

如果else if (viewName.startsWith("forward:")) {,会创建InternalResourceView对象,找到renderMergedOutputModel方法,可以看到rd.forward(request, response);rd 对应的RequestDispatcher类package javax.servlet;就是原生的servlet转发器。

给add.html中的form input中添加name属性,对应javabean中的属性

string-》date  日期的格式化,默认日期是按照 / 的方式

进入到WebMvcAutoConfiguration类

@Bean
@Override
public FormattingConversionService mvcConversionService() {
WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat());
addFormatters(conversionService);
return conversionService;
}

进入getDateFormat方法中,WebMvcProperties类中显示

/**
* Date format to use. For instance, `dd/MM/yyyy`.
*/
private String dateFormat;

修改application.properties配置文件

spring.mvc.date-format=yyyy-MM-dd

举报

相关推荐

0 条评论