JSTL-预选dropwdown值 [英] JSTL - preselect dropwdown value
本文介绍了JSTL-预选dropwdown值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个来自外部系统的java变量.我需要根据接收到的值预先选择dorpdown.
I have a java variable coming from an external system. I need to preselect the dorpdown based on the value received.
我正在尝试测试以下代码,但仍无法正常工作.
I am trying to test the below code but still not working.
//String myPaymentType ="I";
//String myPaymentType = "I";
//下拉菜单的值为",I,R,IR
//Drop down has values as "",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
//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
//PaymentTYpes is an ArraList of PaymentType objects
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>
推荐答案
//It should be
request.setAttribute("myPaymentType","I");
//Not
String myPaymentType = "I";
我还有一个问题,我们如何才能使用jstl再次验证变量?我们只需要在请求对象中有变量吗?
I still have a question, how we can validate againest a variable using jstl? Do we have to have variable in only request object?
这篇关于JSTL-预选dropwdown值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文