使用Struts提交多行数据
以下是使用
struts提交多行数据的
例子,提交多行数据至SurveyListForm:
提交页面form.jsp,用于提交两条数据,注意[]的使用:
<FORM METHOD=POST ACTION="survey.jui">
<INPUT TYPE="text" NAME="surveys[0].checkPerson">
<INPUT TYPE="text" NAME="surveys[1].checkPerson">
<INPUT TYPE="submit">
</FORM>
响应页面index.jsp(survey.jui),用于获得数据:
<logic:iterate id="survey" name="surveyListForm" property="surveys" indexId="index">
<html:text name="survey" property="checkPerson" indexed="true"/>
</logic:iterate>
struts-config.xml:
<form-beans>
<form-bean name="surveyForm" type="com.fenet.insurance.crm.web.form.SurveyForm" />
<form-bean name="surveyListForm" type="com.fenet.insurance.crm.web.form.SurveyListForm" />
</form-beans>
<action path="/survey" parameter="method" type="com.fenet.insurance.crm.web.action.SurveyAction" name="surveyListForm" scope="request" validate="false">
<forward name="method1" path="/index.jsp" />
</action>
SurveyAction:
public class SurveyAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, Http
ServletRequest request, HttpServletResponse response) throws Exception {
SurveyListForm sForm = (SurveyListForm)form;
List list = sForm.getSurveys();
for(int i=0; i<list.size(); i++){
SurveyForm f = (SurveyForm)list.get(i);
System.out.println(f.getCheckPerson());//后台打印多行数据 request.setAttribute("surveys", sForm);
return mapping.findForward("method1"); }
SurveyListForm定义多行数据:
public class SurveyListForm extends BaseForm{
private List surveys = new AutoArrayList (SurveyForm.class);
public List getSurveys() {
return surveys;
}
public void setSurveys(List surveys) {
this.surveys = surveys;
}
SurveyForm:
public class SurveyForm extends BaseForm{
private String checkPerson; public String getCheckPerson() {
return checkPerson;
public void setCheckPerson(String checkPerson) {
this.checkPerson = checkPerson; }
AutoArrayList,用于自动创建List里面的对象:
public class AutoArrayList extends ArrayList {
private Class itemClass;
public AutoArrayList(Class itemClass) {
this.itemClass = itemClass; public Object get(int index) {
try {
while (index >= size()) {
add(itemClass.newInstance()); } catch (Exception e) {
e.printStackTrace(); return super.get(index); }