移:使用Struts提交多行数据

使用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, HttpServletRequest 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);

    /**
     * @return Returns the surveys.
     */
    public List getSurveys() {
        return surveys;
    }

    /**
     * @param surveys The surveys to set.
     */
    public void setSurveys(List surveys) {
        this.surveys = surveys;
    }

}
SurveyForm:

public class SurveyForm extends BaseForm{
    private String checkPerson;
    /**
     * @return Returns the checkPerson.
     */
    public String getCheckPerson() {
        return checkPerson;
    }
    /**
     * @param checkPerson The checkPerson to set.
     */
    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);
    }
}
 
结论:多行数据可以使用如:<INPUT TYPE="text" NAME="surveys[0].checkPerson">的方式提交,Struts会自动映射成List对象。不过需要注意使用AutoArrayList进行对象的实例化
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值