我想使用 jsp 文件中的函数打印 3 个按钮。正确的方法是什么,因为我的方法似乎是错误的。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Page</title>
<%!
int j = 3;
%>
<%!
public void manyButtons() {
for (int i = 0; i < j; i++) {
%>
<input type="button" value="button<%=i%>"/>
<%!
}
}
%>
</head
>
<body>
<% manyButtons(); %>
</body>
</html>
最佳答案
正如评论所说,您希望避免使用 Java 来实现此目的。值得庆幸的是,您可以使用 JSTL 轻松做到这一点。像这样使用 forEach 循环:
<c:forEach var="i" begin="1" end="3" step="1" varStatus="status">
<input type="button" value="button${i}"/>
</c:forEach>
这应该复制您正在尝试做的事情。
关于java - 在 JSP 文件中正确使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11656043/