【发布时间】:2020-07-02 16:13:12
【问题描述】:
为了运行应用程序,我在 war 中使用了 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">DateTime</label>
<input id="dateTime" type="date" 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>
但我得到一个错误:
日志错误:
字段“datetime”上的对象“mealsCreate”中的字段错误:被拒绝 价值[2020-04-29];代码 [typeMismatch.mealsCreate.datetime,typeMismatch.datetime,typeMismatch.java.time.LocalDate,typeMismatch]; 论据 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [mealsCreate.datetime,datetime];论据 [];默认消息 [约会时间]];默认消息[无法转换类型的属性值 'java.lang.String' 到属性所需的类型'java.time.LocalDate' '约会时间';嵌套异常是 java.lang.IllegalStateException: 无法将类型“java.lang.String”的值转换为所需类型 属性“日期时间”的“java.time.LocalDate”:没有匹配的编辑器或 找到转换策略]] 22:16:59.659 [localhost-startStop-2] INFO o.s.o.j.LocalContainerEntityManagerFactoryBean - 关闭 JPA 持久性单元“默认”的 EntityManagerFactory
我的模特:
@Entity
@Table(name="meals")
public class Meal {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "date_time")
private LocalDate 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 LocalDate getDatetime() {
return datetime;
}
public void setDatetime(LocalDate 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 date,
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";
}
}
我的代码有什么问题?
【问题讨论】:
-
当你从 JSP 传递值时,它被认为是控制器保存方法中的字符串值,你需要将字符串转换为日期格式。
-
具体怎么做?
标签: java html datetime jsp spring-data-jpa