无人机中的城堡 2017-10-15
struts的数据封装共有3中方式,属性封装,模型驱动封装和表达式封装,其中表达式封装为常用
一:属性封装:
属性封装其实就是自己定义变量,注意变量名要和表单的name属性名一致,然后生成get和set的方法就可以了,使用的话直接自己用属性名就可以了。
二:模型驱动封装:
01):创建实体类(属性名必须和表单的name属性值一模一样)
02):action实现ModelDriven接口
03):实现ModelDriven接口的getModel方法
private 实体类 实体类对象名 = new 实体类();
public 实体类 getModel() {
return 实体类对象名;
}
04):使用实体类对象名即可
05):下面是我自己写的代码
action类:
public class LoginAction extends  ActionSupport  implements  ModelDriven<ELogin>{
    
    @Override
    public String execute() throws Exception {
        return super.execute();
    }
    
    public String admin(){
        System.out.println(elogin.getUname()+"---"+elogin.getUpwd());
        return SUCCESS;
    }
    
    private ELogin elogin=new ELogin();
    @Override
    public ELogin getModel() {
        return elogin;
    }
}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>
<form action="loginadmin.action"  method="post">
用户名:<input  name="uname"><br>
密码:<input  name="upwd"  type="password"><br>
<input  type="submit"  value="提交">
</form>
</body>
</html>三:表达式封装:
01):01:创建实体类(属性名必须和表单的name属性值一模一样)
02):private 实体类 实体类对象名;
03):生成实体类对象名的set和get的方法
04):修改表单那么属性值:name="实体对象名.属性名"
这是action类:
public class Login2Action extends ActionSupport {
    private ELogin eLogin;
    public ELogin geteLogin() {
        return eLogin;
    }
    public void seteLogin(ELogin eLogin) {
        this.eLogin = eLogin;
    }
    public String admin() {
        System.out.println(eLogin.getUname() + "---" + eLogin.getUpwd());
        return SUCCESS;
    }
}这是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>
<form action="loginadmin.action"  method="post">
用户名:<input  name="elogin.uname"><br>
密码:<input  name="elogin.upwd"  type="password"><br>
<input  type="submit"  value="提交">
</form>
</body>
</html>注意:表达式封装与模型驱动封装不可同时在一个action类中使用,否则框架只会用模型驱动封装
ELogin实体类:
public class ELogin {
    private String uname;
    private String upwd;
    public ELogin() {
        super();
    }
    public ELogin(String uname, String upwd) {
        super();
        this.uname = uname;
        this.upwd = upwd;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
}表达式封装和模型封装的相同点以及不同点:
相同点:都可以把数据封装到实体类中
不同点:模型封装只能够把数据封装到一个实体类中,在一个action里面只能实现一次ModelDriven的接口所以模型封装只会封装一个到一个实体类中
表达式封装可以吧数据封装到多个对象中
四:ognl表达式写法:导入struts2的标签库(<%@ taglib uri="/struts-tags" prefix="s"%>)