---
title: "（5）将实体对象作为ActionForm的属性"
author: "bolan392"
date: "2009-07-09T16:52:01Z"
tags: []
slug: "5-actionform"
draft: false
lastmod: "2009-07-09T16:52:01Z"
id: 422722
url: "https://bolan392.iteye.com/blog/422722"
---

1.        新建实体对象Student

 public class Student {  
 private String userName;  
 private int age;  
 private String address;  
 public String getUserName() {  
  return userName;  
 }  
 public void setUserName(String userName) {  
  this.userName = userName;  
 }  
 public int getAge() {  
  return age;  
 }  
 public void setAge(int age) {  
  this.age = age;  
 }  
 public String getAddress() {  
  return address;  
 }  
 public void setAddress(String address) {  
  this.address = address;  
 }  
 @Override  
 public String toString() {  
  // TODO Auto-generated method stub  
  return userName+age+address;  
 }  
}

2.    新建ActionForm,将实体对象作为其成员变量

 public class RegistForm extends ActionForm {  
 private  int count;  
 private Student student = new Student();  
 public Student getStudent() {  
  return student;  
 }  
 public void setStudent(Student student) {  
  this.student = student;  
 }  
}

 

3.  创建页面,页面中input元素的name属性为实体对象名.属性名

&lt;form action="regist2.do" method="get"&gt;  
   name:&lt;input type="text" name="student.userName"&gt;  
   age :&lt;input type="text" name="student.age"&gt;  
   address:&lt;input type="text" name="student.address"&gt;  
   &lt;input type="submit" name="button1"&gt;  
  &lt;/form&gt;

4.  配置struts-config.xml

 

5.    创建Action,当该execute方法被调用时，ActionForm当中的实体对象已经被填充值

  public ActionForward execute(ActionMapping mapping, ActionForm form,  
   HttpServletRequest request, HttpServletResponse response) {  
  //页面对象  
  RegistForm rf = (RegistForm)form;  
  //封装业务对象BO  
  Student s = rf.getStudent();  
  System.out.println(s);  
  StudentBiz sBiz = new StudentBiz();  
  sBiz.save(s);  
  return mapping.findForward("success");  
 }