1

My goal is to retrieve value from database and show in JSP.

Radio button

If the database data is Owner, the Owner radio button will be check. If the database data is Cashier, the Cashier will then be check.

Dropdown list

If the database data is Orange, then the Orange option will be selected.

Below are my codes.

Help will be appreciate. Thanks! :)

Radio button

<input type="radio" name="role" id="Owner" value="Owner" <c:if out='${staff.staffRole} == "Owner"'>checked</c:if>/>

<input type="radio" name="role" id="Cashier" value="Cashier" <c:if out='${staff.staffRole} == "Cashier"'>checked</c:if>/>

Dropdown list

<select class="form-control">
    <option>Apple</option>
    <option>Orange</option>
    <option>Durian</option>
</select>
blackpanther
  • 10,998
  • 11
  • 48
  • 78
Purple Owl
  • 137
  • 1
  • 6
  • 19
  • `${staff.staffRole} == "Owner"` [Please compare strings with equals](http://stackoverflow.com/questions/17443201/why-doesnt-work-on-string/17443215#17443215) – Suresh Atta Dec 31 '13 at 12:28
  • @sᴜʀᴇsʜᴀᴛᴛᴀ I already changed into checked .. But still cannot. – Purple Owl Dec 31 '13 at 12:47

4 Answers4

3

For Radio Buttons:

    <c:choose>
  <c:when test='${staff.staffRole == "Owner"}'>
    <input type="radio" name="role" id="Owner" value="Owner" checked >
  </c:when>
<c:otherwise>
  <input type="radio" name="role" id="Owner" value="Owner">
</c:otherwise>
</c:choose>
<c:choose>
  <c:when test='${staff.Cashier} == "Owner"}'>
    <input type="radio" name="role" id="Cashier" value="Cashier" checked >
  </c:when>
  <c:otherwise>
    <input type="radio" name="role" id="Cashier" value="Cashier" value="Owner">
  </c:otherwise>
</c:choose>

For DropDown assuming your data is in bean same bean under staffFruit

            <select class="form-control">
            <c:choose>
                  <c:when test='${staff.staffFruit == "Apple"}'>
                    <option selected>Apple</option>
                  </c:when>
                <c:otherwise>
                  <option>Apple</option>
                </c:otherwise>
            </c:choose>
            <c:choose>
                  <c:when test='${staff.staffFruit == "Orange"}'>
                    <option selected>Orange</option>
                  </c:when>
                <c:otherwise>
                  <option>Orange</option>
                </c:otherwise>
            </c:choose>
            <c:choose>
                  <c:when test='${staff.staffFruit == "Durian"}'>
                    <option selected>Durian</option>
                  </c:when>
                <c:otherwise>
                  <option>Durian</option>
                </c:otherwise>
            </c:choose>
            </select>

This is a simple if else ladder. I would recommend you to use something more convenient one like

Anil Gowda
  • 446
  • 3
  • 12
0

You can do it better using simple scriplet of jsp.

Radio-buttons:

<%
String ownerChecked = "";
String cashierChecked = "";
if(staff.staffRole.equals("Owner")){
    ownerChecked = "checked";
}else{
    cashierChecked = "checked";
}
%>

<input type="radio" name="role" id="Owner" value="Owner" <%=ownerChecked %> />
<input type="radio" name="role" id="Cashier" value="Cashier" <%=cashierChecked %> />

For Dropdown:

<select class="form-control">
    <option selected="<%=staff.staffFruit.equals("Apple") %>">Apple</option>
    <option selected="<%=staff.staffFruit.equals("Orange") %>">Orange</option>
    <option selected="<%=staff.staffFruit.equals("Durian") %>">Durian</option>
</select>

Try this and inform me if further assitance is required.

Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
0

Change

${staff.staffRole} == "Owner"

To

${staff.staffRole == "Owner"}
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
0

You can use JSTL eq operator

<c:if out="${staff.staffRole eq 'Owner'}"> ......

Or

<c:if out="${staff.staffRole == 'Owner'}"> .....
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90