struts向jsp页面传值有两种方式:
1、在Action中定义成员变量,jsp页面通过${成员变量名}或<s:property value="成员变量名">取值。
如:private String username;
setter/getter方法
${username}或<s:property value="username">
2、如果成员变量较多,会使得Action代码冗余,因此考虑使用,HttpServletRequest、HttpSession和ServletContext对象进行数据读取操作。Struts2对这个三个对象用Map进行了封装,我们就可以使用Map对象来存取数据了。
如:ActionContext actionContext = ActionContext.getContext();
//get HttpServletRequest
Map<String,Object> request = (Map) actionContext.get("request");
request.put("a", "a is in request");
//get HttpSession
Map<String,Object> session= (Map) actionContext.get("session");
session.put("b", "b is in session");
//get ServletContext
Map<String,Object> application = (Map) actionContext.get("application");
Map<String,Object> application = actionContext.getApplication();
application.put("c", "c is in application");
jsp取值:${a}或${requestScope.a}