首页 > 解决方案 > Thymeleaf 和 Springboot th:field Null

问题描述

我有一个 MVC 网页,我尝试从输入type="datetime-local"获取一个值并将该值保存在th:field 中。能够保存这些日期并使用它们

但它似乎只在input="text"标签中工作,而不是 type="datetime-local" 因为当我调试时,输入的日期值是type="datetime-local"

我需要客户选择要开始的事件的日期

这是整个表单和保存日期的两个输入的示例。

客户端视图 [在此处输入图像描述][1]

看法

<div class="container">
    <div class="row">
        <div class="col-md-4 mt-5">
            <form th:action="@{/guardar/} + ${usuario.id_user}" method="post" th:object="${event}">
                <div class="form-group">
                    <label for="title" >Titulo </label>
                    <input type="text" id="title" placeholder="Titulo" class="form-control" th:field="*{title}"/>
                </div>
                <div class="form-group">
                    <label for="e_description" >Descripción</label>
                    <input type='text' name="e_description" th:field="*{e_description}"  placeholder="Descripcion" class="form-control"/>
                </div>
                <div class="form-group">
                    <label for="startDate">Inicio de Evento:</label>
                    <input type="text" id="time" placeholder="Inicio de Evento" class="form-control" th:field="*{startDate}"/>
                </div>
                <div class="form-group">
                    <label for="endDate">Termino de Evento:</label>
                    <div class="input-group date" id="endDate">
                        <input type="datetime-local" id="time2"  placeholder="Termino de Evento" class="form-control" th:field="*{endDate}"/>
                        <div class="input-group-addon input-group-append">
                            <div class="input-group-text">
                                <i class="fa fa-clock-o"></i>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="className" >ClassName</label>
                    <input type="text" name="className" th:field="*{className}" placeholder="className" class="form-control"/>
                </div>
                <div class="form-group">
                    <label for="icon">Icono</label>
                    <input type="text" name="icon" th:field="*{icon}" placeholder="icon" class="form-control" />
                </div>
                <div class="form-group">
                    <label for="allDay">¿Todo el día?</label>
                    <input type="checkbox" value="true" name="allDay" th:field="*{allDay}" />
                </div>
                <button class="btn btn-primary" type="submit">Submit form</button>
            </form>
        </div>
    </div>
</div>

/guardar/} 的控制器 + ${usuario.id_user} @PostMapping("/guardar/{id}")

    public String guardar(@Valid Event event, Errors errores,@PathVariable int id,Model model) throws ParseException {
       /* if (errores.hasErrors()) {
            return "agregar";
        }*/
        UserPlanD userPlanD = userService.encontrarUsuarioPorId(id);
        model.addAttribute("usuario",userPlanD);
        event.setUser(userPlanD);
       Date inicio_evento = formatter.parse(event.getStartDate().toString());
       String termino_evento = event.getStartDate().toString();
        event.setStartDate(inicio_evento);
        List<Event> eventicos = Arrays.asList(event);
        System.out.println("Evento" + event + "y usuario: " + userPlanD);
        eventService.guardar(event);
        /*userPlanD.setEvents(eventicos);
        userService.guardar(userPlanD);*/

        return "redirect:/eventos";
    } 

模型是下一个:


@Table(name="event_calendar")
public class Event implements Serializable {

 //private static final long serialVersionUID = 1L;

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "id_event")
   private Long id_event;


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

    @NotEmpty
   private String e_description;

    @NotEmpty
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSSSSS")
    @Column(columnDefinition = "DATETIME")
    private Date startDate;

    @NotEmpty
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSSSSS")
    @Column(columnDefinition = "DATETIME")
     private Date endDate;

    @NotEmpty

    private String className;

    @NotEmpty
    private String icon;

    @NotEmpty
    private String allDay;

    @ManyToOne
    @JoinColumn(name = "id_user")
    private UserPlanD user; ```



Here its a debug result of the controller result in the line the got the next code  "System.out.println("Evento" + event + "y usuario: " + userPlanD)"





  [1]: https://i.stack.imgur.com/QOYfW.png

标签: javahtmljpathymeleaf

解决方案


推荐阅读