Struts2学习3——数据绑定及获取Session

本文介绍如何在Struts2框架中通过不同方式接收表单提交的数据,包括直接定义属性、封装为类以及实现ModelDriven接口的方法。

接收表单参数


1. 在Action中定义表单属性

<form action="login" method="post" name="form">
User:<s:textfield name="username"/><br/>
Password:<s:password name="password"/><br/>
<s:submit value="提交"/> 
</form> 

在Action中定义

    private String username;
    private String password;
    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;
    }

2. 表单参数封装成类

<form action="login" method="post" name="form"> 
User:<s:textfield name="user.username"/><br/>
Password:<s:password name=" user.password"/><br/>
<s:submit value="提交"/> 
</form> 

Model类


public class test {
    private String username;
    private String password;
    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;
    }

}

在Action中定义Model对象

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

3. 实现ModelDriven接口

表单及Model、Action中User定义与上面相同,对Action类:

public class loginAction extends ActionSupport implements ModelDriven<User>{
   User user=new User();
   ...
   public Users getModel()
   {
      return user;
   }
}

在View显示Action中的变量

在Action定义

    private String username;
    private String password;
    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;
    }

然后在jsp中,

 <%@ taglib prefix="s" uri="/struts-tags" %>
 <s:property value="username" />
 或

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

获取Session

方法1

在前一篇文章已有介绍,这里简要再记录一下

    private RbacAdmins rbacAdmin;

    public RbacAdmins getRbacAdmin() {
        return rbacAdmin;
    }

    public void setRbacAdmin(RbacAdmins rbacAdmin) {
        this.rbacAdmin = rbacAdmin;
    }
    @Override
    public String execute() throws Exception {
        ActionContext actionContext = ActionContext.getContext();
        Map session = actionContext.getSession();
        rbacAdmin =(RbacAdmins)session.get(Session.ADMIN);      
        return "APP";
    }

方法2(代码未测试)

    import java.util.Map;
    import org.apache.struts2.interceptor.SessionAware;
    import com.opensymphony.xwork2.ActionSupport;

    public class SessionTest1Action extends ActionSupport implements SessionAware {
    private Map session;
    public void setSession(Map session) {
        this.session = session;
    }

    public String execute() {
        this.session.put("USER_NAME", "Test User 1");
        return SUCCESS;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI星球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值