// 本类将演示拦截器中对ActionContext的操作
publicclass MyInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
// 获得ActionContext
ActionContext actionContext = invocation.getInvocationContext();
// 存入值
Person person = new Person();
actionContext.put("person", person);
// 获取值
Object value = actionContext.get("person");
// 获取HttpServletRequest
HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
// 获取request的Map,即HttpServletRequest.getAttribute(...)和HttpServletRequest.setAttribute(...)所操作的值
Map requestMap = (Map) actionContext.get("request");
// 其他代码
// ......
return invocation.invoke();
}
}
// 本类将演示在Action中对ActionContext进行操作
publicclass MyAction extends ActionSupport {
@Override
public String execute() throws Exception {
// 获得值栈
ActionContext actionContext = ActionContext.getContext();
// 存入值
Person person = new Person();// 这是之前例子中定义的类
actionContext.put("person", person);
// 获取值
Object object = actionContext.get("person");
// 其他代码
// ......
return SUCCESS;
}
}
<!DOCTYPE html>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<!-- 本JSP将演示在JSP中对ActionContext的使用 -->
<!-- 本JSP为MyAction对应的JSP -->
<!-- 由于Action中已经向ActionContext存入了key为"person"的值,所以可以使用“#person”来获取它,如下 -->
<s:propertyvalue="#person"/>
<!-- 获得person的name属性,如下 -->
<s:propertyvalue="#person.name"/>
<!-- 获得Struts2在ActionContext中存入的值,比如request的Map,如下 -->
<s:propertyvalue="#request"/>
<!-- 获得Struts2在ActionContext中存入的值,比如session的Map,如下 -->
<s:propertyvalue="#session"/>
<!-- 获得Struts2在ActionContext中存入的值,request请求传递的GET参数或POST参数的Map,如下 -->
<s:propertyvalue="#parameters"/>
<!-- 以下演示在JSP中把值存入ActionContext中 -->
<!-- 存入一个字符串"myName",key为"myKey",如下 -->
<s:setvalue="%{'myName'}"var="myKey"/>
<!-- 使用s:bean标签来创建一个对象,并把它存入ActionContext中,key为myObject,如下 -->
<s:beanname="com.example.Person"var="myObject"/>
<!-- 之后就可以用“#”来读取它们,如下 -->
<s:propertyvalue="#myKey"/>
<s:propertyvalue="#myObject"/>
</body>
</html>
// 本类将演示拦截器中对HttpServletRequest和request的Map的操作
publicclass MyInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
// 获得ActionContext
ActionContext actionContext = invocation.getInvocationContext();
// 获得HttpServletRequest
HttpServletRequest httpServletRequest=(HttpServletRequest)actionContext.get(StrutsStatics.HTTP_REQUEST);
// 获得request的Map
Map requestMap = (Map) actionContext.get("request");
// 创建一个类作为实例
Person person = new Person();
// 以下两行的语句作用相同
httpServletRequest.setAttribute("person", person);
requestMap.put("person", person);
// 其他代码
// ......
return invocation.invoke();
}
}
// 本类将演示在Action中对HttpServletRequest和request的Map进行操作(静态方法获得ActionContext)
publicclass MyAction extends ActionSupport {
@Override
public String execute() throws Exception {
// 获得ActionContext
ActionContext actionContext = ActionContext.getContext();
// 获得HttpServletRequest
HttpServletRequest httpServletRequest=(HttpServletRequest)actionContext.get(StrutsStatics.HTTP_REQUEST);
// 获得request的Map
Map requestMap = (Map) actionContext.get("request");
// 创建一个类作为实例
Person person = new Person();
// 以下两行的语句作用相同
httpServletRequest.setAttribute("person", person);
requestMap.put("person", person);
// 其他代码
// ......
return SUCCESS;
}
}
// 本类将演示在Action中使用ServletRequestAware获得HttpServletRequest(注意:要使用ServletConfigInterceptor拦截器)
publicclass MyAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
//此方法是接口ServletRequestAware的方法
publicvoid setServletRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public String execute() throws Exception {
// HttpServletRequest已在该类的字段中准备好,可直接使用
// ......
return SUCCESS;
}
}
// 本类将演示在Action中使用ServletRequestAware获得request的Map(注意:要使用ServletConfigInterceptor拦截器)
publicclass MyAction extends ActionSupport implements RequestAware {
Map<String, Object> request;
// 该方法是接口RequestAware的方法
publicvoid setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public String execute() throws Exception {
// request的Map已在该类的字段中准备好,可直接使用
// ......
return SUCCESS;
}
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<!-- 本JSP将演示在JSP中对request的Map的使用 -->
<!-- 本JSP为MyAction对应的JSP -->
<!-- request的Map是Struts2自动在ActionContext中存入的值(key为request),所以使用“#”来访问ActionContext,从中读取request -->
<s:property value="#request"/>
<!-- 以下两行均是访问request的Map中key为“name”的值 -->
<s:property value="#request.name"/>
<s:property value="#request['name']"/>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有