`
bolan392
  • 浏览: 274401 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

(5)将实体对象作为ActionForm的属性

阅读更多

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属性为实体对象名.属性名

<form action="regist2.do" method="get">
   name:<input type="text" name="student.userName">
   age :<input type="text" name="student.age">
   address:<input type="text" name="student.address">
   <input type="submit" name="button1">
  </form>

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");
 }

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics