zoukankan      html  css  js  c++  java
  • Struts2学习笔记(六)——Action处理请求参数

    在struts2框架中关于Action处理请求参数有两种方案(三个方式),表单属性的名称应该和在Action类中定义的成员属性或者在JavaBean中定义的成员属性名称一样:

    1、属性驱动

      1)直接在Action类中定义成员属性来接收请求参数 (将Action当成javaBean),在Action中还需要定义成员属性的setter方法。

    表单信息:

    1 <form action="${pageContext.servletContext.contextPath}/testAction.action">
    2     name:<input type="text" name="name"><br>
    3     password:<input type="password" name="password"><br>
    4     <input type="submit" value="提交">
    5 </form>

    Action类:

     1 public class TestAction {
     2     private String name;
     3     private String password;
     4     
     5     public String getName() {
     6         return name;
     7     }
     8 
     9     public void setName(String name) {
    10         this.name = name;
    11     }
    12 
    13     public String getPassword() {
    14         return password;
    15     }
    16 
    17     public void setPassword(String password) {
    18         this.password = password;
    19     }
    20 
    21     public String execute() {
    22         System.out.println("name: " + name);
    23         System.out.println("password: " + password);
    24         return "success";
    25     }
    26 }

    前台输入信息:

    后台显示结果:

      2)直接将javaBean做为Action的属性,在页面上必须使用ognl表达式来描述组件的name属性

    1 username:<input type="text" name="user.username"><br>

    JavaBean:

     1 public class Person {
     2     private String name;
     3     private String password;
     4  
     5     public String getName() {
     6         return name;
     7     }
     8     public void setName(String name) {
     9          this.name = name;
    10     }
    11     public String getPassword() {
    12         return password;
    13     }
    14     public void setPassword(String password) {
    15         this.password = password;
    16     }
    17 }

    表单信息:

    1 <form action="${pageContext.servletContext.contextPath}/testAction.action">
    2     name:<input type="text" name="person.name"><br>
    3     password:<input type="password" name="person.password"><br>
    4     <input type="submit" value="提交">
    5 </form>

    Action:

     1 public class TestAction {
     2     private Person person;
     3     
     4     public Person getPerson() {
     5         return person;
     6     }
     7 
     8     public void setPerson(Person person) {
     9         this.person = person;
    10     }
    11 
    12     public String execute() {
    13         System.out.println("name: " + person.getName());
    14         System.out.println("password: " + person.getPassword());
    15         return "success";
    16     }
    17 }

    前台输入信息:

    后台显示结果:

    这种方式是通过name="params"的拦截器实现参数封装的。

    1 <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

    2、模型驱动

    模型驱动处理请求参数的步骤为:

    步骤:

    • 1.Action必须实现ModelDriven接口
    • 2.重写getModel()方法,返回一个javaBean对象.
    • 3.实例化一个javaBean对象.

    JavaBean:

     1 public class Person {
     2     private String name;
     3     private String password;
     4     public String getName() {
     5         return name;
     6     }
     7     public void setName(String name) {
     8         this.name = name;
     9     }
    10     public String getPassword() {
    11         return password;
    12     }
    13     public void setPassword(String password) {
    14         this.password = password;
    15     }
    16 }

    表单信息:

    1 <form action="${pageContext.servletContext.contextPath}/testAction.action">
    2     name:<input type="text" name="name"><br>
    3     password:<input type="password" name="password"><br>
    4     <input type="submit" value="提交">
    5 </form>

    Action:

     1 public class TestAction implements ModelDriven {
     2     private Person person = new Person();
     3     
     4     public Person getPerson() {
     5         return person;
     6     }
     7 
     8     public void setPerson(Person person) {
     9         this.person = person;
    10     }
    11 
    12     public String execute() {
    13         System.out.println("name: " + person.getName());
    14         System.out.println("password: " + person.getPassword());
    15         return "success";
    16     }
    17 
    18     @Override
    19     public Object getModel() {
    20         return person;
    21     }
    22 }

    前台输入信息:

    后台显示结果:

    4、封装数据到Collection或数组

    JavaBean:

     1 public class Person {
     2     private String name;
     3     private String password;
     4     public String getName() {
     5         return name;
     6     }
     7     public void setName(String name) {
     8         this.name = name;
     9     }
    10     public String getPassword() {
    11         return password;
    12     }
    13     public void setPassword(String password) {
    14         this.password = password;
    15     }
    16 }

    表单信息:

    1 <form action="${pageContext.servletContext.contextPath}/testAction.action">
    2     name1:<input type="text" name="list[0].name"><br>
    3     password1:<input type="password" name="list[0].password"><hr>
    4     name2:<input type="text" name="list[1].name"><br>
    5     password2:<input type="password" name="list[1].password"><br>
    6     <input type="submit" value="提交">
    7 </form>

    Action:

     1 public class TestAction {
     2     private List<Person> list;
     3 
     4     public List<Person> getList() {
     5         return list;
     6     }
     7 
     8     public void setList(List<Person> list) {
     9         this.list = list;
    10     }
    11 
    12     public String execute() {
    13         System.out.println("name1: " + list.get(0).getName());
    14         System.out.println("password1: " + list.get(0).getPassword());
    15         System.out.println("name2: " + list.get(1).getName());
    16         System.out.println("password2: " + list.get(1).getPassword());
    17         return "success";
    18     }
    19 }

    前台输入信息:

    后台显示结果:

    5、封装数据到Map

    JavaBean:

     1 public class Person {
     2     private String name;
     3     private String password;
     4     public String getName() {
     5         return name;
     6     }
     7     public void setName(String name) {
     8         this.name = name;
     9     }
    10     public String getPassword() {
    11         return password;
    12     }
    13     public void setPassword(String password) {
    14         this.password = password;
    15     }
    16 }

    表单信息:

    1 <form action="${pageContext.servletContext.contextPath}/testAction.action">
    2     name1:<input type="text" name="map['one'].name"><br>
    3     password1:<input type="password" name="map['one'].password"><hr>
    4     name2:<input type="text" name="map['two'].name"><br>
    5     password2:<input type="password" name="map['two'].password"><br>
    6     <input type="submit" value="提交">
    7 </form>

    Action:

    public class TestAction {
        private Map<String, Person> map;
    
        public Map<String, Person> getMap() {
            return map;
        }
    
        public void setMap(Map<String, Person> map) {
            this.map = map;
        }
    
        public String execute() {
            System.out.println("name1: " + map.get("one").getName());
            System.out.println("password1: " + map.get("one").getPassword());
            System.out.println("name2: " + map.get("two").getName());
            System.out.println("password2: " + map.get("two").getPassword());
            return "success";
        }
    }

    前台输入信息:

    后台显示结果:

  • 相关阅读:
    使用命令行管理virtualBox
    springboot activiti 整合项目框架源码 shiro 安全框架 druid 数据库连接池
    activiti工作流的web流程设计器整合视频教程 SSM 和 独立部署
    java springMVC SSM 操作日志 4级别联动 文件管理 头像编辑 shiro redis
    MVC、MVP、MVVM 模式对比
    GoBelieve IM 消息推送的方案
    Token生成(转载)
    ios的framework合并
    Gobelieve 架构(转载)
    xcode10不兼容问题解决方法,framework编译脚本
  • 原文地址:https://www.cnblogs.com/xiaolongSunny/p/4651456.html
Copyright © 2011-2022 走看看