1、一般是在Action中定义一个成员变量,然后对这个成员变量提供get/set方法,在JSP页面就可以取到这个变量的值了。###
1)在Action中定义成员变量
//定义一个成员变量
private String message;
//提供get/set方法
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
2)在JSP页面中取值
${message}
或者
<s:property value="message"/>
2、但是定义的成员变量多了,感觉整个Action的代码就很长了。这个时候可以使用一些Servlet API进行值的存取操作:HttpServletRequest、HttpSession和ServletContext。Struts2对这个三个对象用Map进行了封装,我们就可以使用Map对象来存取数据了。###
1)在Action中存值
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");
Map<String,Object> session = actionContext.getSession();
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");
2)在JSP页面上取值
${a}
${b}
${c}
or
${requestScope.a}
${sessionScope.b}
${applicationScope.c}