Print string from array in JSP

I want to make a quiz, I want to output an array of questions after submitting the form.

I know to use a bean, I think, but how would I do it?

thank

+3
source share
2 answers

Use JSTL <c:forEach> for this. JSTL support depends on the appropriate servlet container. For example, Tomcat does not come with JSTL out of the box. You can install JSTL by simply dropping jstl-1.2.jar in /WEB-INF/libyour web application. You can use the main JSTL tags in your JSP by declaring it according to its documentation at the top of your JSP file:

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

(Object[]) List items <c:forEach>. var, :

<c:forEach items="${questions}" var="question">
    <p>Question: ${question}</p>
</c:forEach>

, Java:

for (String question : questions) { // Assuming questions is a String[].
    System.out.println("<p>Question: " + question + "</p>");
}
+2

JSP 2.0 :

<% 
request.setAttribute( "questions", new String[]{"one","two","three"} );  
%>   
<c:forEach var="question" items="${questions}" varStatus="loop">  
    [${loop.index}]: ${question}<br/>  
</c:forEach>  

, submit JSP.

JSP 1.2:

<c:forEach var="question" items="${questions}" varStatus="loop">  
    <c:out value="[${loop.index}]" />: <c:out value="${question}"/><br/>  
</c:forEach>  

EL JSTL, Question, , :

${question.myProperty}
+2

Source: https://habr.com/ru/post/1743258/


All Articles