HTML + Java |从输入type =“ time”接收0

时间:2018-07-19 09:45:43

标签: java html spring input thymeleaf

我正在尝试制作一个webApp,您应该在其中选择起床的时间。我正在使用Spring,Thymeleaf作为我的Java和html之间的连接器。但是无论我选择什么时间,它都会返回0。

我的StartDay班:

/* ----------------------------
|        StartDay Class       |
------------------------------ */

@Entity
@Table(name = "start_day")
public class StartDay {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "start_day_id")
private int startDayId;
@Column(name = "age_in_days", nullable = false)
private int ageInDays;
@Column(name = "day_created", nullable = false)
private String dayCreated;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "dream_remembered", nullable = false)
private boolean dreamRemembered;
@Column(name = "nightmare", nullable = false)
private boolean nightmare;
@Column(name = "waking_time", nullable = false)
private int wakingTime;
@Column(name = "good_night_of_sleep", nullable = false)
private boolean goodNightOfSleep;



/* ------------------------------
|        Getter & Setter        |
------------------------------ */

public String getDayCreated() {
    return dayCreated;
}
public void setDayCreated(String dayCreated) {
    this.dayCreated = dayCreated;
}

public int getAgeInDays() {
    return ageInDays;
}
public void setAgeInDays(int ageInDays) {
    this.ageInDays = ageInDays;
}

public boolean isDreamRemembered() {
    return dreamRemembered;
}
public void setDreamRemembered(boolean dreamRemembered) {
    this.dreamRemembered = dreamRemembered;
}

public boolean isNightmare() {
    return nightmare;
}
public void setNightmare(boolean nightmare) {
    this.nightmare = nightmare;
}

public int getWakingTime() {
    return wakingTime;
}
public void setWakingTime(int wakingTime) {
    this.wakingTime = wakingTime;
}

public boolean isGoodNightOfSleep() {
    return goodNightOfSleep;
}
public void setGoodNightOfSleep(boolean goodNightOfSleep) {
    this.goodNightOfSleep = goodNightOfSleep;
}

public String getUsername() {
    return username;
}
public void setUsername(String userId) {
    this.username = userId;
}

/* ------------------------------
|          CONSTRUCTOR          |
------------------------------ */

public StartDay(int ageInDays, String dayCreated, String username, boolean dreamRemembered, boolean nightmare, int wakingTime, boolean goodNightOfSleep) {
    this.ageInDays = ageInDays;
    this.dayCreated = dayCreated;
    this.username = username;
    this.dreamRemembered = dreamRemembered;
    this.nightmare = nightmare;
    this.wakingTime = wakingTime;
    this.goodNightOfSleep = goodNightOfSleep;
}

public StartDay() {
}

}

我的start-dag.html页面:

<form th:action="@{/normal/start-dag}" th:object="${startDay}" method="post">
    <div>
        <p>Hvad tid stod du op?</p>
        <input type="time" name="wakingTime">

        <p>Har du sovet godt?</p>
        <!--<input type="checkbox" name="goodNightOfSleep"><span>Ja</span>-->

        <p>Kan du huske hvad du drømte?</p>
        <input type="checkbox" name="dreamRemembered"><span>Ja</span>

        <p>Var det et mareridt?</p>
        <input type="checkbox" name="nightmare"><span>Ja</span>

        <input type="hidden" name="username" />
        <input type="hidden" name="ageInDays" />
        <input type="hidden" name="dayCreated" />

        <input type="submit" value="Start dag">
    </div>
</form>

任何帮助/建议都值得赞赏!

更新#1 –添加了更多代码

我的StartDayController

// Start\create day
    @GetMapping("/normal/start-dag")
    public  String opretGoal(Model model){
        StartDay startDay = new StartDay();
        model.addAttribute("startDay", startDay);
        return "normal/start-dag";
    }
    @PostMapping("/normal/start-dag")
    public String opretGoal(@ModelAttribute StartDay startDay, BindingResult bindingResult){

        startDayService.createGoalOfTheDay(startDay);

        return "redirect:/normal/menu";
    }

Test on frontEnd

1 个答案:

答案 0 :(得分:2)

@Column(name = "waking_time", nullable = false)
private int wakingTime;

您正在发送08:00作为输入,并接受int值。您可以像

一样接受
@DateTimeFormat
@Column(name = "waking_time", nullable = false)
private Date wakingTime;

并将其转换为所需格式。

,或者如果您想接受时间为08:00。接受它作为字符串。

@Column(name = "waking_time", nullable = false)
private String wakingTime;