欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Struts2封装获取表单数据方式

程序员文章站 2022-07-14 10:34:39
...

使用原始方式获取表单到实体类对象
publicclassForm5DemoActionextendsActionSupport {@Overridepublic String execute() throws Exception {

           HttpServletRequest request=ServletActionContext.getRequest();

              String username=request.getParameter("username");

              String password=request.getParameter("password");

              String address=request.getParameter("address");

              User user=new User();

              user.setUsername(username);
              user.setPassword(password);
              user.setAddress(address);

              System.out.println(user);

                return NONE;
            }
}
struts.xml中
<actionname="form5"class="cn.itcast.form.Form5DemoAction"></action>
<%@ page language="java" c>"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><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><formaction="${pageContext.request.contextPath}/form5.action"method="post">
              username:<inputtype="text"name="username"/><br/>
              password:<inputtype="text"name="password"/><br/>
              address:<inputtype="text"name="address"/><br/><inputtype="submit"value="提交"/></form></body></html>
填写jsp中的表单提交 控制台打印出如下效果
Struts2封装获取表单数据方式
属性封装
直接把表单提交属性封装到action的属性里面
实现步骤
在action成员变量位置 变量名称和表单输入项的name属性值一样
生成变量的set方法(set和get方法都写出来)
用属性封装获取表单数据到属性里面 不能把数据直接封装到实体类对象里面
publicclassData1DemoActionextendsActionSupport {private String username;
             private String password;
             private String address;


            public String getUsername() {
                return username;
            }


            publicvoidsetUsername(String username) {
                this.username = username;
            }


            public String getPassword() {
                return password;
            }


            publicvoidsetPassword(String password) {
                this.password = password;
            }


            public String getAddress() {
                return address;
            }


            publicvoidsetAddress(String address) {
                this.address = address;
            }


            @Overridepublic String execute() throws Exception {
                System.out.println(username+" "+password+" "+address);

                return NONE;
            }
}
struts.xml
<actionname="data1"class="cn.zhoushicanyin.date.Data1DemoAction"></action>
<%@ page language="java" c>"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><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><formaction="${pageContext.request.contextPath}/data1.action"method="post">
              username:<inputtype="text"name="username"/><br/>
              password:<inputtype="text"name="password"/><br/>
              address:<inputtype="text"name="address"/><br/><inputtype="submit"value="提交"/></form></body></html>
填写jsp中的表单提交 控制台打印出如下效果
Struts2封装获取表单数据方式
模型驱动封装
使用模型驱动方式 可以直接把表单数据封装到实体类对象里面
实现步骤:
action实现接口 ModelDriver
实现接口里面的方法 getModel方法 把创建对象返回
在action里面创建实体类对象
使用模型驱动和属性封装注意问题
在一个action中 获取表单数据可以属性封装 也可以使用模型驱动 但不能同时使用属性封装和模型驱动封装获取同一个表单数据 如果同时使用 只会执行模型驱动
publicclass Data2DemoAction extends ActionSupport implements ModelDriven<User>{

    private User user=new User();

    private String username;
    private String password;
    private String address;



    public User getUser() {
        return user;
    }

    publicvoidsetUser(User user) {
        this.user = user;
    }

    public String getUsername() {
        return username;
    }

    publicvoidsetUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    publicvoidsetPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    publicvoidsetAddress(String address) {
        this.address = address;
    }

    @Override
    public User getModel() {
        return user;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(user);//模型驱动封装和属性封装同时进行用时 只会执行模型驱动封装
        System.out.println(username);
        System.out.println(password);
        System.out.println(address);
        return NONE;
    }

}
struts.xml
<actionname="data2"class="cn.zhoushicanyin.date.Data2DemoAction"></action>
<%@ page language="java" c>"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><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><formaction="${pageContext.request.contextPath}/data2.action"method="post">
              username:<inputtype="text"name="username"/><br/>
              password:<inputtype="text"name="password"/><br/>
              address:<inputtype="text"name="address"/><br/><inputtype="submit"value="提交"/></form></body></html>
填写jsp中的表单提交 控制台打印出如下效果
Struts2封装获取表单数据方式
表达式封装
实现过程
在action里面声明实体类
生成实体类变量的set和get方法
在表单输入项的name属性值里面写表达式形式
把表达式封装归类到属性封装里面
publicclassData3DemoActionextendsActionSupport{private User User;

    private Book book;


    public Book getBook() {
        return book;
    }


    publicvoidsetBook(Book book) {
        this.book = book;
    }


    public User getUser() {
        return User;
    }


    publicvoidsetUser(User user) {
        User = user;
    }


    @Overridepublic String execute() throws Exception {
        System.out.println(book.getNbook());
        System.out.println(User);  
        return NONE;
        }
}
相关标签: @Override