首页 > 解决方案 > 如何使用弹簧在表格中显示日期?

问题描述

为了运行应用程序,我在战争中使用了 tomcat 8.5.50 包。我使用弹簧 5.2 版本。在所有操作中我都使用 JpaRepository。我没有在我的代码中使用日期时间转换器 - 也许这是一个错误?我想使用 jsp 在我的表中添加一个日期:

   <div id="row">
        <%--@elvariable id="mealsCreate" type=""--%>
        <form:form action="${pageContext.request.contextPath}/create"
                   modelAttribute="mealsCreate" method="post">
            <div class="col-md-9">
                <input type="hidden"/>

                <div class="form-group">
                    <div class="col-md-12">
                        <label for="datetime" type="table" class="table">DateTime</label>
                        <input id="datetime" type="datetime-local" name="datetime"/>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-12">
                        <label for="description" type="table" class="table">Description</label>
                        <input id="description" type="text" name="description"/>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-12">
                        <label for="calories" type="table" class="table">Calories</label>
                        <input id="calories" type="number" name="calories"/>
                    </div>
                </div>

                <button type="submit" class="btn btn-default" name="saveMeals">Save</button>
                <button type="button" class="btn btn-default" name="cancelMeals" onclick="window.history.back()">Cancel</button>

            </div>
        </form:form>
    </div>

但没有任何东西粘贴到我的表中:

在此处输入图像描述

我的模型:

@Entity
public class Model {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @Column(name = "date_time", columnDefinition = "timestamp default new()")
    private LocalDateTime dateTime;

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

    @Column(name = "calories")
    private int calories;

    public Meal() {
    }

    public Meal(int id) {
        this.id = id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public LocalDateTime getDateTime() {
        return dateTime;
    }

    public void setDateTime(LocalDateTime dateTime) {
        this.dateTime = dateTime;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getCalories() {
        return calories;
    }

    public void setCalories(int calories) {
        this.calories = calories;
    }
}

也没有将任何内容加载到数据库中。我的sql查询:

CREATE TABLE meals
(
  id integer NOT NULL,
  date_time timestamp without time zone,
  description text NOT NULL,
  calories integer NOT NULL,
  CONSTRAINT meals_pkey PRIMARY KEY (id)
);

控制器:

@Controller
public class Controller {
    @Autowired
    MealService mealService;

    @GetMapping(value = "/list")
    public String getAll(Model model) {
        model.addAttribute("meals", mealService.getAll());

        return "meals";
    }

    @GetMapping(value = "/createForm")
    public String addForm(Model model) {
        Meal meal = new Meal();
        model.addAttribute("mealsCreate", meal);

        return "createmealForm";
    }

    @PostMapping(value = "/create")
    public String save(@ModelAttribute("mealsCreate") Meal meal) {
        mealService.save(meal);
        return "redirect:/list";
    }
}

我的代码有什么问题?

标签: javahtmlsqlspringjsp

解决方案


推荐阅读