【问题标题】:SPRING MVC, inserting ENUM into SQL table & having a drop-down list of ENUM in JSP pageSPRING MVC,将 ENUM 插入 SQL 表并在 JSP 页面中有一个 ENUM 下拉列表
【发布时间】:2019-10-25 02:18:22
【问题描述】:

我正在做一个基本的 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 + "]";
}

}

【问题讨论】:

    标签: java mysql spring spring-mvc jsp


    【解决方案1】:
    <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>
    

    这是我尝试在现有的&lt;form:form&gt; 标记中添加标记后的样子

    【讨论】:

      【解决方案2】:

      怎么样

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

      或与百里香:

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

      【讨论】:

      • 创建优惠券

        标题
        startDate
        endDate
        金额
        message
        price
        图片
      • 你在spring mvc方案中用这个路径试过了吗?${pageContext.request.contextPath}/company/coupontype
      • Florian Kiling,是的,但是路径是控制器中的映射,即“/coupon”,此方法应该创建一个优惠券,使用 ${pageContext.request.contextPath}/company/coupontype我需要使用映射 /coupontype 创建另一种方法,然后呢?我将在控制器中有两个映射,每个映射做不同的事情,我需要这两件事同时发生:\
      • 也许不可能?
      • 我刚刚创建了一个 Spring MVC 项目并使用了 Thymeleaf。可以显示一个枚举,我更新答案。 github.com/FloKling/spring-mvc-enum
      猜你喜欢
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 2018-02-07
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多