如何在JSP代码的侧面编写TD.我写了,但结果合并了


<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<%@ page import="java.sql.*" %>
<%@ page import= "myExam.quest" %>
</head>
<body>
<%! int index=0; %> 
<%! int pos=0; %>   
<% String c,a;
String[] CA =(String[])session.getAttribute("CA");
String[] AS =(String[])session.getAttribute("AS");
List<quest> l=(List<quest>)session.getAttribute("myquestion");
quest[] question = new quest[l.size()];
l.toArray(question);
%>
<table border=1>
<tr>
<th  colspan="2" width="500">Questions</th>
<th>CorrectAnswer</th>
<th>Answer sheet</th>
</tr>
<tr>
<% 
for(pos=0;pos<question.length;pos++){
String ques=question[pos].ques;
String opa=question[pos].opa;
String opb=question[pos].opb;
String opc=question[pos].opc;
String opd=question[pos].opd;
c=CA[pos];
a=AS[pos];
%>  
<td>Question <%=pos+1%></td>
<td width="500"><%= ques %></td>
<td><%=c  %></td>
<td><%= a%>
</tr>
</table>
<%} %>
</body>
</html>

我写了所有的东西都是写的请忽略会议

输出是

程序的输出 只有第一行采用表格格式

请建议一些要执行的工作或如何修改它

使用 scriplet 编写代码是一种非常糟糕的做法。您应该避免使用纸币。无论如何,请尝试使用以下代码:

在此处插入标题
<% String c,a;
String[] CA =(String[])session.getAttribute("CA");
String[] AS =(String[])session.getAttribute("AS");
List<quest> l=(List<quest>)session.getAttribute("myquestion");
quest[] question = new quest[l.size()];
l.toArray(question);
%>
<table border=1>
<tr>
<th  colspan="2" width="500">Questions</th>
<th>CorrectAnswer</th>
<th>Answer sheet</th>
</tr>
<% 
for(pos=0;pos<question.length;pos++){
String ques=question[pos].ques;
String opa=question[pos].opa;
String opb=question[pos].opb;
String opc=question[pos].opc;
String opd=question[pos].opd;
c=CA[pos];
a=AS[pos];
%>
<tr><!-- moved tr inside for loop-->
<td>Question <%=pos+1%></td>
<td width="500"><%= ques %></td>
<td><%=c  %></td>
<td><%= a%></td><!-- added missing /td-->
</tr>
<%} %>
</table><!-- moved /table outside of for loop-->
</body>

问题是你的表格html没有很好地嵌套在循环中。

<% 
for(pos=0;pos<question.length;pos++){
String ques=question[pos].ques;
String opa=question[pos].opa;
String opb=question[pos].opb;
String opc=question[pos].opc;
String opd=question[pos].opd;
c=CA[pos];
a=AS[pos];
%>
<tr> <!-- moved inside -->
<td>Question <%=pos+1%></td>
<td width="500"><%=ques%></td>
<td><%=c%></td>
<td><%=a%></td> <!-- added missing closing tag -->
</tr>
<%} %>
</table><!-- moved outside -->

附言还可以考虑使用 JSTL 核心标记来实现这种实现。

最新更新