java - SPRING MVC,将 ENUM 插入 SQL 表并在 JSP 页面中有一个 ENUM 下拉列表
问题描述
我正在做一个基本的 CRUD 项目,有一个由公司创建的优惠券对象,优惠券对象有一个 ENUM 参数,我正在尝试做一个带有输入的 JSP 页面,以便用户(公司)可以输入所有优惠券详细信息,然后将其发送到服务器并更新 SQL 表,
这是我到目前为止的 JSP 部分,CouponType 是 ENUM,仍然不知道如何添加它:
<h1> Create Coupon </h1>
<form:form action="${pageContext.request.contextPath}/company/coupon" method="POST" modelAttribute="coupon">
title<input type="text" name="title"><br>
startDate<input type="date" name="startDate"><br>
endDate<input type="date" name="endDate"><br>
amount<input type="number" name="amount"><br>
message<input type="text" name="message"><br>
price<input type="number" name="price"><br>
image<input type="text" name="image"><br>
<input type="submit" value="add">
</form:form>
这是创建 Coupon 的 CompanyController :
@PostMapping("/coupon")
public String createNewCoupon(@ModelAttribute Coupon coupon,Model theModel) {
System.out.println
("inside createCoupon company method");
System.out.println(coupon);
coupon.setId(0);
couponService.save(coupon);
theModel.addAttribute("coupon",coupon);
System.out.println(coupon);
return "savedCoupon";
}
这是优惠券类:
@Entity
@Table(name="coupon")
public class Coupon {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private long id;
@Column(name="name")
private String title;
@Column(name="startDate")
private String startDate;
@Column(name="endDate")
private String endDate;
@Column(name="amount")
private int amount; // decrease amount on every customer purchase
@Column(name="couponType")
private CouponType type;
@Column(name="message")
private String message;
@Column(name="price")
private double price;
@Column(name="image")
private String image;
@ManyToOne
@JoinColumn(name="company_id")
private Company company;
@ManyToMany
private List<Customer> customer;
public Coupon() {
}
public Coupon(long id, String title, String startDate, String endDate, int amount, String message,
double price, String image) {
this.id = id;
this.title = title;
this.startDate = startDate;
this.endDate = endDate;
this.amount = amount;
this.message = message;
this.price = price;
this.image = image;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public CouponType getType() {
return type;
}
public void setType(CouponType type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public String toString() {
return "Coupon [id=" + id + ", title=" + title + ", startDate=" + startDate + ", endDate=" + endDate
+ ", amount=" + amount + ", type=" + ", message=" + message + ", price=" + price + ", image="
+ image + "]";
}
}
解决方案
<h1> Create Coupon </h1>
<form:form action="${pageContext.request.contextPath}/company/coupon" method="POST" modelAttribute="coupon">
title<input type="text" name="title"><br>
startDate<input type="date" name="startDate"><br>
endDate<input type="date" name="endDate"><br>
amount<input type="number" name="amount"><br>
message<input type="text" name="message"><br>
price<input type="number" name="price"><br>
<form:select path="com.example.CouponType">
<form:options/>
</form:select>
image<input type="text" name="image"><br>
<input type="submit" value="add">
</form:form>
这是我尝试在现有<form:form>
标签中添加标签后的样子
推荐阅读
- mongodb - JanusGraph 的自定义存储后端
- kubernetes - pod 有未绑定的立即 PersistentVolumeClaims(重复 3 次)
- laravel - 刀片 Laravel 中 foreach 中的字符损坏
- javascript - 修剪用户名 Laravel + JS
- sql - 查询将数据从一个表列移动到另一个表
- elasticsearch - 为什么 Elasticsearch ignore_malformed 会向索引添加格式错误的值?
- bash - 如何正确编写 bash 可执行文件
- javascript - 反应 Dropzone 以在 drop 上应用 css
- pdf - 如何使用 autocmd 将 PDF 文件转换为 TXT 文件?
- arrays - 强盗问题的动态规划子问题的解释?