简体   繁体   English

如何从jsp中的单选按钮获取值并将其传递给Java servlet

[英]How get values from radio button in jsp and pass them to java servlet

I have problem with passing values from jsp to java servlet. 我在将值从jsp传递到Java servlet时遇到问题。 My class Student has follow fields: id, firstName, lastName, precenseStatus. 我班的学生有以下字段:id,firstName,lastName,precenseStatus。 Field precenseStatus is empty and a want to set "Obecny", "Nieobecny" or "Spóźniony" string value at it in jsp. 字段precenseStatus为空,并且要在jsp中为其设置“ Obecny”,“ Nieobecny”或“Spóźniony”字符串值。 I pass to jsp list of Student object (students). 我将jsp列表传递给Student对象(学生)。

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(): updatePrecensesServlet doPost():

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

or 要么

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

Not works :/ 不起作用:/

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

I pass follow list: 我通过关注列表:

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}

I expecting that I've got list of Students with set precenseStatus field. 我希望我已经设置了precenseStatus字段的学生列表。 for example: 例如:

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}

But now is still: 但是现在仍然是:

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}

I've selected all radio buttons in jsp of course. 我已经选择了jsp中的所有单选按钮。 I don't left those radio buttons empty. 我没有将那些单选按钮留空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM