Struts2学习3——数据绑定及获取Session
来源:互联网 发布:讯佳摇杆淘宝 编辑:程序博客网 时间:2024/06/08 17:24
接收表单参数
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; }}
0 0
- Struts2学习3——数据绑定及获取Session
- Struts2向对象传递数据及Struts2中怎么获取request,response,session详解(转)
- PHP学习笔记——表单数据获取,Session,Cookie
- struts2获取Session对象中数据
- Struts2 设置session及获取spring aplicationicontext
- CheckBoxList 的数据绑定及数据获取
- Struts2 在action中获取模拟session用于存储数据
- struts2 获取session
- struts2 jsp 获取 Session
- Struts2 获取 Session对象
- 【Struts2】Session的获取
- struts2 select绑定数据
- DataGrid数据绑定及学习笔记
- struts2 session 3(利用request获取session)
- struts2学习笔记--获取Session和request方法
- struts2学习笔记(四)--获取Session和request方法
- Struts2学习(第六篇)——获取servletAPI和封装表单数据
- Struts2学习第二天——获取参数与数据校验
- 软件测试用例设计原则
- c#图像处理
- was起停顺序
- 神经网络入门 ,源码6
- genprog svn 1688 操作指南
- Struts2学习3——数据绑定及获取Session
- 神经网络入门 ,源码7
- wcscpy wcscpy_s strcpy strcpy_s的区别
- 解决Minimum supported Gradle version is 3.3. Current version is 2.14.1问题
- 欢迎使用CSDN-markdown编辑器
- ac自动机及相关dp
- 神经网络入门 ,源码8
- Ubuntu 系统
- 导入csv文件时,由于可能丢失数据,所以无法转换该值