struts2中action接收参数的方法
来源:互联网 发布:淘宝的营销词 编辑:程序博客网 时间:2024/05/21 10:09
struts2中action接收参数的方法
http://hi.baidu.com/yangguang_huoyunshen/blog/item/a18ed600044ed010738da59b.html
纯属转载:便于记忆
Struts2中Action接收参数的方法主要有以下三种:
Struts2中Action接收参数的方法主要有以下三种:
1.使用Action的属性接收参数:
a.定义:在Action类中定义属性,创建get和set方法;
b.接收:通过属性接收参数,如:userName;
c.发送:使用属性名传递参数,如:user1!add?userName=Magci;
2.使用DomainModel接收参数:
a.定义:定义Model类,在Action中定义Model类的对象(不需要new),创建该对象的get和set方法;
b.接收:通过对象的属性接收参数,如:user.getUserName();
c.发送:使用对象的属性传递参数,如:user2!add?user.userName=MGC;
3.使用ModelDriven接收参数:
a.定义:Action实现ModelDriven泛型接口,定义Model类的对象(必须new),通过getModel方法返回该对象;
b.接收:通过对象的属性接收参数,如:user.getUserName();
c.发送:直接使用属性名传递参数,如:user2!add?userName=MGC
struts2 Action获取表单传值
1.通过属性驱动式
JSP:
<form action=
"sys/login.action"
method=
"post"
>
<input type=
"text"
name=
"username"
>
<input type=
"submit"
value=
"submit"
>
</form>
Action:直接通过get、set方法获取。
public
class
sysAction
extends
ActionSupport{
private
String username;
public
String login()
throws
Exception {
System.out.println(username);
return
SUCCESS;
}
public
String getUsername() {
return
username;
}
public
void
setUsername(String username) {
this
.username= username;
}
}
2.模型驱动方式,必须要实现ModelDriven<T>接口。对于要传入多个model第二种方式不方便
JSP: <form action="sys/login.action" method="post"> <input type="text" name="username"> <input type="submit" value="submit"> </form>
Action:必须实现getModel() 方法
public class sysAction extends ActionSupport implements ModelDriven<User>{ private User user; public String login() throws Exception { System.out.println(getModel().getUsername()); return SUCCESS; } public User getModel() { if (null == user) { return user = new User(); } return user; }}
3.第三种方式可以完全不实现ModelDriven<T>,也可使用多个model对象的属性。
JSP: <form action="sys/login.action" method="post"> <input type="text" name="user.username"> <input type="text" name="teacher.level"> <input type="submit" value="submit"> </form>
Action: 必须提供set方法
public class sysAction extends ActionSupport{ private User user; private Teacher teacher; public String login() throws Exception { System.out.println(user.getUsername()); System.out.println(teacher.getLevel()); return SUCCESS; } public void setUser(User user) { this.user = user; } public void setTeacher(Teacher teacher) { this.teacher = teacher; }}
- Struts2中Action接收参数的方法
- Struts2中Action接收参数的方法
- Struts2中Action接收参数的方法
- Struts2中Action接收参数的方法
- struts2中action接收参数的方法
- struts2中action接收参数的方法
- struts2中action接收参数的方法
- struts2中action接收参数的方法
- struts2中action接收参数的方法
- struts2中action接收参数的方法
- Struts2中Action接收参数的方法
- struts2中Action接收参数的方法
- struts2中action接收参数的方法
- Struts2中Action接收参数的方法
- struts2中action接收参数的方法
- struts2中action接收参数的方法
- Struts2中Action接收参数的方法
- struts2中action接收参数的方法
- 郭宇:Airpub - 纯前端博客引擎实践
- jvm的内存模型之eden区
- Linux节点之间互相拷贝文件或目录报错
- 5.5.1 Lambda 函数
- uiview viewwithtag
- struts2中action接收参数的方法
- C++中异常处理的语法
- 关于使用Artery插件的一些积累2
- 一个资深面试官的测试工程师招聘心得
- 安装IIS7
- 解决Android SDK 下载太慢
- 发布数据库路径不对的问题. access
- jvm内存区域划分 (解释的很形象)
- autocomplete插件