1.JSP页面
<html:form action="/demo.do">
<font size="2">
个人基本信息:<br>
<hr>
姓名<html:text property="name"/><br>
年龄<html:select property="age">
<html:option value="20">20</html:option>
<html:option value="21">21</html:option>
<html:option value="22">22</html:option>
</html:select><br>
性别:<html:radio property="sex" value="男">男</html:radio>
<html:radio property="sex" value="女">女</html:radio><br>
兴趣:<html:checkbox property="interest" value="看书">看书</html:checkbox>
<html:checkbox property="interest" value="睡觉">睡觉</html:checkbox><br>
备注:<html:textarea property="remark"></html:textarea><br>
<html:submit value="提交"/>
</font>
</html:form>
2.ActionForm
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.cw.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 09-25-2008
*
* XDoclet definition:
* @struts.form name="demoForm"
*/
public class DemoForm extends ActionForm {
private String sex;
private String remark;
private String[] interest;
private Integer age;
private String name;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getInterest() {
return interest;
}
public void setInterest(String[] interest) {
this.interest = interest;
}
}
3.Struts-comfig.xml
<struts-config>
<data-sources />
<form-beans>
<form-bean name="demoForm" type="com.cw.struts.form.DemoForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action
attribute="demoForm"
name="demoForm"
path="/demo"
scope="request"
type="com.cw.struts.action.DemoAction"
validate="false" />
</action-mappings>
<message-resources parameter="com.cw.struts.ApplicationResources" />
</struts-config>
4.Action
public class DemoAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
DemoForm demoForm = (DemoForm) form
System.out.println("姓名:"+demoForm.getName());
System.out.println("年龄:"+demoForm.getAge());
System.out.println("性别:"+demoForm.getSex());
String[] interest = (String[])demoForm.getInterest();
for(int i=0;i<interest.length;i++)
{
System.out.println("兴趣:"+interest[i]);
}
System.out.println("备注:"+demoForm.getRemark());
return null;
}
}
<html:form action="/demo.do">
<font size="2">
个人基本信息:<br>
<hr>
姓名<html:text property="name"/><br>
年龄<html:select property="age">
<html:option value="20">20</html:option>
<html:option value="21">21</html:option>
<html:option value="22">22</html:option>
</html:select><br>
性别:<html:radio property="sex" value="男">男</html:radio>
<html:radio property="sex" value="女">女</html:radio><br>
兴趣:<html:checkbox property="interest" value="看书">看书</html:checkbox>
<html:checkbox property="interest" value="睡觉">睡觉</html:checkbox><br>
备注:<html:textarea property="remark"></html:textarea><br>
<html:submit value="提交"/>
</font>
</html:form>
2.ActionForm
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.cw.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 09-25-2008
*
* XDoclet definition:
* @struts.form name="demoForm"
*/
public class DemoForm extends ActionForm {
private String sex;
private String remark;
private String[] interest;
private Integer age;
private String name;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getInterest() {
return interest;
}
public void setInterest(String[] interest) {
this.interest = interest;
}
}
3.Struts-comfig.xml
<struts-config>
<data-sources />
<form-beans>
<form-bean name="demoForm" type="com.cw.struts.form.DemoForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action
attribute="demoForm"
name="demoForm"
path="/demo"
scope="request"
type="com.cw.struts.action.DemoAction"
validate="false" />
</action-mappings>
<message-resources parameter="com.cw.struts.ApplicationResources" />
</struts-config>
4.Action
public class DemoAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
DemoForm demoForm = (DemoForm) form
System.out.println("姓名:"+demoForm.getName());
System.out.println("年龄:"+demoForm.getAge());
System.out.println("性别:"+demoForm.getSex());
String[] interest = (String[])demoForm.getInterest();
for(int i=0;i<interest.length;i++)
{
System.out.println("兴趣:"+interest[i]);
}
System.out.println("备注:"+demoForm.getRemark());
return null;
}
}