gpt4 book ai didi

java - SPRING MVC,将ENUM插入SQL表并在JSP页面中有ENUM的下拉列表

转载 作者:行者123 更新时间:2023-11-29 15:55:06 24 4
gpt4 key购买 nike

我正在开发一个基本的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>

这是创建优惠券的 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> 中添加标签后的样子标签

关于java - SPRING MVC,将ENUM插入SQL表并在JSP页面中有ENUM的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56530489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com