Struts2的数据封装:Struts2是一个web层框架,框架是软件的半成品。提供了数据封装的基本功能。

注:Struts2底层(核心过滤器里面的默认栈里面的拦截器,具体见struts-default.xml)完成了参数的接收、封装、类型转换的功能。例如字符串与基本类型转换的功能

分类:

属性驱动:提供属性set方法的方式(基本不用)、页面中提供表达式方式(实际上是内部提供的OGNL表达式)

模型驱动(最常用):采用模型驱动方式,通过实现一个模型驱动的接口ModelDriven,手动提供对象的实例private User user = new User();

区别:

  • 属性驱动:向多个数据封装中同时封装数据
  • 模型驱动:  最常用的方式,缺点:只能同时向一个对象中封装数据

提供属性set方法的方式(不常用,缺点是需要自己往对象里面封装) :

这种方式很少用,除非数据少,需要在Action类里自己往对象里封装

前端jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
 <h1>Struts2的数据封装</h1>
 <h3>方式一:属性驱动:提供Set方法的方式</h3>
 <form action="${pageContext.request.contextPath }/userAction1.action" method="post">
 用户名:<input type="text" name="username"><br/>
 密码:<input type="password" name="password"><br/>
 年龄:<input type="text" name="age"><br/>
 生日:<input type="text" name="birthday"><br/>
 工资:<input type="text" name="salary"><br/>
 <input type="submit" value="提交">
 </form>
 </body>
 </html>

实体类User:

package com.itheima.struts2.domain;
/**
 * 实体对象
 */
import java.util.Date;

public class User {
   private String username;
   private String password;
   private int age;
   private Date birthday;
   private Double salary;

public void setUsername(String username) {
    this.username = username;
}
public void setPassword(String password) {
    this.password = password;
}
public void setAge(int age) {
    this.age = age;
}
public void setBirthday(Date birthday) {
    this.birthday = birthday;
}
public void setSalary(Double salary) {
    this.salary = salary;
}

@Overridepublic String toString() {    return "User [username=" + username + ", password=" + password + ", age=" + age + ", birthday=" + birthday            + ", salary=" + salary + "]";}   

}

Action类:

 package com.itheima.struts2.demo2;

 import java.util.Date;

 import com.itheima.struts2.domain.User;
 import com.opensymphony.xwork2.ActionSupport;
 /**
  * 数据封装的方式一:属性封装
  *
  */
 public class UserAction1 extends ActionSupport {
    private String username;
    private String password;
    private Integer age;
    private Date birthday;
    private Double salary;

 public void setUsername(String username) {
     this.username = username;
 }

 public void setPassword(String password) {
     this.password = password;
 }

 public void setAge(Integer age) {
     this.age = age;
 }

 public void setBirthday(Date birthday) {
     this.birthday = birthday;
 }

 public void setSalary(Double salary) {
     this.salary = salary;
 }

     public String execute() throws Exception{
        //接收数据
         System.out.println(username);
         System.out.println(password);
         System.out.println(age);
         System.out.println(birthday);
         System.out.println(salary);
        //封装数据
         User user = new User();
         user.setAge(age);
         user.setUsername(username);
         user.setBirthday(birthday);
         user.setSalary(salary);

        return NONE;
    }
 }

属性驱动:提供页面表达式的方式

前端JSP:name属性前加上实体类的对象前缀,如:user.password

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
 <h1>Struts2的数据封装</h1>
 <h3>方式二:属性驱动:提供页面表达式方式</h3>
 <form action="${pageContext.request.contextPath }/userAction2.action" method="post">
 用户名:<input type="text" name="user.username"><br/>
 密码:<input type="password" name="user.password"><br/>
 年龄:<input type="text" name="user.age"><br/>
 生日:<input type="text" name="user.birthday"><br/>
 工资:<input type="text" name="user.salary"><br/>
 <input type="submit" value="提交">
 </form>
 </body>
 </html>

Action类:相较而言,这种直接封装对象,而不是操作属性的方式,比较简约

package com.itheima.struts2.demo2;

import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 数据封装方式二:属性驱动
 * @author 李腾
 *
 */
public class UserAction2 extends ActionSupport {
    //提供一个User对象,在前端页面的name属性上加上user前缀
    private User user;
    //提供user的get和set方法,一定要提供get方法
    //因为拦截器提供对象的封装,需要创建User对象,前提是前端属性名字的前缀和类一样
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }

    public String execute() throws Exception{
        System.out.println(user);
        return NONE;
    }

}

模型驱动:采用模型驱动的方式(最常用)

Action类实现ModelDriven接口,重写getModel方法,返回实体对象

缺点:只能向一个对象(比如user)中封装对象。而页面表达式可以向多个对象封装(只需改变name属性的前缀如student.username user.username )

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
 <h1>Struts2的数据封装</h1>
 <h3>方式三:模型驱动方式</h3>
 <form action="${pageContext.request.contextPath }/userAction3.action" method="post">
 用户名:<input type="text" name="username"><br/>
 密码:<input type="password" name="password"><br/>
 年龄:<input type="text" name="age"><br/>
 生日:<input type="text" name="birthday"><br/>
 工资:<input type="text" name="salary"><br/>
 <input type="submit" value="提交">
 </form>
 </body>
 </html>

