博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts 2 ModelDriven Action
阅读量:6348 次
发布时间:2019-06-22

本文共 2568 字,大约阅读时间需要 8 分钟。

To create a ModelDriven Action our Action class should implement the ModelDriven interface and should include the modelDriven interceptor. The modelDriven interceptor is already included in the default stack.

The next step is to implement the getModel() method in such a way that it returns the application domain object, in our example we return the User object.

When using the ModelDriven method we need to initialize the User object ourselves.

The framework will automatically transfers the form data into the User object.

1 public class UserAction extends ActionSupport implements ModelDriven { 2  3     private User user = new User(); 4  5     public UserAction() { 6     } 7  8     public Object getModel() { 9         return user;10     }11 12     public String execute() {13         return SUCCESS;14     }15 16     public User getUser() {17         return user;18     }19 20     public void setUser(User user) {21         this.user = user;22     }23 }

You can directly access the user attributes like name, age etc in Action use the following syntax.

1 user.getXXX();

The User class contains the following attributes and the corresponding getter and setter methods.

public class User {    private String name;    private int age;    private String sex;    private String[] hobby;    private String country;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String[] getHobby() {        return hobby;    }    public void setHobby(String[] hobby) {        this.hobby = hobby;    }    public String getCountry() {        return country;    }    public void setCountry(String country) {        this.country = country;    }}

  In the jsp page the user attributes can be accessed directly. To refer the user's age, the value of the name attribute should be

name = "age"

  The index.jsp page contains the following code.

<%@taglib uri="/struts-tags" prefix="s" %>
User Details

  The result.jsp page contains the follwing code.

<%@taglib uri="/struts-tags" prefix="s" %>
User Details

User Details


User Name :
Age :
Hobbies :
Country :

  

转载于:https://www.cnblogs.com/JavaTechLover/archive/2013/03/17/Struts2-ModelDriven-Action.html

你可能感兴趣的文章
服务器要使用两张网卡做bond0以实现网络冗余和提高带宽
查看>>
dhcp 配置
查看>>
中国孩子最需要的6种教育
查看>>
PVS让存储颤抖,系列博文之二:PVS的写缓存新技术之MCS方式实测篇
查看>>
jQuery插件开发的两种方法及$.fn.extend的详解
查看>>
SSDB高可用方案
查看>>
undo表空间
查看>>
Exchange 2013部署系列之(六)配置邮件流和客户端访问
查看>>
TE二次开发:点线面图层的结构
查看>>
关于swift的一些属性总结
查看>>
python学习笔记-Day17 (上节知识点补充)
查看>>
python学习笔记-Day04-第一部分(冒泡算法的实现)
查看>>
企业中网络的高可用性之双网卡的绑定
查看>>
Exchange 2010之接受域
查看>>
ceph环境下 测试磁盘在写入时cache盘的占用情况
查看>>
找出数组中两数之和为指定值的所有整数对
查看>>
基本概念学习(2002)---指令周期
查看>>
Pentaho CDE详细开发使用手册
查看>>
Pylint的安装
查看>>
面向对象的三个基本特征 和 五种设计原则
查看>>