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

时间:2019-06-10 16:55:56

标签: java mysql spring spring-mvc jsp

嘿,我正在做一个基本的CRUD项目,有一个公司创建的Coupon对象,该Coupon对象有一个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 ammount on ever 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 + "]";
}

}

2 个答案:

答案 0 :(得分:0)

怎么样

<select name="coupon-type"> 
    <option value="">Please Choose</option> 
    <% for(int i = 0;i < CouponType.values().length; i++){ %> 
        <option value="<%= CouponType.values()[i] %>" > <%=CouponType.values()[i]%> </option>
    <%}%> 
</select>

或Spring MVC方式:

<form:select path="com.example.CouponType">
   <form:options/>
</form:select>

或与Thymeleaf:

<select>
    <option th:each="value : ${T(com.example.demo.ExampleEnum).values()}"
            th:value="${value}"
            th:text="${value}">
    </option>
</select>

答案 1 :(得分:0)

<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>标签

中后的样子