简体   繁体   English

如何在JSP的表中显示多个行?

[英]How can I display more than one row in a table in JSP?

This is my Servlet code: 这是我的Servlet代码:

while(rs.next()){
    int questionId = rs.getInt("question_id");
    String questions = rs.getString("questions_name");
    String option1 = rs.getString("option1");
    String option2 = rs.getString("option2");
    String option3 = rs.getString("option3");
    String option4 = rs.getString("option4");
    String correctAns = rs.getString("correct_ans");

    request.setAttribute("questionId", questionId);
    request.setAttribute("questions", questions);
    request.setAttribute("option1", option1);
    request.setAttribute("option2", option2);
    request.setAttribute("option3", option3);
    request.setAttribute("option4", option4);
    request.setAttribute("correctAns", correctAns);
}

This is my DAO code: 这是我的DAO代码:

 public ResultSet StartTest(Test passData) throws SQLException{
        Statement myStatement = getConnection();
        String query;
        query = "SELECT question_id, questions_name, option1, option2, option3, option4, correct_ans "
                + "FROM question WHERE courses_codes = '"+passData.getCourseCode()+"'"
                + "ORDER BY RAND()"
                + "LIMIT 5";
        rs = myStatement.executeQuery(query);
        return rs;
    }

JSP code: JSP代码:

<table id="TTquestionDiv">
    <tr>
        <td>${questions}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${option1}" />${option1}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${option2}" />${option2}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${option3}" />${option3}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${option4}" />${option4}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${correctAns}" />${correctAns}${b}</td>
    </tr>
</table>

Use this code in your servlet 在servlet中使用此代码

HttpSession session=request.getSession();  
Arraylist<Person>listPerson=daoPerson.getLisPersons();
        session.setAttribute("lsperson",listPerson);  

Import jstl tag library in jsp page 在jsp页面中导入jstl标签库

 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

and put this code 并把这段代码

   <c:forEach items="${lsperson}" var="person">
        <tr>      
            <td>${person.name}</td>
            <td>${person.lastName}</td>
            <td>${person.age}</td>
        </tr>
    </c:forEach>

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

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