gpt4 book ai didi

java - 将日期参数从 html 表单发送到 thymeleaf 中的 Controller

转载 作者:搜寻专家 更新时间:2023-11-01 03:36:37 24 4
gpt4 key购买 nike

我在将日期传递给 Controller ​​时遇到以下问题,如果我删除日期则它可以正常工作。

我的html代码是

<div class="form-group" id="all">
<label class="col-lg-2 control-label" for="focusedInput3">Start date</label>
<div class="col-lg-10">
<input type="date" id="startDate" name="startDate" th:value="*{startDate}" />
</div>
</div>
<div class="form-group" id="all">
<label class="col-lg-2 control-label" for="focusedInput4">End date</label>
<div class="col-lg-10">
<input type="date" id="endDate" name="endDate" th:value="*{endDate}" />
</div>
</div>

我的实体类是

@Column(name = "name")
private String courseName;

@Column(name = "semister")
private String semister;

@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat (pattern="dd-MMM-YYYY")
@Column(name = "startDate")
private Date startDate;

@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat (pattern="dd-MMM-YYYY")
@Column(name = "endDate")
private Date endDate;

点击提交按钮后,出现如下错误:

enter image description here

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Apr 24 14:05:33 IST 2015
There was an unexpected error (type=Not Found, status=404).
No message available

这是我的 Controller

@Controller
@RequestMapping("/admin")
public class CourseScheduleController {

@Autowired
private CourseScheduleService courseScheduleService;

@RequestMapping("/createCourse")
public ModelAndView getAllativities() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("courseName", "");
model.put("semister", "");
model.put("startDate",null);
model.put("endDate",null);
return new ModelAndView("createCourse",model);
}

@RequestMapping("/saveCourse")
public String saveCourseSchedule(@ModelAttribute CourseBE courseBE){
courseScheduleService.saveCourseSchedule(courseBE);
return "redirect:/admin/createCourse";
}

请帮我解决这个问题。

最佳答案

也许你应该在 Controller 上包含一个 Binder :

@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");

dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String value) {
try {
setValue(new SimpleDateFormat("dd/MM/yyyy").parse(value));
} catch (ParseException e) {
setValue(null);
}
}
});

}

关于java - 将日期参数从 html 表单发送到 thymeleaf 中的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29842584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com