在struts2中主要有以下几种方法可以获取表单提交的数据
- 使用ActionContext类获取
- 使用ServletActionContext类获取
- 使用接口(ServletRequestAware)注入方式
- 属性封装
- 模型驱动封装
- 表达式封装
JSP主要代码:
GetFormDataAction主要代码:
public String execute() {
ActionContext context = ActionContext.getContext();
Map params = context.getParameters();
Set keys = params.keySet();
for(String key : keys) {
// 返回数组类型
Object[] obj = (Object[]) params.get(key);
System.out.println(key+"="+Arrays.toString(obj));
}
return NONE;
}
struts.xml中的主要配置(后面5个同此配置):
<package name="demo" namespace="/" extends="struts-default">
<action name="getFormData" class="action.GetFormDataAction">action>
package>
JSP代码无需修改
修改GetFormDataAction类中的execute方法:
public String execute() {
// 先获取request对象,通过request对象获取参数
HttpServletRequest request = ServletActionContext.getRequest();
String username = request.getParameter("username");
String password = request.getParameter("password");
String[] habits = request.getParameterValues("habits");
System.out.println("[username:"+username+"]"+"[password:"+password+"]"+"[habits:"+Arrays.toString(habits)+"]");
return NONE;
}
JSP中代码无需修改
修改GetFormDataAction类,使此类实现ServletRequestAware接口
package action;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.tribes.util.Arrays;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class GetFormDataAction extends ActionSupport implements ServletRequestAware {
// 通过接口注入方式直接得到request对象
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public String execute() {
String username = request.getParameter("username");
String password = request.getParameter("password");
String[] habits = request.getParameterValues("habits");
System.out.println("[username:"+username+"]"+"[password:"+password+"]"+"[habits:"+Arrays.toString(habits)+"]");
return NONE;
}
}
修改GetFormDataAction类,添加属性和属性的get和set方法
package action;
import java.util.Arrays;
import com.opensymphony.xwork2.ActionSupport;
public class GetFormDataAction extends ActionSupport{
private String username;
private String password;
private String[] habits;
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[] getHabits() {
return habits;
}
public void setHabits(String[] habits) {
this.habits = habits;
}
public String execute() {
System.out.println("[username:"+username+"]"+"[password:"+password+"]"+"[habits:"+Arrays.toString(habits)+"]");
return NONE;
}
}
以上四种方法都只是获取单独的值,若想封装到对象中则需要自己进行处理,下面的两种方法可以直接将表单的值封装到实体对象中去。
新建一个类User
package entity;
import java.util.Arrays;
public class User {
private String username;
private String password;
private String[] habits;
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[] getHabits() {
return habits;
}
public void setHabits(String[] habits) {
this.habits = habits;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", habits=" + Arrays.toString(habits) + "]";
}
}
修改GetFormDataAction类,使该类实现ModelDriven接口,泛型为我们定义的User,并在该类中定义一个User的对象。
package action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import entity.User;
public class GetFormDataAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
return user;
}
public String execute() {
System.out.println(user);
return NONE;
}
}
修改输入框的name,在原来的name值前面加上action定义的对象名称
package action;
import com.opensymphony.xwork2.ActionSupport;
import entity.User;
public class GetFormDataAction extends ActionSupport{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() {
System.out.println(user);
return NONE;
}
}
上面两种方法可以将表单数据封装到一个对象中去,但是如果在一个页面需要提交两个及以上用户的信息,那么就需要将数据封装list或map集合中去
package action;
import java.util.Arrays;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import entity.User;
public class GetFormDataAction extends ActionSupport{
private List users;
public List getUsers() {
return users;
}
public void setUsers(List users) {
this.users = users;
}
public String execute() {
System.out.println(Arrays.toString(users.toArray()));
return NONE;
}
}
然后修改jsp文件,新增一组用户信息的输入框,并修改输入框的name属性值
package action;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionSupport;
import entity.User;
public class GetFormDataAction extends ActionSupport{
private Map users;
public Map getUsers() {
return users;
}
public void setUsers(Map users) {
this.users = users;
}
public String execute() {
Set keys = users.keySet();
for(String key : keys) {
System.out.println(key+"="+users.get(key));
}
return NONE;
}
}
然后修改jsp文件