我有一个来自外部系统的java变量。我需要根据收到的价值预先选择dorpdown。JSTL - 预选下拉值
我想测试下面的代码,但仍然无法正常工作。
// 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>
Thsnks for explination。我需要使这条线路正常工作。 $ {paymentType.paymentValue == myPaymentType? 'selected':''}。但它仍然不起作用。 – user3067524
请提供'paymentTypes'的java代码。我想有一些错误 –
更新了我的帖子。 Iam能够使用我的原始代码显示值和描述。只有预选的问题。 – user3067524