Java 使用Struts标记时,Struts操作数据不会出现在jsp中
我是struts新手,最近听说使用jstl标记是最受欢迎的方式,但我很难做到这一点 Questions.javaJava 使用Struts标记时,Struts操作数据不会出现在jsp中,java,html,jsp,jstl,struts1,Java,Html,Jsp,Jstl,Struts1,我是struts新手,最近听说使用jstl标记是最受欢迎的方式,但我很难做到这一点 Questions.java public class Questions { private String label; private String option1; .... public String getLabel() { return label; } public void setLabel(String label) { this.label = label; ... }
public class Questions {
private String label;
private String option1;
....
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
...
}
这是我的动作课
PaperEdit val = (PaperEdit)form;
String sql = "SELECT * FROM "+val.getCategory();
List<Questions> question = new ArrayList<Questions>();
try{
Statement st = DBConnection.DBConnection.DBConnect();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
question.add(rs.getString("ques_name"));
question.add(rs.getString(3));
question.add(rs.getString(4));
question.add(rs.getString(5));
question.add(rs.getString(6));
question.add(rs.getString(7));
}
request.setAttribute("ques", question);
}
PaperEdit val=(PaperEdit)表单;
String sql=“SELECT*FROM”+val.getCategory();
列表问题=新建ArrayList();
试试{
语句st=DBConnection.DBConnection.DBConnect();
结果集rs=st.executeQuery(sql);
while(rs.next()){
添加(rs.getString(“ques_name”);
添加(rs.getString(3));
添加(rs.getString(4));
添加(rs.getString(5));
添加(rs.getString(6));
添加(rs.getString(7));
}
request.setAttribute(“ques”,问题);
}
现在,Netbeans显示while循环中的所有语句,其中包含错误:
找不到适合添加的方法(字符串)
方法列表。添加(整数,问题)不适用
(实际参数列表和正式参数列表长度不同)
方法列表。添加(问题)不适用
(实际参数字符串不能通过方法调用转换转换为问题)
我试图使用jstl标记在jsp页面中获取这些数据。这是它禁止浏览的那一页
display.jsp
<table width="60%" align="center" border="1px">
<logic:iterate name="ques" id="question">
<tr>
<td><bean:write name="question" property="ques_name"/></td>
</tr>
</logic:iterate>
</table>
首先,您没有在JSP中使用任何JSTL标记。您正在使用Struts标记。您确实应该更喜欢JSTL标记 现在,您的问题是设计问题。与6个字符串列表不同,您应该有一个
Question
实例列表,其中Question
类将具有以下属性:
- 标签
- 选择1
- 选择2
- 选择3
- 选择4
- 答复
<logic:iterate name="questions" id="question">
<tr>
<td><bean:write name="question" property="label"/></td>
<td><bean:write name="question" property="option1"/></td>
...
</tr>
</logic:iterate>
现在,当从表中读取行时,您应该为每行创建一个Question
对象,并填写问题列表。由于列表包含多个问题,我们将其命名为问题
,而不是问题
:
// this is an list of questions. It's empty initially
// this list isn't meant to contain Strings, and it can't.
// It will contain one Question object for each row in the table.
List<Question> questions = new ArrayList<Question>();
try{
Statement st = DBConnection.DBConnection.DBConnect();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
// this block is executed for each row in the table.
// Each row is transformed into a Question object
Question question = new Question();
question.setLabel((rs.getString("ques_name"));
question.setOption1(rs.getString(3));
question.setOption2(rs.getString(4));
question.setOption3(rs.getString(5));
question.setOption4(rs.getString(6));
question.setAnswer(rs.getString(7));
// now that we have created a Question object and populated it with the
// cells of the row, we will add it to the list of questions:
questions.add(question);
}
// So now, we have a list of questions. Each element of the list is an object
// of type Question, which has a property label, a property option1, etc.
request.setAttribute("questions", questions);
}
//这是一个问题列表。一开始是空的
//此列表不能包含字符串,也不能包含。
//它将为表中的每一行包含一个问题对象。
列出问题=新建ArrayList();
试试{
语句st=DBConnection.DBConnection.DBConnect();
结果集rs=st.executeQuery(sql);
while(rs.next()){
//此块针对表中的每一行执行。
//每一行都转换为一个问题对象
问题=新问题();
问题.setLabel((rs.getString(“ques_name”));
问题.setOption1(rs.getString(3));
问题.setOption2(rs.getString(4));
问题.setOption3(rs.getString(5));
问题.setOption4(rs.getString(6));
问题.setAnswer(rs.getString(7));
//现在我们已经创建了一个Question对象并用
//行的单元格,我们将其添加到问题列表中:
问题.加入(问题);
}
//现在,我们有一个问题列表,列表中的每个元素都是一个对象
//具有属性标签、属性选项1等的问题类型。
request.setAttribute(“问题”,疑问);
}
现在在JSP中,我们可以遍历这个问题列表。在循环中,当前问题将命名为“问题”
您可以用一些代码详细说明问题类的内容吗?我是把它放在action类还是其他地方?您应该能够自己创建一个包含字段和getter的类。如果不是,那么您还没有准备好进行web编程,您应该阅读一些Java入门教程。为什么要在action c中创建这个类lass?创建一个顶级类,并将其放入您认为应该是的任何包中。无论如何,请参阅我编辑的答案。应用程序的流程不是像JSP->Struts ActionForm Bean->Struts Action->JSP吗?那么我该如何处理您给我的解决方案呢?(当然,我知道如何生成getter和setter)在JDBC代码中,不是创建6个列表,而是创建单个问题实例列表。在操作中,不是将这6个列表设置为属性,而是将单个问题列表设置为1个属性(名为问题)。在JSP中,使用我的答案中的代码。您当前的流程没有改变。不,您没有。您没有在任何地方使用问题类。列表必须是列表
,而不是列表
。结果集的每一行都必须转换为问题对象,并添加到问题列表中。
<c:forEach var="question" items="${questions}">
<tr>
<td><c:out value="${question.label}" /></td>
<td><c:out value="${question.option1}"/></td>
...
</tr>
</c:forEach>
public class Question {
private String label;
private String option1;
// other fields omitted for brevity
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
// other getters and setters omitted for brevity
}
// this is an list of questions. It's empty initially
// this list isn't meant to contain Strings, and it can't.
// It will contain one Question object for each row in the table.
List<Question> questions = new ArrayList<Question>();
try{
Statement st = DBConnection.DBConnection.DBConnect();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
// this block is executed for each row in the table.
// Each row is transformed into a Question object
Question question = new Question();
question.setLabel((rs.getString("ques_name"));
question.setOption1(rs.getString(3));
question.setOption2(rs.getString(4));
question.setOption3(rs.getString(5));
question.setOption4(rs.getString(6));
question.setAnswer(rs.getString(7));
// now that we have created a Question object and populated it with the
// cells of the row, we will add it to the list of questions:
questions.add(question);
}
// So now, we have a list of questions. Each element of the list is an object
// of type Question, which has a property label, a property option1, etc.
request.setAttribute("questions", questions);
}
<logic:iterate name="questions" id="question">
<tr>
<%-- let's write the property label of the current question
This will in fact call question.getLabel() and write it to the response
--%>
<td><bean:write name="question" property="label"/></td>
<%-- let's write the property option1 of the current question
This will in fact call question.getOption1() and write it to the response
--%>
<td><bean:write name="question" property="option1"/></td>
</tr>
</logic:iterate>