HTML的from怎么把user对象信息提交给controller,用的是springboot和mybatis-plus框架
时间: 2023-06-02 21:05:26 浏览: 70
可以使用SpringBoot中的@RequestParam注解从表单中获取用户对象信息,并将信息传递给Controller。示例代码如下:
在Controller中定义一个用于处理表单提交请求的方法:
```java
@PostMapping("/submitUser")
public String submitUser(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("email") String email,
Model model) {
// 将获取到的表单数据封装成User对象
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
// 调用Service层进行保存操作
userService.save(user);
// 设置响应数据
model.addAttribute("message", "User saved successfully!");
// 返回页面
return "home";
}
```
在HTML表单中设置对应的字段名和值,例如:
```html
<form action="/submitUser" method="post">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="email" name="email" />
<button type="submit">Submit</button>
</form>
```
这里假设用户对象的属性名与表单字段名相同,如果不相同则需要使用@RequestParam注解的value属性指定对应的表单字段名。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)