zoukankan      html  css  js  c++  java
  • strus2中获取表单数据 两种方式 属性驱动 和模型驱动

    strus2中获取表单数据 两种方式 属性驱动 和模型驱动

    属性驱动

    /**
    * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值
    * 如果一个属性在对象栈,在页面上可以根据name属性进行回显
    */

    /**
    * 属性驱动实现的条件:
    * 1、当前请求的action在栈顶,所以action中的属性就暴漏出来了
    * 2、获取页面上表单的元素,整合成一个map
    * 3、调用setValue方法赋值
    */

     1 package cn.itcast.struts2.sh;
     2 
     3 import java.util.ArrayList;
     4 import java.util.HashMap;
     5 import java.util.List;
     6 import java.util.Map;
     7 
     8 import com.opensymphony.xwork2.Action;
     9 import com.opensymphony.xwork2.ActionContext;
    10 import com.opensymphony.xwork2.ModelDriven;
    11 import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor;
    12 import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
    13 import com.opensymphony.xwork2.util.ValueStack;
    14 
    15 public class UserAction {
    16         
    17     private String ss;
    18     public String getSs() {
    19         return ss;
    20     }
    21     public void setSs(String ss) {
    22         this.ss = ss;
    23     }
    24     public String setValue(){
    25         ValueStack valueStack = ActionContext.getContext().getValueStack();
    26         /**
    27          * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值
    28          * 如果一个属性在对象栈,在页面上可以根据name属性进行回显
    29          */
    30         
    31         /**
    32          * 属性驱动实现的条件:
    33          *    1、当前请求的action在栈顶,所以action中的属性就暴漏出来了
    34          *    2、获取页面上表单的元素,整合成一个map
    35          *    3、调用setValue方法赋值
    36          */
    37                 //或者直接给ss赋值,因为当前action会放到StackValue栈顶所以可以取相关的里面的数据
    38         valueStack.setValue("ss", "ss");
    39         List<User> userList = new ArrayList<User>();
    40         List<List<User>> users = new ArrayList<List<User>>();
    41         User user = new User();
    42         user.setUid(1L);
    43         user.setUname("aaa");
    44         userList.add(user);
    45         users.add(userList);
    46         ActionContext.getContext().put("users", users);
    47         
    48         Map<String, List<User>> map = new HashMap<String, List<User>>();
    49         map.put("userList", userList);
    50         ActionContext.getContext().put("map", map);
    51         return "index";
    52     }
    53 }

    当 action中的与表单交互的基本数据项非常多的时候,在一个action中写很多基本元素显得代码非常臃肿,所以建立了一个Javaben 专门用于放基本数据,然后通过模型驱动的形式和页面进行交互,放在StackValue堆栈中

    内部原理是用到一个模型驱动的拦截器ModelDrivenInterceptor类中的intercept方法 然后装载数据到自己写的javaben

    然后会把 UserAction和 User都放到栈顶StackValue 中的

    代码:自己定义的javaben

     1 package cn.itcast.struts2.sh;
     2 
     3 public class User {
     4     private Long uid;
     5     private String uname;
     6     public Long getUid() {
     7         return uid;
     8     }
     9     public void setUid(Long uid) {
    10         this.uid = uid;
    11     }
    12     public String getUname() {
    13         return uname;
    14     }
    15     public void setUname(String uname) {
    16         this.uname = uname;
    17     }
    18 }

    action中装载这个javabean

     1 package cn.itcast.struts2.sh;
     2 
     3 import java.util.ArrayList;
     4 import java.util.HashMap;
     5 import java.util.List;
     6 import java.util.Map;
     7 
     8 import com.opensymphony.xwork2.Action;
     9 import com.opensymphony.xwork2.ActionContext;
    10 import com.opensymphony.xwork2.ModelDriven;
    11 import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor;
    12 import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
    13 import com.opensymphony.xwork2.util.ValueStack;
    14 
    15 public class UserAction implements ModelDriven<User>{
    16     
    17     private User model = new User();
    18     
    19     public User getModel() {
    20         // TODO Auto-generated method stub
    21         return this.model;
    22     }
    23     
    24     private String ss;
    25     public String getSs() {
    26         return ss;
    27     }
    28     public void setSs(String ss) {
    29         this.ss = ss;
    30     }
    31     public String setValue(){
    32         ValueStack valueStack = ActionContext.getContext().getValueStack();
    33         /**
    34          * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值
    35          * 如果一个属性在对象栈,在页面上可以根据name属性进行回显
    36          */
    37         
    38         /**
    39          * 属性驱动实现的条件:
    40          *    1、当前请求的action在栈顶,所以action中的属性就暴漏出来了
    41          *    2、获取页面上表单的元素,整合成一个map
    42          *    3、调用setValue方法赋值
    43          */
    44         valueStack.setValue("ss", "ss");
    45         List<User> userList = new ArrayList<User>();
    46         List<List<User>> users = new ArrayList<List<User>>();
    47         User user = new User();
    48         user.setUid(1L);
    49         user.setUname("aaa");
    50         userList.add(user);
    51         users.add(userList);
    52         ActionContext.getContext().put("users", users);
    53         
    54         Map<String, List<User>> map = new HashMap<String, List<User>>();
    55         map.put("userList", userList);
    56         ActionContext.getContext().put("map", map);
    57         return "index";
    58     }
    59 }
  • 相关阅读:
    Linux0.12内存寻址
    Linux0.12任务调度与进程切换
    Mapreduce实例——倒排索引
    解决echart警告:Can't get dom width or height
    Mapreduce实例——MapReduce自定义输入格式
    Mapreduce实例——ChainMapReduce
    Mapreduce实例——二次排序
    设计模式中介者模式
    设计模式七大原则
    Mapreduce实例——MapReduce自定义输出格式
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3774131.html
Copyright © 2011-2022 走看看