zoukankan      html  css  js  c++  java
  • Struts2的数据封装

    1.属性驱动

      *提供属性的set方法

    HTML:

      

    <h1>数据封装方式一:提供set方法</h1>
    <form action="${pageContext.request.contextPath}/xxx.action" method="post">
        姓名:<input type="text" name="name" /><br/>
        年龄:<input type="text" name="age" />
       提交:<input type="submit" value="提交" />
    </form>

     Action:

    public class xxxAction extends ActionSupport {
          private String name;
          private Integer age;
    
           public void setName() {
                   this.name = name;
           }
           public void setAge() {
                   this.age = age;
           }  
    
         public String execute() throws Exception {
                   System.out.println(name);
                   System.out.println(age);
                    return NONE;
          }
    }            

    2.页面表达式的方式

     HTML:

    <h1>数据封装方式二:提供页面OGNL表达式的方式</h1>
    <form action="${pageContext.request.contextPath}/xxx.action" method="post">
        姓名:<input type="text" name="user.name" /><br/>
        年龄:<input type="text" name="user.age" /><br/>
       提交:<input type="submit" value="提交" />
    </form>

    Action:

    public class xxxAction extends ActionSupport {
          // 在Action中封装对象的属性:提供get AND set方法
          private User user;
    
           public void setUser() {
                   this.user = user;
           }
           public void getUser() {
                   this.user = user;
           }  
    
         public String execute() throws Exception {
                   System.out.println(user);
                    return NONE;
          }
    }    

    3.模型驱动(!)

    HTML:

    <h1>数据封装方式三:模型驱动的方式</h1>
    <form action="${pageContext.request.contextPath}/xxx.action" method="post">
        姓名:<input type="text" name="name" /><br/>
        年龄:<input type="text" name="age" /><br/>
       提交:<input type="submit" value="提交" />
    </form>

     Action:

    public class xxxAction extends ActionSupport implements ModelDriven<User> {
         
          private User user = new User();
        
          // 模型驱动的使用方法:
          @Override
          public User getModel() {
                  return user; 
         }
    
         public String execute() throws Exception {
                   System.out.println(user);
                    return NONE;
          }
    }         
  • 相关阅读:
    java实现操作系统磁盘寻道先来先服务算法
    专业素养:发布文件,别忘了给出校验信息
    vue系列教程-08vue的动画和过渡效果
    vue系列教程-07vue动态绑定样式
    vue系列教程-06vue的事件处理
    vue系列教程-05vue常用指令
    vue系列教程-04vue数据处理和页面渲染
    vue系列教程-03vuejs的结构和生命周期
    vue系列教程-01第一个vue程序
    vue系列教程-02什么是mvvm和spa
  • 原文地址:https://www.cnblogs.com/f-s-q/p/6416780.html
Copyright © 2011-2022 走看看