Action类:

package com.itheima.struts2.demo2;

import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 * 数据封装方式三:模型驱动  最常用的方式,缺点:只能同时向一个对象中封装数据
 *          二:向多个数据封装中同时封装数据
 * @author 李腾
 *
 */
public class UserAction3 extends ActionSupport  implements ModelDriven<User>{
    //前提:手动实例化User
    private User user = new User();

    //模型驱动需要使用的方法
    @Override
    public User getModel() {
        return user;
    }

    public String execute() throws Exception{
        System.out.println(user);
        return NONE;
    }

}

十一 三种Struts2的数据封装方式,封装页面传递的数据的更多相关文章

  1. 九 三种Struts2访问Servlet方式总结

    Servlet是单例的,Action是多例的. 多个程序访问Servlet只会创建一个Servlet对象,多个程序访问Action会创建对应的多个Action对象. 跳转页面可以获取对象的属性,说明使 ...

  2. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

  3. 三种Singleton的实现方式

    来源:http://melin.iteye.com/blog/838258 三种Singleton的实现方式,一种是用大家熟悉的DCL,另外两种使用cas特性来实现. public class Laz ...

  4. Objective-C:三种文件导入的方式以及atomic和nonatomic的区别

    一.三种文件导入的方式比较:   类的前项声明@class.import.include: 1.采用@class 类名的方式,它会告诉编译器有这么一个类,目前不需要知道它内部的实例变量和方法是如何定义 ...

  5. 浅淡Webservice、WSDL三种服务访问的方式(附案例)

    Webservice Webservice是使应用程序以与平台和编程语言无关的方式进行相互通信技术. eg:站点提供访问的数据接口:新浪微博.淘宝. 官方解释:它是一种构建应用程序的普遍模型,可以在任 ...

  6. 三种root的修补方式

    三种root的修补方式 system/core/adb/abd.c adbd漏洞,请看abd.c的第917行/* then switch user and group to "shell&q ...

  7. 三种Tomcat集群方式的优缺点分析

    三种Tomcat集群方式的优缺点分析 2009-09-01 10:00 kit_lo kit_lo的博客 字号:T | T 本文对三种Tomcat集群方式的优缺点进行了分析.三种集群方式分别是:使用D ...

  8. python列表和字符串的三种逆序遍历方式

    python列表和字符串的三种逆序遍历方式 列表的逆序遍历 a = [1,3,6,8,9] print("通过下标逆序遍历1:") for i in a[::-1]: print( ...

  9. Objective-C:三种文件导入的方式比较

    三种文件导入的方式比较:   类的前项声明@class.import.include:   1.采用@class 类名的方式,它会告诉编译器有这么一个类,目前不需要知道它内部的实例变量和方法是如何定义 ...

随机推荐

  1. yii2 components

    components顾名思义就是组件的意思,yii默认会根据 components 数组里面的键值去 vendor\yiisoft\yii2\web 里面查找这个键值得类,如果没有找到,再根据这个键值 ...

  2. Codeforces Round #577 (Div. 2) 题解

    比赛链接:https://codeforc.es/contest/1201 A. Important Exam 题意:有\(n\)个人,每个人给出\(m\)个答案,每个答案都有一个分值\(a_i\), ...

  3. Django框架之ORM的相关操作(二)

    模型类: class Commongity(models.Model): id=models.AutoField(primary_key=True) name=models.CharField(max ...

  4. 本机修改虚拟机linux中的代码文件

    最近在研究swoole这个框架,好不容易装了一个swoole,为了开发方面,需要早宿主机和虚拟机之间文件共享,一开始使用vmware tool可以实现共享,但是只能在linux中看到win共享的文件, ...

  5. 攻防世界 你知道php备份文件吗?

    题目地址:https://adworld.xctf.org.cn/task/answer?type=web&number=3&grade=0&id=5064 php的备份有两种 ...

  6. 包、logging模块、hashlib模块、openpyxl模块、深浅拷贝

    包.logging模块.hashlib模块.openpyxl模块.深浅拷贝 一.包 1.模块与包 模块的三种来源: 1.内置的 2.第三方的 3.自定义的 模块的四种表现形式: 1.py文件 2.共享 ...

  7. Smart License

    思科启动了通过构建思科智能软件管理器门户来简化客户许可管理的计划. 它可以帮助客户了解他们购买的许可证以及他们使用的许可证. 其他各种思科产品已经启用Smart Enabled,随着此版本(我这里学习 ...

  8. Django ORM中的模糊查询

    ORM映射 什么是ORM映射?在笔者认为就是对SQL语句的封装,所写语句与SQL对应语句含义相同,使开发更加简单方便,不过也是存在弊端的,使程序运行效率下降.例如: UserInfo.objects. ...

  9. Codeforces AIM Tech Round 5 (rated, Div. 1 + Div. 2)

    A. Find Square time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...

  10. 第八届极客大挑战 Re

    0x01.Writeup-RE-CM_2 题目: 解题思路: 1.这个是经过xor的,王老师提示说用xortool,于是放进kali,装好之后执行 xortool CM_2.exe -b, 0.out ...