struts2获取对象

ActionContext(struts 建议的)  action的上下文对象


//ActionContext可以理解为HttpServletRequest的代理

ActionContext  acx= ActionContext.getContext();

Map map= acx.getParameters();

String[] s=(String[])map.get("userName");

// request.setAttribute("hero","joke");

acx.put("hero","joke");

//request.getAttribute("hero");

acx.get("hero")

//session 就是HttpSession的代理

Map  session=acx.getSession();

// session.setAttribute("hero","joke");

session.put("hero","joke");

//session.getAttribute("hero");

session.get("hero")

//app就是ServletContextd的代理

Map app=acx.getApplication();

app.put("a","aa");

app.get("a");



ServletActionContext(获取真的httpServletRequest)

HttpServletRequest request = ServletActionContext.getRequest();

String name = request.getParameter("userName");

request.setAttribute("hero", "水晶");

HttpSession session = request.getSession();

session.setAttribute("s", "session");

session.getAttribute("s");

ServletContext sc = request.getServletContext();

sc.setAttribute("a", "app");

你可能感兴趣的:(struts2获取对象)