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. PHP通过thrift2访问HBASE

    前一段时间需要在网页上显示HBASE查询的结果,考虑用PHP来实现,在网上搜了一下,普遍都是用thrift作为接口来实现的.​ 参考博文:​ http://www.cnblogs.com/scotom ...

  2. Go递归函数

    package main import "fmt" func main() { /* 递归函数(recursion):一个函数自己调用自己,就叫做递归函数. 递归函数要有一个出口, ...

  3. 面向对象--OO--object-oriented

    如何把大象装冰箱? 面向过程:打开冰箱门---把大象装进去---关上冰箱门 面向对象: 1.大象:进入冰箱.离开冰箱 2.冰箱:开门.关门 3.人:检测1.检测2 面向对象三大特性:封装.继承.多态 ...

  4. PHP基础学习笔记1

    一.基本语法 1.1 形式 PHP 脚本以 <?php 开始,以 ?> 结束: <?php //php代码 ?> 1.2 注释 单行注释 //这是单行注释 多行注释 /* 这是 ...

  5. 基于jmeter+ant实现的接口自动化测试

    jmeter+ANT接口自动化测试框架 项目说明 本框架是一套基于jmeter+Ant+Excel+Python而设计的数据驱动接口自动化测试框架,jmeter 作为执行器,Ant 作为构建工具,进行 ...

  6. GIT-maven

     maven 一:什么是maven 1.maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的创建,报告和文档的软件项目管理工具. 2.maven是跨平台的项目管理工具,主要服务于 ...

  7. 《gPRC使用protobuf构建微服务》阅读笔记

    首先我需要去了解一些概念,根据百度百科了解到: l  微服务架构:微服务架构是一项在云中部署应用和服务的新技术.微服务可以在“自己的程序”中运行,并通过“轻量级设备与HTTP型API进行沟通”. l  ...

  8. Redis注册成服务

    注册服务 redis-server.exe –-service-install redis.windows.conf 删除服务 redis-server –-service-uninstall 开启服 ...

  9. Linux下编译并使用miracl密码库

    参考:http://blog.sina.com.cn/s/blog_53fdf1590102y9ox.html MIRACL(Multiprecision Integer and RationalAr ...

  10. GTA5整合包

    链接:https://pan.baidu.com/s/1WUvLMyTcQYsw3wi6OfJfJA 提取码:jcpm