使用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进行对象的实例化
以下是使用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进行对象的实例化