Java 未将记录添加到表中
我想在表体保存一条新记录。 我使用SpringBoot和HTML。 因此,它在HTML中显示:Java 未将记录添加到表中,java,html,spring,thymeleaf,Java,Html,Spring,Thymeleaf,我想在表体保存一条新记录。 我使用SpringBoot和HTML。 因此,它在HTML中显示: <select id="bodiesList" multiple="multiple" class="selector"> <option th:each="b : ${bodies}" th:value="${b.id}" th:text="${b.name}"></option> </select> 控制器: @Contro
<select id="bodiesList" multiple="multiple" class="selector">
<option th:each="b : ${bodies}"
th:value="${b.id}"
th:text="${b.name}"></option>
</select>
控制器:
@Controller
@RequestMapping("/general")
public class OrderController {
@Autowired
@Qualifier("orderServiceImpl")
private OrderService orderService;
@GetMapping(value = "/list")
public String get(Model theModel) {
theModel.addAttribute("orders", orderService.getOrders());
return "general/index";
}
@GetMapping(value = "/showFormForAdd")
public String add(Model theModel) {
Orders orders = new Orders();
List<Body> body = orderService.getBody();
theModel.addAttribute("bodies", body);
theModel.addAttribute("order", orders);
return "general/create-orders";
}
@PostMapping(value = "/saveOrders")
public String addOrders(@ModelAttribute("order") Orders orders, @RequestParam("picture") MultipartFile file) throws IOException {
orders = orderService.uploadOrders(orders, file);
orderService.save(orders);
return "redirect:/general/list";
}
}
但不在数据库中不在我的列中没有保存任何内容
需要在我的代码中修复什么?使用th:字段将值从html页面发送到控制器。试试这样的东西-
<select th:field="*{body}" id="bodiesList" multiple="multiple" class="selector">
<option th:each="b : ${bodies}"
th:value="${b.id}"
th:text="${b.name}"></option>
</select>
然后在表单提交后在控制器上捕获此信息,与在saveOrder控制器上捕获订单相同
在这里阅读有关thymelead官方文档中th:字段的更多信息-谢谢,这很有帮助。但是现在表中的列主体显示不正确。就像在一张图片上:这怎么可能被修复?我从图片上了解到,您只需要从body对象中提取name字段的值,然后保存到数据库中,类似于-body.getName;然后保存它