两者都是在服务器端执行的,JSP在转化成Servelt并编译成java文件时,会将ognl,el,<%%>等解释出来,然后返回给客户端;
ognl表达式需要依赖于Struts2的标签,如<s:property value="#xxxx"> 在Struts的页面中不能使用,如使用<a href="checkInfo.jsp?id=%{#student.studentid}">但是可以使用<s:a href="checkInfo.jsp?id=%{#student.studentid}">因为<s:a>是Struts2的标签
说明:<s:a href="checkInfo.jsp?id=%{#student.studentid}">中的%{}的作用是告诉解释器其中的代码为ognl表达式;
但是el表达式是可以的如${sessionScope.username}
页面区中的区别:
名称
servlet ognl el
parameters request.getParameter("username") #username ${username}
request request.getAttribute("userName") #request.userName ${requestScope.username}
session session.getAttribute("userName") #session.userName ${sessionScope.username}
application application.getAttribute("userName") #application.userName ${applicationScope.username}
attr 用于按request > session > application顺序访问其属性(attribute #attr.userName相当于按顺序在以上三个范围(scope)内读取userName属性,直到找到为
OGNL的讲解:
ognl是Struts2默认的语言,也成为(对象图导航语言)
1.#号的用途一般有三种:c.用于构建map,如#{‘foot1’:'bar1','foot2':'bar2'}
2.%的用途:
用途是在标志的属性为字符串属性时计算OGNL表达式的值,
- <s:set name=”foobar” value=”#{’foo1′:’bar1′, ‘foo2′:’bar2′}” />
- <p>The value of key “foo1″ is <s:property value=”#foobar['foo1']” /></p>
- <p>不使用%:<s:url value=”#foobar['foo1']” /></p>
- <p>使用%:<s:url value=”%{#foobar['foo1']}” /></p>
$号的用途主要有两个:在国际化资源文件中引用OGNL表达式,例如国际化中的代码reg.agerange=国际化资源信息:年龄介于${min}和${max}年龄之间
在Struts2中引用OGNL表达式,如下面的代码:
- <validators>
- <field name=”intb”>
- <field-validator type=”int”>
- <param name=”min”>10</param>
- <param name=”max”>100</param>
- <message>BAction-test校验:数字必须为${min}为${max}之间!</message>
- </field-validator>
- </field>
- </validators>