我爱编程Struts2

struts封装获取表单数据方式

2018-05-25  本文已影响0人  小漫画ing

原始方式获取表单封装到实体类对象:

package com.xuman.doudou;

public class User {
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User [password=" + password + ", address=" + address + ", username=" + username + "]";
    }
    private String password;
    private String address;
    private String username;

}

在上面的代码中,@override这个是为了输入结果方便写的,创建了一个tostring的方式,并且返回这个方法中的值,在实现类中会调用这个方法。

package com.xuman.doudou;

import javax.enterprise.inject.New;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.xuman.doudou.User;

import com.opensymphony.xwork2.ActionSupport;
public class from4 extends ActionSupport {
    @Override
    public 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;
    }
    
}

后面的输入语句中的user就是调用了user类的方法的返回值。

    <package name="demo2" namespace="/" extends="struts-default">
<!-- <action name="from1" class="com.xuman.from.from1"></action> -->
        <action name="from4" class="com.xuman.doudou.from4"></action>
    </package>

下面我们就来运行一下结果:


提交数据
运行结果

属性封装操作:

<package name="demo2" namespace="/" extends="struts-default">
<!-- <action name="from1" class="com.xuman.from.from1"></action> -->
        <action name="from5" class="com.xuman.doudou.from5"></action>
    </package>

我这里写的from5,是因为我的实现类的命名是from5

package com.xuman.doudou;

import com.opensymphony.xwork2.ActionSupport;

public class from5 extends ActionSupport {
    //1定义变量
    //变量的名称和表单输入项name属性值一样
    private String username;
    private String password;
    private String address;
    //2生成变量的set和get的方法
    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    
    
    @Override
    public String execute() throws Exception {
        System.out.println(username+password+address);
        return NONE;
    }

}

模型驱动封装

<package name="demo1" namespace="/" extends="struts-default">
    <action name="Model" class="com.itcast.manman.Model"></action>
</package>

这里要注意配置文件的命名,最好是struts.xml。否则会报错。

<body>
    <form action="${pageContext.request.contextPath}/Model.action"  method="post">
        username:<input type="text" name="username"><br><br>
        password:<input type="text" name="password"><br><br>
        address:<input type="text" name="address"><br><br>
        <button type="submit">登录</button>//注意
    </form>
  </body>
package com.itcast.manman;

public class User {
    private String username;
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + ", address=" + address + "]";
    }
    private String password;
    private String address;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    

}

package com.itcast.manman;

import javax.enterprise.inject.New;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
 * 使用模型驱动获取表单数据
 */
public class Model extends ActionSupport implements ModelDriven<User>{
    //创建对象
    //前提要求:表单输入项name属性值和实体类属性名称一样
    private User user =  new User();
    public User getModel() {
        // 返回创建user对象
        
        return user;
    }
    
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        System.out.println(user);
        return NONE;
    }
}

这里我们使用的是继承ActionSupport这个类,并且实现一个接口ModelDriven<User>,这里的尖括号里面的内容是封装类,还要重写getModel()这个方法。具体的注释在代码里面。

表达式封装:

1、实现过程:
(1)使用表达式封装可以把表单数据封装到实体类对象里面。

package com.itcast.manman1;

import com.opensymphony.xwork2.ActionSupport;

public class demo extends ActionSupport{
//声明实体类
    private User user;
    //声明实体类的get,set方法
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        System.out.println(user);
        return SUCCESS;
    }
}

<form action="${pageContext.request.contextPath}/demo.action"  method="post">
        username:<input type="text" name="user.username"><br><br>
        password:<input type="text" name="user.password"><br><br>
        address:<input type="text" name="user.address"><br><br>
        <button type="submit">登录</button>
    </form>
<action name="demo" class="com.itcast.manman1.demo" method="execute">
        <result name="success">/index.jsp</result>
    </action>

当成功的时候,让他跳转到index.jsp页面,运行结果如下:


输入数据
运行结果

图二是我运行后的获取到的数据,以及跳转到的页面,页面内容我原本设置的就是上面这句话。

上一篇下一篇

猜你喜欢

热点阅读