• Struts2页面数据和Action数据三种基本对应方式


    在Struts2中页面的数据和Action有两种基本对应方式,分别是:属性驱动和模型驱动。

    其中属性驱动又分为:基本类型的属性对应和JavaBean风格的属性对应

    一。属性驱动

    1.基本类型的属性驱动。

    在Action里面设置属性名称和jsp页面设置的属性名称一样就可以了,当然Action里面的属性要有getter、setter方法,或者直接把属性设置成public的(但是一般都不那么做)。

    package com.action;
    
    import com.opensymphony.xwork2.Action;
    
    public class HelloWorldAction implements Action {
        
        private String account;
        private String password;
        private String submitFlag;
        
        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            if(account.equals("ironman")&&password.equals("123")){
                return "toWelcome";
            }else{
                return "loginError";
            }
            
        }
        //public 的 getter setter方法。。。
        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;
        }
    
        public String getSubmitFlag() {
            return submitFlag;
        }
    
        public void setSubmitFlag(String submitFlag) {
            this.submitFlag = submitFlag;
        }
    }

    jsp代码

    <form action="helloworldAction" method="post">
        <input type="hidden" name="submitFlag" value="login"/>
        username:<input type="text" name="account"><br>
        password:<input type="password" name="password"><br>
        <input type="submit" value="submit"/>
        
    </form>

    可以看到那些属性名都是一样的。

    2.JavaBean风格的属性对应。

    首先写一个JavaBean将属性封装起来,并且提供public的setter和getter方法。

    package com.bean;
    
    public class HelloWorld {
        private String account;
        private String password;
        private String submitFlag;
        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;
        }
        public String getSubmitFlag() {
            return submitFlag;
        }
        public void setSubmitFlag(String submitFlag) {
            this.submitFlag = submitFlag;
        }
        
    }

    我们现在来看Action的代码会发现少很多,内容也比较清晰,所以平常使用最多的也是这种方法。当然三种方法可以混合使用,但是要避免冲突。

    package com.action;
    
    import com.bean.HelloWorld;
    import com.opensymphony.xwork2.Action;
    
    public class HelloWorldAction implements Action{
        
        private HelloWorld hello = new HelloWorld();
        
        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            if(hello.getAccount().equals("ironman")&&hello.getPassword().equals("123")){
                return "toWelcome";
            }else{
                return "loginError";
            }
        }
    
        public HelloWorld getHello() {
            return hello;
        }
    
        public void setHello(HelloWorld hello) {
            this.hello = hello;
        }
        
    }

    再来看看jsp中的代码,会有一些不一样。

    <form action="helloworldAction" method="post">
        <input type="hidden" name="hello.submitFlag" value="login"/>
        username:<input type="text" name="hello.account"><br>
        password:<input type="password" name="hello.password"><br>
        <input type="submit" value="submit"/>
        
    </form>

    我们看到,在属性前面要加上了那个对象的名字。

    二。使用模型驱动。

    他的基本实现方式是让Action实现一个ModelDriven接口,这个接口需要我们实现个getModel的方法,这个方法返回的就是Action所使用的数据模型对象。

    主要是在Action里面有变化,只是添加了ModelDriven的实现,另外去掉了对应的getter/setter方法,其他的都没有什么变化。

    package com.action;
    
    import com.bean.HelloWorld;
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ModelDriven;
    
    public class HelloWorldAction implements Action , ModelDriven{
        
        private HelloWorld hello = new HelloWorld();
        
        
        @Override
        public Object getModel() {
            // TODO Auto-generated method stub
            return hello;
        }
    
        @Override
        public String execute() throws Exception  {
            // TODO Auto-generated method stub
            if(hello.getAccount().equals("ironman")&&hello.getPassword().equals("123")){
                return "toWelcome";
            }else{
                return "loginError";
            }
        }
    
    }
  • 相关阅读:
    TiDB基本简介
    flink双流join
    Kafka客户端内存缓冲GC处理机制--客户端内存
    shell常用命令大全[bigdata版]
    kafka channel的parseAsFlumeEvent解析event
    hdfs的写流程以及namenode,datanode挂掉后处理
    [转载]LSM树由来、设计思想以及应用到HBase的索引
    HBase之写流程与读流程
    HBase之写流程中的刷写时机
    HBase之读写流程中WAL机制
  • 原文地址:https://www.cnblogs.com/ironmantony/p/3517014.html
Copyright © 2020-2023  润新知