JSTL - 预选下拉值

时间:2014-02-11 19:54:36

标签: jsp jstl

我有一个来自外部系统的java变量。我需要根据收到的价值预先选择dorpdown。

我正在尝试测试以下代码,但仍无法正常工作。

// String myPaymentType =“I”;

//下拉的值为“”,I,R,IR

<select name="paymentType" id="paymentType" class="form-control">
       <c:forEach var="paymentType" items="${paymentTypes}">
        <option <c:if test='${paymentType.paymentValue == myPaymentType}' >selected</c:if>
          value="${paymentType.paymentValue}" >${paymentType.paymentDesc}</option>
      </c:forEach>
    </select>                                                       

// PaymentType.Java

 private String paymentValue;
private String paymentDesc;
public PaymentType(String paymentValue, String paymentDesc) {
    super();
    this.paymentValue = paymentValue;
    this.paymentDesc = paymentDesc;
}
public String getPaymentValue() {
    return paymentValue;
}
public void setPaymentValue(String paymentValue) {
    this.paymentValue = paymentValue;
}
public String getPaymentDesc() {
    return paymentDesc;
}
public void setPaymentDesc(String paymentDesc) {
    this.paymentDesc = paymentDesc;
}

// PaymentTYpes是PaymentType对象的ArraList

    PaymentType paymentType1 = new PaymentType("","Select Payment Type");
        PaymentType paymentType2 = new PaymentType("I","Initial");
        PaymentType paymentType3 = new PaymentType("R","Recurring");
        PaymentType paymentType4 = new PaymentType("IR","Intial & Recurring");
        PaymentType paymentType5 = new PaymentType("O","Onetime");

        paymentTypes.add(0,paymentType1);
        paymentTypes.add(1,paymentType2);
        paymentTypes.add(2,paymentType3);
        paymentTypes.add(3,paymentType4);
        paymentTypes.add(4,paymentType5);

// HTML查看源

    <select name="paymentType" id="paymentType" class="form-control">                       <option  value="" >Select Payment Type</option>
       <option  value="I" >Initial</option>
        <option  value="R" >Recurring</option>
       <option  value="IR" >Intial & Recurring</option>
       <option  value="O" >Onetime</option>
      </select>

2 个答案:

答案 0 :(得分:0)

假设在java端

String myPaymentType = "I";
String paymentTypes = "-;I;R;IR";
在JSP中

以下应该可以正常工作(好吧,它也将被转换为java)

<select name="paymentType">
   <c:forEach var="pt" items="${paymentTypes}">
    <option ${pt == myPaymentType ? 'selected' : ''} >${pt}</option>
   </c:forEach>
</select>

如果您需要单独的数据和显示值,则需要提供一对数组。 像

这样的东西
 class Pair {
    private String data;
    private String display;
    public Pair (String pDisplay, String pData) {
       data = pData;
       display = pDisplay;
    }
    public String getData () {
       return data;
    }
    public String getDisplay () {
       return display;
    }
 }

 Pair[] paymentTypes = new Pair [] {new Pair ("one", "1"), new Pair ("two", "2")};

然后你可以做

<select name="paymentType">
   <c:forEach var="pt" items="${paymentTypes}">
    <option ${pt.data == myPaymentType ? 'selected' : ''} >${pt.display}</option>
   </c:forEach>
</select>

(免责声明,没有IDE编写,可能有错误) 该提案不是唯一可行的解​​决方案。

答案 1 :(得分:0)

 //It should be 
   request.setAttribute("myPaymentType","I");

 //Not
   String myPaymentType = "I";

我还有一个问题,我们如何使用jstl验证变量?我们必须只在请求对象中有变量吗?