struts2的Action类一般都会继承(extends)ActionSupport类(Action类,即实现action的类)
1.前传后:jsp里<input type="text" name="username"/>,后台Action类里有变量private String username;及其get(),set()方法即可得到
此时,后传前:
Action类:
1)com.opensymphony.xwork2.ActionContext;
定义:ActionContext context=ActionContext.getContext();
context.put("username", username);
前台用:${username}即可得到其值;
2)javax.servlet.http.HttpServletRequest
HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
request.setAttribute("message", "通过ServletRequestAware接口实现了访问Servlet API");
前台取值:${requestScope.message}
3)ServletActionContext.getRequest().setAttribute("message", "通过ServletActionContext接口实现了访问Servlet API");
前台取值:${requestScope.message}

2.前台传值:<input type="text" name="user.userName"/>
Action类:(User类与前台传的所有变量名相同)
private User user;
public User getUser() {
return user;
}
ActionContext context=ActionContext.getContext();
context.getSession().put("userName", user.getUserName());
前台取值:<%=request.getAttribute("user.userName") %>或${userName }

3.前台传值:<input type="text" name="userName"/>
Action类:继承extends ActionSupport implements ModelDriven<User>

User user=new User();
@Override
public User getModel() {
return user;
}
前台取值:${userName }

05-04 03:30