先分别介绍一下select标签和struts2的iterator标签
struts2的iterator标签主要用来对集合的迭代输出,有一下三个属性:
value:被迭代的集合,可选
id :指定集合里面的元素的id
status 迭代元素的索引,[b]该属性制定了迭代是的IteratorStatus实例,通过该实例可以判断当前迭代元素的属性值[/b],默认值为false。
IteratorStatus实例包括一下几个方法:
int getCount() 返回当前迭代了几个元素
int getIndex() 返回当前元素索引
boolean isEven() 当然的索引是否偶数
boolean isFirst() 当前是否第一个元素
boolean isLast() 当前是否最后一个元素
boolean isOdd() 当前元素索引是否奇数
select标签有一下几个可选属性:
disabled disabled 规定禁用该下拉列表。
multiple multiple 规定可选择多个选项。
name name 规定下拉列表的名称。
size number 规定下拉列表中可见选项的数目。
示例:
<select>
<option value ="volvo">Volvo</option>
<option value ="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
在iterator里使用select,jsp代码:
action :
在action里设定多一些answeri,保证不会少于实际iterator要输出的集合里answer的个数。然后将这些answeri放到一个字符串数组里,就可以放到for循环里调用了。
struts2的iterator标签主要用来对集合的迭代输出,有一下三个属性:
value:被迭代的集合,可选
id :指定集合里面的元素的id
status 迭代元素的索引,[b]该属性制定了迭代是的IteratorStatus实例,通过该实例可以判断当前迭代元素的属性值[/b],默认值为false。
IteratorStatus实例包括一下几个方法:
int getCount() 返回当前迭代了几个元素
int getIndex() 返回当前元素索引
boolean isEven() 当然的索引是否偶数
boolean isFirst() 当前是否第一个元素
boolean isLast() 当前是否最后一个元素
boolean isOdd() 当前元素索引是否奇数
select标签有一下几个可选属性:
disabled disabled 规定禁用该下拉列表。
multiple multiple 规定可选择多个选项。
name name 规定下拉列表的名称。
size number 规定下拉列表中可见选项的数目。
示例:
<select>
<option value ="volvo">Volvo</option>
<option value ="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
在iterator里使用select,jsp代码:
<form action="AnswerCAction">
<table width="80%" border="0" align="center" cellpadding="3" cellspacing="1" class="table_style">
<s:iterator value="choice" status="i">
<tr>
<td width="90%" class="xx"><s:property value="#i.getIndex()+1"/> <s:property value="cquestion"/></td>
<td><select name="answer<s:property value="#i.getIndex()"/>">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
</td>
</tr>
</s:iterator>
</table>
<input name="submit" type="submit" class="button" value="下一页"/>
</form>
action :
package action;
import hibernate.bean.Choice;
import hibernate.dao.AnswerCDao;
import java.util.List;
public class AnswerCAction extends SuperAction{
private Choice choice;
private List testclist;
private List choicelist;
private Long stuansId;
private Long testChoId;
private Long choiceId;
private AnswerCDao accDao;
private int i;
private String[] setAnswer = new String[30];
private String answer0;
private String answer1;
private String answer2;
private String answer3;
private String answer4;
private String answer5;
private String answer6;
private String answer7;
private String answer8;
private String answer9;
private String answer10;
private String answer11;
private String answer12;
private String answer13;
private String answer14;
private String answer15;
private String answer16;
private String answer17;
private String answer18;
private String answer19;
private String answer20;
private String answer21;
private String answer22;
private String answer23;
private String answer24;
private String answer25;
private String answer26;
private String answer27;
private String answer28;
private String answer29;
private String answer30;
public Choice getChoice() {
return choice;
}
public void setChoice(Choice choice) {
this.choice = choice;
}
public String getAnswer0() {
return answer0;
}
public void setAnswer0(String answer0) {
this.answer0 = answer0;
}
public String getAnswer1() {
return answer1;
}
public void setAnswer1(String answer1) {
this.answer1 = answer1;
}
public String getAnswer2() {
return answer2;
}
public void setAnswer2(String answer2) {
this.answer2 = answer2;
}
public String getAnswer3() {
return answer3;
}
public void setAnswer3(String answer3) {
this.answer3 = answer3;
}
public String getAnswer4() {
return answer4;
}
public void setAnswer4(String answer4) {
this.answer4 = answer4;
}
public String getAnswer5() {
return answer5;
}
public void setAnswer5(String answer5) {
this.answer5 = answer5;
}
public String getAnswer6() {
return answer6;
}
public void setAnswer6(String answer6) {
this.answer6 = answer6;
}
public String getAnswer7() {
return answer7;
}
public void setAnswer7(String answer7) {
this.answer7 = answer7;
}
public String getAnswer8() {
return answer8;
}
public void setAnswer8(String answer8) {
this.answer8 = answer8;
}
public String getAnswer9() {
return answer9;
}
public void setAnswer9(String answer9) {
this.answer9 = answer9;
}
public String getAnswer10() {
return answer10;
}
public void setAnswer10(String answer10) {
this.answer10 = answer10;
}
public String getAnswer11() {
return answer11;
}
public void setAnswer11(String answer11) {
this.answer11 = answer11;
}
public String getAnswer12() {
return answer12;
}
public void setAnswer12(String answer12) {
this.answer12 = answer12;
}
public String getAnswer13() {
return answer13;
}
public void setAnswer13(String answer13) {
this.answer13 = answer13;
}
public String getAnswer14() {
return answer14;
}
public void setAnswer14(String answer14) {
this.answer14 = answer14;
}
public String getAnswer15() {
return answer15;
}
public void setAnswer15(String answer15) {
this.answer15 = answer15;
}
public String getAnswer16() {
return answer16;
}
public void setAnswer16(String answer16) {
this.answer16 = answer16;
}
public String getAnswer17() {
return answer17;
}
public void setAnswer17(String answer17) {
this.answer17 = answer17;
}
public String getAnswer18() {
return answer18;
}
public void setAnswer18(String answer18) {
this.answer18 = answer18;
}
public String getAnswer19() {
return answer19;
}
public void setAnswer19(String answer19) {
this.answer19 = answer19;
}
public String getAnswer20() {
return answer20;
}
public void setAnswer20(String answer20) {
this.answer20 = answer20;
}
public String getAnswer21() {
return answer21;
}
public void setAnswer21(String answer21) {
this.answer21 = answer21;
}
public String getAnswer22() {
return answer22;
}
public void setAnswer22(String answer22) {
this.answer22 = answer22;
}
public String getAnswer23() {
return answer23;
}
public void setAnswer23(String answer23) {
this.answer23 = answer23;
}
public String getAnswer24() {
return answer24;
}
public void setAnswer24(String answer24) {
this.answer24 = answer24;
}
public String getAnswer25() {
return answer25;
}
public void setAnswer25(String answer25) {
this.answer25 = answer25;
}
public String getAnswer26() {
return answer26;
}
public void setAnswer26(String answer26) {
this.answer26 = answer26;
}
public String getAnswer27() {
return answer27;
}
public void setAnswer27(String answer27) {
this.answer27 = answer27;
}
public String getAnswer28() {
return answer28;
}
public void setAnswer28(String answer28) {
this.answer28 = answer28;
}
public String getAnswer29() {
return answer29;
}
public void setAnswer29(String answer29) {
this.answer29 = answer29;
}
public String getAnswer30() {
return answer30;
}
public void setAnswer30(String answer30) {
this.answer30 = answer30;
}
public AnswerCDao getAccDao() {
return accDao;
}
public void setAccDao(AnswerCDao accDao) {
this.accDao = accDao;
}
public String execute()throws Exception{
//String a0 = getAnswer0();
String[] getAnswer = {this.getAnswer0(),this.getAnswer1(),this.getAnswer2(),this.getAnswer3(),this.getAnswer4(),this.getAnswer5(),this.getAnswer6(),this.getAnswer7(),this.getAnswer8(),this.getAnswer9(),this.getAnswer10(),this.getAnswer11(),this.getAnswer12(),this.getAnswer13(),this.getAnswer14(),this.getAnswer15(),this.getAnswer16(),this.getAnswer17(),this.getAnswer18(),this.getAnswer19(),this.getAnswer20(),this.getAnswer21(),this.getAnswer22(),this.getAnswer23(),this.getAnswer24(),this.getAnswer25(),this.getAnswer26(),this.getAnswer27(),this.getAnswer28(),this.getAnswer29(),this.getAnswer30()};
testclist = (List) session.get("testclist");
stuansId = (Long) session.get("stuansid");
choicelist = (List) session.get("choicelist");
int size = choicelist.size();
if(size!=0){
for(i=0;i<size;i++){
//choiceId = (Long) choicelist.get(i);
testChoId = (Long) testclist.get(i);
accDao.insert2ac(testChoId,stuansId,getAnswer[i]);
}
return "success";
}else{
return "error";
}
}
}
在action里设定多一些answeri,保证不会少于实际iterator要输出的集合里answer的个数。然后将这些answeri放到一个字符串数组里,就可以放到for循环里调用了。