如何从jsp中的单选按钮获取值并将它们传递给java servlet

时间:2015-12-31 14:56:50

标签: java jsp servlets

我将值从jsp传递到java servlet时遇到问题。 我的班级学生有以下字段:id,firstName,lastName,precenseStatus。 字段precenseStatus为空,想要在jsp中设置“Obecny”,“Nieobecny”或“Spóźniony”字符串值。 我传递给学生对象(学生)的jsp列表。

JSP:

<form action="updatePrecensesServlet" method="post">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Imię</th>
                    <th>Nazwisko</th>
                    <th>Obecność</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach var="student" items="${students}">
                    <tr>
                        <td><c:out value="${student.firstName}"  /></td>
                        <td><c:out value="${student.lastName}" /></td>
                        <td>
                            <form role="form">
                                <c:if test="${student.precenseStatus == 'Obecny'}">
                                    <label class="radio-inline">
                                        <input checked type="radio" name="${student.precenseStatus}" value="Obecny"/>Obecny
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="${student.precenseStatus}" value="Nieobecny"/>Nieobecny
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="${student.precenseStatus}" value="Spóźniony"/>Spóźniony
                                    </label>
                                </c:if>
                                <c:if test="${student.precenseStatus == 'Nieobecny'}">
                                    <label class="radio-inline">
                                        <input type="radio" name="${student.precenseStatus}" value="Obecny"/>Obecny
                                    </label>
                                    <label class="radio-inline">
                                        <input checked type="radio" name="${student.precenseStatus}" value="Nieobecny"/>Nieobecny
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="${student.precenseStatus}" value="Spóźniony"/>Spóźniony
                                    </label>
                                </c:if>
                                <c:if test="${student.precenseStatus == 'Spó?niony'}">
                                    <label class="radio-inline">
                                        <input type="radio" name="${student.precenseStatus}" value="Obecny"/>Obecny
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="${student.precenseStatus}" value="Nieobecny"/>Nieobecny
                                    </label>
                                    <label class="radio-inline">
                                        <input checked type="radio" name="${student.precenseStatus}" value="Spóźniony"/>Spóźniony
                                    </label>
                                </c:if>
                                <c:if test="${student.precenseStatus != 'Obecny' &&
                                              student.precenseStatus != 'Nieobecny' && 
                                              student.precenseStatus != 'Spó?niony'}">
                                      <label class="radio-inline">
                                          <input type="radio" name="${student.precenseStatus}" value="Obecny"/>Obecny
                                      </label>
                                      <label class="radio-inline">
                                          <input type="radio" name="${student.precenseStatus}" value="Nieobecny"/>Nieobecny
                                      </label>
                                      <label class="radio-inline">
                                          <input type="radio" name="${student.precenseStatus}" value="Spóźniony"/>Spóźniony
                                      </label>
                                </c:if>
                            </form>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
        <button name="students" value="${student}" scope="request" type="submit" class="btn btn-success">Zatwierdź</button>
    </form>

updatePrecensesServlet doPost():

System.out.println(request.getParameterValues("students").toString());

System.out.println(request.getParameter("students").toString());

不起作用:/

Student{ID=2, firstName=YYY, lastName=XXX, precenseStatus=null}

1 个答案:

答案 0 :(得分:0)

我通过以下列表:

Student{ID=2, firstName=YYY, lastName=XXX, precenseStatus=null}
Student{ID=3, firstName=AAA, lastName=FFF, precenseStatus=null}
Student{ID=4, firstName=BBB, lastName=DDD, precenseStatus=null}
Student{ID=5, firstName=CCC, lastName=WWW, precenseStatus=null}

我希望我已经获得了设置precenseStatus字段的学生列表。 例如:

Student{ID=2, firstName=YYY, lastName=XXX, precenseStatus=Obecny}
Student{ID=3, firstName=AAA, lastName=FFF, precenseStatus=Nieobecny}
Student{ID=4, firstName=BBB, lastName=DDD, precenseStatus=Obecny}
Student{ID=5, firstName=CCC, lastName=WWW, precenseStatus=Spóźniony}

但现在仍然是:

Student{ID=2, firstName=YYY, lastName=XXX, precenseStatus=null}
Student{ID=3, firstName=AAA, lastName=FFF, precenseStatus=null}
Student{ID=4, firstName=BBB, lastName=DDD, precenseStatus=null}
Student{ID=5, firstName=CCC, lastName=WWW, precenseStatus=null}

我当然选择了jsp中的所有单选按钮。我没有把这些收音机按钮留空。