Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Stucts2 页面上的值如何与Action的属性值对应

在Strut2中,页面的数据和Action有两种基本对应方式:分别是:属性驱动(FieldDriven)和模型驱动(ModelDriven)。属性驱动又分为两种情况:一种是基本数据类型的属性对应:另一种是JavaBean风格的属性对应。下面就分别来看看它们是什么意思都如何实现。属性驱动(FieldDriven):基本数据类型的属性对应在index.jsp中,我是这样写的<form action="helloWorldAction.action" method="post">
    账号:<input type="text" name="account"/><br>
    密码:<input type="password" name="password"/>
    <input type="submit" value="提交"/>
  </form>在Action中是这样写的public class HelloWorldAction extends ActionSupport{
 
    private String account;
    private String password;
   
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
   
    //其他代码省略....
}
可以发现,在页面上index.jsp的name的属性,和Action的属性是同一个名称的。这样一来,当页面提交的时候,Struts2会自动从request对象里把数据提取出来,然后按照名称进行对应,自动设置到Action属性里面去。2.属性驱动FieldDriven(JavaBean风格的属性对应) (1)先看看域对象的写法,按照JavaBean的风格来写,示例代码如下:public class HelloWorldModel {
   
    private String account;
    private String password;
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}
(2)这时候Action写法也有变化,主要就是直接使用这个对象,其实就是定义一个属性就是这样对象类型,然后为这个属性提供相应的getter()和setter()方法即可。如果把这个属性的可访问属性设置为public就不需要getter()和setter()方法。Action修改后的示例代码如下: public class HelloWorldAction extends ActionSupport{
 
    private HelloWorldModel hwm =  new HelloWorldModel();
 
    public HelloWorldModel getHwm() {
        return hwm;
    }
 
    public void setHwm(HelloWorldModel hwm) {
        this.hwm = hwm;
    }
    //其他代码省略....
} (3)Action发生变化后,index.jsp页面中的内容也要进行相应的改变,否则数据就无法正确对应,主要是在相应的name属性上,添加一个域对象的前缀,指明这个值到底是对应到哪宇哥域对象里面去,示例如下:<form action="helloWorldAction.action" method="post">
       账号:<input type="text" name="hwm.account"/><br>
       密码:<input type="password" name="hwm.password"/>
       <input type="submit" value="提交"/>
    </form>
</body>3.模型驱动(ModelDriven) 它的基本实现方式让Action实现一个ModelDriven的接口,这个接口需要我们实现getModel的方法。这个方法的返回值就是Action所使用的数据模型对象。修改后的示例代码如下: public class HelloWorldAction extends ActionSupport implements ModelDriven{
 
    private HelloWorldModel hwm =  new HelloWorldModel();
 
    public Object getModel() {
        // TODO Auto-generated method stub
        return hwm;
    }
    //其他代码省略....
}在index.jsp中也需要做相应的调整,主要是是去掉刚才给name属性添加的hwm前缀。其示例代码如下: <form action="helloWorldAction.action" method="post">
       账号:<input type="text" name="account"/><br>
       密码:<input type="password" name="password"/>
       <input type="submit" value="提交"/>
 </form>为什么种方式不需要前缀了呢?  因为使用这种ModelDriven的方式,一个Actiion只能对应一个Model,因此不需要添加前缀,Struts2j就知道,页面上account的值就对应到这个Model的account属性。Struts2学习笔记-Value Stack(值栈)和OGNL表达式  http://www.linuxidc.com/Linux/2015-07/120529.htm struts2文件上传(保存为BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htmStruts2的入门实例 http://www.linuxidc.com/Linux/2013-05/84618.htmStruts2实现ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm遇到的Struts2文件下载乱码问题 http://www.linuxidc.com/Linux/2014-03/98990.htmStruts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htmStruts2 注解模式的几个知识点 http://www.linuxidc.com/Linux/2013-06/85830.htmStruts 的详细介绍:请点这里
Struts 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-11/124858.htm