当前位置: 首页 > 知识库问答 >
问题:

从表单提交时,DateTimeFormat批注不适用于“hh:mm”

石喜
2023-03-14

所以首先,当我使用@DateTimeFormat(模式="HH: mm")时,这段代码是有效的。但是一旦我将其更改为@DateTimeFormat(模式="hh: mm")它就停止工作。我是新来的,所以我不确定问题会从哪里来。我想做的就是为我的数据库节省时间。

错误是:

Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalTime] for value '12:00';

这是我映射到表单的对象:

@Entity
@Table(name = "Schedule")
@NoArgsConstructor
@Getter
@Setter
public class Schedule {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @DateTimeFormat(pattern = "hh:mm")
    private LocalTime startTime;
    @DateTimeFormat(pattern = "hh:mm")
    private LocalTime endTime;
    @Column(name = "days")
    private String days;

    public Schedule(LocalTime startTime, LocalTime endTime, String days) {
        this.startTime = startTime;
        this.endTime = endTime;
        this.days = days;
    }
}

这是我的控制器:

@Controller
@RequestMapping("/schedules")
@AllArgsConstructor
public class WebScheduleController {
    
    ScheduleService scheduleService;

    @GetMapping
    public String getSchedules(Model model) {
        List<Schedule> schedules = scheduleService.getSchedules();
        model.addAttribute("scheduleList", schedules);
        model.addAttribute("schedule", new Schedule());
        return "schedules";
    }

    @PostMapping
    public String createSchedule(@ModelAttribute("schedule") Schedule schedule,Model model ){

        scheduleService.createSchedule(schedule);

        return "redirect:/schedules/";
    }
    

}

这是超文本标记语言形式:

<form method="post" th:action="@{/schedules}" th:object="${schedule}" class="col card p-3 mb-5">
    <div class="form-group">
        <label for="startTime">Start time</label>
        <input id="startTime" placeholder="Enter start time" required type="time" th:field="*{startTime}"/>
    </div>
    <div class="form-group">
        <label for="endTime">End time</label>
        <input id="endTime" placeholder="Enter end time" required type="time" th:field="*{endTime}" />
    </div>
    <div class="form-group">
        <label for="days">Days</label>
        <input id="days" placeholder="Enter days" required type="text" th:field="*{days}" />
    </div>
    <input type="submit" class="btn btn-primary" value="Create Schedule">
</form>

共有1个答案

松霖
2023-03-14

您是否尝试删除am/pm?也许这个字符串am/pm也试图在发送数据时成为时间的一部分。数据库不允许在日期中使用字符串。

 类似资料:
  • 问题内容: 从Spring Boot 2.2.7切换到Spring Boot 2.3.1后,针对我的特定情况,在提交表单时进行实体转换不再起作用 Category.java SearchForm.java: HTML形式: Controller.java: 以前使用Spring Boot 2.2.7提交表单时,会将HTML表单中的“ c”(选择保留类别ID)转换为SearchForm.java中的

  • 从spring boot 2.2.7切换到spring boot 2.3.1后,针对我的特定案例提交表单时的实体转换不再有效 category.java searchForm.java: HTML表单: controller.java: 以前在spring boot 2.2.7form submit将“C”从HTML表单(select持有类别的ID)转换为SearchForm.java中的类别实体

  • 问题内容: 我想@WebServlet在运行于Tomcat 8的Java EE Web应用程序中使用注释。 我已经读到我需要在自己的服务器中声明Servlet版本3.1,web.xml并且我的Servlet需要扩展HttpServlet。我做了所有这些,但仍然@WebServlet行不通。我正在获取HTTP 404。 我也试过我的配置用metadata-complete=”false”在我web.

  • 下面是MultistepForm的代码。我已经实现了一个多步骤表单功能,所以这里一切都很好,只是问题是在这个表单中,我在最后一步有一个预览页面,所以当我点击提交按钮时,表单正在消失,按钮正在顶部移动。所以如果有人能帮我请。

  • 我在一个基于Spring的Web应用程序(版本4.1.6。RELEASE,Spring Security 4.0.0。RELEASE)中工作,我收到错误(POST方法)。根据Spring的留档“Spring Security自动为任何

  • 获取Form变量 通过Action的如下方法可以获取变量: GetSlice GetString GetInt GetBool GetFloat GetFile GetForm 自动映射 通常我们通过http.Request.Form来获得从用户中请求到服务器端的数据,这些数据一般都是采用name,value的形式提交的。xweb默认支持自动将这些变量映射到Action的成员中,并且这种映射支持子