在JSP中从数组输出String [英] Output a String from an array in JSP

查看:347
本文介绍了在JSP中从数组输出String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个小测验,我希望在提交表单后输出一系列问题。

I want to make a quiz, I want to have to output an array of questions after a form is submitted.

我知道要使用一个bean我认为但是我该怎么做?

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

谢谢

推荐答案

使用< a href =http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/\"rel =nofollow noreferrer> JSTL < c:forEach> 为此。 JSTL支持取决于所讨论的servletcontainer。例如, Tomcat 不提供开箱即用的JSTL。您只需删除 jstl-1.2.jar in / WEB-INF / lib 。您可以使用 JSTL核心代码在JSP中,根据JSP文件顶部的文档声明它:

Use the JSTL <c:forEach> for this. JSTL support is dependent on the servletcontainer in question. For example Tomcat doesn't ship with JSTL out of the box. You can install JSTL by just dropping jstl-1.2.jar in /WEB-INF/lib of your webapplication. You can use the JSTL core tags in your JSP by declaring it as per its documentation in top of your JSP file:

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

您可以找到一个数组( Object [] )或列表< c:forEach>的 items 属性中标签。您可以使用 var 属性定义每个项目,以便您可以在循环内访问它:

You can locate an array (Object[]) or List in the items attribute of the <c:forEach> tag. You can define each item using the var attribute so that you can access it inside the loop:

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

这与普通Java中的以下内容基本相同:

This does basically the same as the following in plain Java:

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

这篇关于在JSP中从数组输出String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