没有合适的资源?快使用搜索试试~ 我知道了~
Struts2框架基础 二
需积分: 3 0 下载量 41 浏览量
2017-10-14
16:30:18
上传
评论
收藏 1.15MB DOCX 举报
温馨提示
试读
29页
OGNL表达式 interceptor拦截器 valuestack的存入 /取出
资源详情
资源评论
资源推荐
一 Struts2 第二天
1 本日重点
掌握在
Acon
中获取常用对象
request 、 response 、 ServletContext
了解
OGNL
及其作用
掌握使用
ValueStack
存储数据
掌握使用
OGNL
表达式获取
ValueStack
数据
了解
Struts2
中常用拦截器的作用
掌握自定义完成拦截器
2 第一节课
2.1 使用
ServletAconContext
获取
Servlet API (掌握)
public class LoginAction1 {
private HttpServletRequest request;
private HttpServletResponse response;
private ServletContext servletContext;
private HttpSession session;
public String execute() {
request = ServletActionContext.getRequest();
response = ServletActionContext.getResponse();
servletContext = ServletActionContext.getServletContext();
session = request.getSession();
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username + " " + password);
return "none";
}
}
2.2 注入方式获取 Servlet API
在执行 Acon 之前,Struts2 会执行 servletCong 的拦截器,这个拦截器会检查 Acon 是否实现了一些特定的
接口,如果实现了,会自动去调用 Acon 的这些接口的实现方法。
public class LoginAction2 implements ServletRequestAware,
ServletResponseAware,
ParameterAware,
SessionAware,
ServletContextAware{
private HttpServletRequest request;
private HttpServletResponse response;
private ServletContext servletContext;
private Map<String, Object> session;
private Map<String, String[]> parameters;
public String execute() {
String username = parameters.get("username")[0];
String password = parameters.get("password")[0];
System.out.println(username + " " + password);
return "none";
}
@Override
public void setServletContext(ServletContext context) {
this.servletContext = context;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setParameters(Map<String, String[]> parameters) {
this.parameters = parameters;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
2.3 OGNL
表达式操作对象(掌握)
OGNL 是一种表达式语言。使用 OGNL 可以存取对象的属性、调用对象的方法等。
OGNL 的三要素:表达式 OgnlContext 上下文 Root 根
2.3.1 对象操作
/**
* 使用 OGNL 进行对象操作
* @throws OgnlException
*/
@Test
public void test01() throws OgnlException {
OgnlContext ctx = new OgnlContext();
System.out.println(Ognl.getValue("'ognl'.length()", ctx, ctx.getRoot()));
}
2.3.2 访问静态成员
/**
* 使用 OGNL 访问静态方法/变量
* @throws OgnlException
*/
@Test
public void test02() throws OgnlException {
OgnlContext ctx = new OgnlContext();
System.out.println(Ognl.getValue("@Math@random()", ctx, ctx.getRoot()));
System.out.println(Ognl.getValue("@Math@PI", ctx, ctx.getRoot()));
}
2.3.3 访问 OGNL 上下文
/**
* 使用 OGNL 访问上下文
* @throws OgnlException
*/
@Test
public void test03() throws OgnlException {
OgnlContext ctx = new OgnlContext();
ctx.put("username", "tom");
System.out.println(Ognl.getValue("#username", ctx, ctx.getRoot()));
}
2.3.4 访问 OGNL 上下文 ROOT
/**
* 使用 OGNL 访问 ROOT
* @throws OgnlException
*/
@Test
public void test04() throws OgnlException {
OgnlContext ctx = new OgnlContext();
Map<String, Object> root = new HashMap<String, Object>();
root.put("username", "tom");
ctx.setRoot(root);
System.out.println(Ognl.getValue("username", ctx, ctx.getRoot()));
}
3 第二节课
3.1 111 操作集合
3.1.1 操作集合
@Test
public void test05() throws OgnlException {
OgnlContext ctx = new OgnlContext();
Object result = Ognl.getValue("{1, 2, 3, 4, 5}", ctx, ctx.getRoot());
System.out.println(result);
// 访问集合中的元素
ctx.setRoot(result);
System.out.println(Ognl.getValue("[0]", ctx, ctx.getRoot()));
剩余28页未读,继续阅读
壹叁壹肆
- 粉丝: 7
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0