<%= request.getParameter("str")%><%=request.getAttribute("str")%>
${str}
从ValueStack取值:
value="name"/>
value="user"/>
value="user.id"/>
从ActionContext中取值.
取值的时候需要加上一个符号:#
value="#user"/>
value="#user.id"/>
<s:property value="#parameters.name"/><br>
//获得request中key为MyName的值
<s:property value="#request.MyName"/><br>
//获得session中key为MyName的值的值
<s:property value="#session.MyName"/><br>
//获得application中key为MyName的值的值
<s:property value="#application.MyName"/><br>
1、通过ActionContext的实例化对象的put:
ActionContext context = ActionContext.getContext();
cotenxt.put(key, value);
对应前端取值的4种方式:
<%=request.getAttribute("str") %>
${str}
<s:property value="str"/>
<s:property value="#request.str"/>
2、通过ActionContext的实例化对象的get:
Map request = (Map)ActionContext.getContext().get("request");
request.put(key,value);
获取方法同上(不可用S标签)
3、通过ServletActionContext获取request:
HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute(key, value);
1、通过request获取session
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession(); session.setAttribute(key, value);
2、通过ActionContext获取session
HttpSession session =ActionContext.getContext().getSession();
session.setAttribute(key, value);
ValueStack vs = ActionContext.getContext().getValueStack();
User user = new User();
vs.push(user);
1、每一次请求都会创建一个ActionContext对象(也会创建一个ValueStack),struts框架默认向这个对象里面存放的对象(数据)很多,包括request、session、application、ValueStack、parameters等。
2、request session application的map类型获取:
(1)手动获取
ActionContext ac = ActionContext.getContext();
//获得Map类型request
Map<String,Object> request =
(Map<String, Object>) ac.get("request");
//获得Map类型session
Map<String, Object> session = ac.getSession();
//获得Map类型application
Map<String, Object> application = ac.getApplication();
(2)自动获取(通过继承接口)
三个接口依次为:
RequestAware,SessionAware,ApplicationAware
public class WebActionTest extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private Map request;
private Map session;
private Map application;
@Override
public String execute() throws Exception {
//页面中用原类型的对象取正常值就可以了
request.put("MyName", "tom");
session.put("YourName", "zhansan");
application.put("HerName", "lily");
return SUCCESS;
}
public void setRequest(Map request) {
this.request = request;
}
public void setSession(Map session) {
this.session = session;
}
public void setApplication(Map application) {
this.application = application;
}
}