struts2如何获取页面参数
最近在复习struts2的知识。在做实验的时候遇到了“要获取从页面传递到action中的参数”的问题,在Google和Baidu之后,获得解决,并总结如下,以备自己和各位朋友查阅。
1. 将请求参数自动设置到字段中。可借助OGNL完成。
struts2对OGNL上下文的概念又做了进一步扩充,在struts2中,OGNL上下文通常如下所示:
|--request | |--application | context map---|--OgnlValueStack(root) [ user, action, OgnlUtil, ... ] | |--session | |--attr | |--parameters
// 获取页面传递的action参数ActionContext actiobContext = ActionContext.getContext();Map paramsMap = actiobContext.getParameters();String[] attributes = (String[]) paramsMap.get("personId"); // 属性对应的是数组......
static ThreadLocal actionContext = new ThreadLocal();......public static ActionContext getContext() { return (ActionContext) actionContext.get(); // Don't do lazy context creation, as it requires container; the creation of which may // precede the context creation //if (context == null) { // ValueStack vs = ValueStackFactory.getFactory().createValueStack(); // context = new ActionContext(vs.getContext()); // setContext(context); //}}
ActionContext context = (ActionContext) actionContext.get();来获取的。我们再来看看这里的actionContext对象的创建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
Map session = ActionContext.getContext().getSession();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletConfig servletConfig = ServletActionContext.getPageContext().getServletConfig();
ActionContext ctx = ActionContext.getContext();ctx.put("liuwei", "andy"); //request.setAttribute("liuwei", "andy");Map session = ctx.getSession(); //sessionHttpServletRequest request = (HttpServletRequest) ctx.get(StrutsStatics.HTTP_REQUEST);HttpServletResponse response = (HttpServletResponse) ctx.get(StrutsStatics.HTTP_RESPONSE);// 或者HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);// ServletActionContext.APPLICATION;// ServletActionContext.SESSION;// ServletActionContext.PAGE_CONTEXT; // 获取页面(传递)的参数Enumeration parameterNames = request.getParameterNames();while (parameterNames.hasMoreElements()) {String parameterName = (String) parameterNames.nextElement();System.out.println(" ---- " + parameterName);}ActionContext ctx = ActionContext.getContext();ctx.put("liuwei", "andy"); //request.setAttribute("liuwei", "andy");Map session = ctx.getSession(); //session
import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {private HttpServletRequest req; // 这条语句放在这个位置是错误的,同样把这条语句放在构造方法中也是错误的。// private HttpServletRequest req = ServletActionContext.getRequest(); public String login() { req = ServletActionContext.getRequest(); //req的获得必须在具体的方法中实现 user = new User(); user.setUserId(userId); user.setPassword(password); if (userDAO.isLogin(user)) { req.getSession().setAttribute("user", user); return SUCCESS; } return LOGIN; } public String queryAll() { req = ServletActionContext.getRequest(); //req的获得必须在具体的方法中实现 userList = userDAO.queryAll(); req.getSession().setAttribute("uList", userList); return SUCCESS; } }
import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {private Map request;private Map session;private Map application;public LoginAction() {this.request = (Map) ActionContext.getContext().get("request");this.session = ActionContext.getContext().getSession();this.application = ActionContext.getContext().getApplication();}public String execute() {this.request.put("r1", "r1");this.session.put("s1", "s1");this.application.put("a1", "a1");return SUCCESS;}}
import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {private HttpServletRequest request;private HttpSession session;private ServletContext application;public LoginAction() {request = ServletActionContext.getRequest();session = request.getSession();application = session.getServletContext();}public String execute() {request.setAttribute("r1", "r1");session.setAttribute("s1", "s1");application.setAttribute("a1", "a1");return SUCCESS;}}
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.interceptor.ApplicationAware;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.interceptor.SessionAware;public class UserAction implements SessionAware, ServletRequestAware, ServletResponseAware, ApplicationAware {private HttpServletRequest request; private HttpServletResponse response; private HttpSession session; private Map<String, Object> application;@Overridepublic void setApplication(Map<String, Object> application) {this.application = application;}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request; }@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response; } @Overridepublic void setSession(Map<String, Object> session) {/*this.session = (HttpSession) session;*/this.session = request.getSession(); }public String login() {//// 此处使用request, response, session, application对象来获取相关的参数// // 业务代码 } }
import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.interceptor.ApplicationAware;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.interceptor.SessionAware;public class UserAction implements ServletRequestAware, ServletResponseAware, SessionAware, ApplicationAware {private Map<String, Object> request;private Map<String, Object> response;private Map<String, Object> session;private Map<String, Object> application;@Overridepublic void setApplication(Map<String, Object> application) {this.application = application;}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = (Map<String, Object>) request; }@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = (Map<String, Object>) response; } @Overridepublic void setSession(Map<String, Object> session) {/*this.session = (HttpSession) session;*/this.session = session; }public String login() {//// 此处使用request, response, session, application对象来获取相关的参数// // 业务代码 } public String execute() {//request.put("r1", "r1");// session.put("s1", "s1");//application.put("a1", "a1");return SUCCESS;}}
import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport implements ServletRequestAware {private HttpServletRequest request;private HttpSession session;private ServletContext application;@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;this.session = request.getSession();this.application = session.getServletContext();}public String execute() {request.setAttribute("r1", "r1");session.setAttribute("s1", "s1");application.setAttribute("a1", "a1");return SUCCESS;}}