您的位置:首页 > 编程语言 > Java开发

Struts2(四) 接受参数的三种方法

2016-12-24 22:20 381 查看
简述:Struts2 接收参数有三种方法,Action、domainModel、modelDriven

1、dto:数据传输对象(DTO)(Data Transfer Object),是一种设计模式之间传输数据的软件应用系统。数据传输目标往往是数据访问对象从数据库中检索数据。数据传输对象与数据交互对象或数据访问对象之间的差异是一个以不具有任何行为除了存储和检索的数据(访问和存取器)。

2、struts2 中会自动的将参数赋值到对象中。

一、方法一:Action接受参数

1.1、请求地址

<a href="<%=path %>/testgetParam?name=flx&age=22">使用Action的属性接受参数</a>


1.2、Action 类

package com.flx.actions;

import com.flx.domain.Users;
import com.opensymphony.xwork2.ActionSupport;

/**
*
* @author FuLX
*
* @2016-12-24下午9:15:16
*
* 接收参数方法:
* 1、Action获取请求参数。
* 2、域模型接收参数【domainModel】
*/
public class GetParamAction extends ActionSupport {
private static final String FINAL_FLX_002 = "getParam_001";

public String getParam_02() {
System.out.println("domainModel接收name = " + user.getName());
System.out.println("domainModel接收age = " + user.getAge());
return FINAL_FLX_002;
}

//1、使用Action接收参数
private String name;
private int age;

public String getParam() {
System.out.println("name = " + name);
System.out.println("age = " + age);
return FINAL_FLX_002;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}


1.3、输出结果

name = flx
age = 22


二、方法二:domainModel接受参数

2.1、请求连接

<a href="<%=path %>/testgetParam_02?user.name=flx&user.age=22">使用domainModel的属性接受参数</a>


2.2、模型域对象

public class Users {

private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}


2.3、Action处理类

package com.flx.actions;

import com.flx.domain.Users;
import com.opensymphony.xwork2.ActionSupport;

/**
*
* @author FuLX
*
* @2016-12-24下午9:15:16
*
* 接收参数方法:
* 1、Action获取请求参数。
* 2、域模型接收参数【domainModel】
*/
public class GetParamAction extends ActionSupport {
private static final String FINAL_FLX_002 = "getParam_001";

private Users user;

public Users getUser() {
return user;
}

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

public String getParam_02() {
System.out.println("domainModel接收name = " + user.getName());
System.out.println("domainModel接收age = " + user.getAge());
return FINAL_FLX_002;
}

}


2.4、结果

domainModel接收name = flx
domainModel接收age = 22

一、方法三:ModelDriven接受参数

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: