OGNL表达式与EL表达式区别

来源:互联网 发布:windows api 打开文件 编辑:程序博客网 时间:2024/05/26 22:55






1.显示页面****************************
/** 列表 */
public String list() throws Exception {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);
return "list";
}


<s:iterator value="roleList">
<tr class="TableDetail1 template">
<td>${name}&nbsp;</td>
<td>${description}&nbsp;</td>
<td>
<s:a action="role_delete?id=%{id}" onclick="return confirm('确定要删除么?')">删除</s:a> 
<s:a action="role_editUI?id=%{id}">修改</s:a>
<a href="setPrivilegeUI.html">设置权限</a>
</td>
</tr>
</s:iterator>


此处将roleList放到了map域中,在页面使用<s:iterator value="roleList"/>标签遍历时,可以直接写集合的名称
此处为使用了ognl表达式
每遍历一个集合中的对象,都会将该对象放在栈顶,在获取的时候可以直接用${对象的字段}获取值
在写超链接的时候带带参数,则使用如下形式:
<s:a action="role_editUI?id=%{id}">修改</s:a>
id为点击该超链接的时候传递过去的参数,可以在Action类中定义该字段id,并设置setter/getter属性,这样就可以
将传递过来的参数的值封装到该字段并使用了。



访问的时候带了一个参数字段并传了值,在action中或modelDriven中定义了该字段和该字段的setter/getter属性,
   就可以在jsp页面中使用该字段了。
---------------------------------------------------------------------------------------
OGNL表达式:
Department parent = departmentService.getById(parentId);
// 注: Department类有id字段
ActionContext.getContext().put("parent", parent);

在JSP页面中写OGNL表达式,用%{}表示,写EL表达式,用${}
<s:a action="department_addUI?parentId=%{#parent.id}"/>
加#号表示直接从map域找parent,找不到报错。
<s:a action="department_addUI?parentId=%{parent.id}"/>
不加#号表示先从栈顶找,如果modelDriven中或action中有该属性(此处为parent),就获取值,
如果没有找到,就从map域中找,如果找到就获取值,找不到报错

注:在s:a标签中,action带参数,后面用ognl表达式获取封装过来的参数的值
在页面遍历集合时,集合的名称也是ognl表达式,遍历的时候会将集合中的元素放到栈顶,
可以使用EL表达式${} 获取当前对象里面的字段的值。

在struts配置文件中也可以使用OGNL表达式,使用${}就可以
<result name="toList" type="redirectAction">department_list?parentId=${parentId}</result>
注: 此处传递的参数为parentId,是jsp页面封装好传递过来的参数   注意不是parent.id



EL表达式${}是对Requst.findAttribute("..")函数的封装,该函数的搜索顺序为page>request>session>application
其没有在栈顶找字段的功能。但是在<s:iteratot value="userlist"> ${name} </s:iterator> 可以使用el表达式获取栈顶元素的值
说明struts2对Request对象的findAttribute函数进行了封装,增加了在栈顶获取的功能


OGNL表达式只对struts标签管用,在HTML标签中还是要使用EL表达式
struts2标签中都是用的OGNL表达式,先从栈顶找,找不到就从map域中找,再找不到直接报错
而常规EL表达式只能从map域中找,不能在栈顶找,找不到什么都不显示。
搜索顺序为:page>request>session>application
但是struts2对HttpRequest对象进行了封装StrutsRequestWrapper,此时EL表达式可以在栈顶搜索
搜索顺序为:page>request>ValueStack>session>application


ActionContext.getContext().put("userlist",userlist); 将userlist放到map域中
ActionContext.getContext().getValueStack().push(userlist);将userList放到了struts2的栈顶了

添加删除修改成功后转向原来的页面:

注:第一个参数id和第二个参数parentId都在action中或者POJO中定义了,其值在此处用OGNL表达式指定了。
<s:iterator value="departmentList">
<s:a action="department_delete?id=%{id}&parentId=%{parent.id}" onClick="return window.confirm('这将删除所有的下级部门,您确定要删除吗?')">删除</s:a>            
</s:iterator>

注:
public class DepartmentAction{
// 注:此处的parentId为action中单独定义的,并设置了其setter/getter属性,对应第二个参数
private Long parentId;
// 其setter/getter属性

public String delete() throws Exception {
// 注:此处的id为模型驱动中POJO中定义的id,对应第一个id参数
departmentService.delete(model.getId());
return "toList";
}
}

<action name="department_*" class="departmentAction" method="{1}">
<!-- 注:此处定义了要回到指定parentId的页面,其中parentId为请求动作的参数 -->
<result name="toList" type="redirectAction">department_list?parentId=${parentId}</result>
</action>




2. 自己实现struts2标签的回显功能
OGNL表达式的in语法,以及<label for="标识">${name}</label>的使用


<%-- 使用Struts2的标签显示复选框,有回显功能,但不方便修改显示效果。
<s:checkboxlist name="privilegeIds" list="privilegeList" listKey="id" listValue="name" >
</s:checkboxlist>
--%>

<%-- 自己使用循环显示复选框,需要自己实现回显功能
<s:iterator value="privilegeList">
<input type="checkbox" name="privilegeIds" value="${id}" id="cb_${id}"   
<s:property value="%{id in privilegeIds ? 'checked' : ''}"/>
/>
<label for="cb_${id}">${name}</label>
</s:iterator>
--%>

注: 此时input标签也可以使用EL表达式来获取栈顶元素的值,是因为struts2的环境,
   struts2标签中能获取是因为struts2对Request对象进行了封装,
难道如果封装之后,不管是否为struts标签还是HTML标签,都可以获取栈顶元素的值。




3. ognl表达式可以调用方法,但是el表达式不可以 
   ognl在strut.xml中使用,用${}


4. <!-- 论坛: 主题相关功能 -->
<action name="topic_*" class="topicAction" method="{1}">
<result name="show">/WEB-INF/jsp/topicAction/show.jsp</result>
<!-- 注:此处的为ognl表达式,而topicId被封装到了map域中,所以要加# -->
<result name="toShow" type="redirectAction">topic_show?id=${#topicId}</result>
</action>


<!-- 论坛:回复相关功能 -->
<action name="reply_*" class="replyAction" method="{1}">
<result name="toTopicShow" type="redirectAction">topic_show?id=${topicId}</result>
</action>

5.

6. 
forumAction/list.jsp
<!-- 转向指定版块 -->
<s:a cssClass="ForumPageTopic" action="forum_show?id=%{id}">${name}</s:a>
<!-- 转向指定主题 -->
<s:a cssClass="ForumTitle" action="topic_show?id=%{lastTopic.id}">${lastTopic.title}</s:a>        

ForumAction:
// 由于上面传了一个id,在ForumAction中实现了modelDriven接口,里面有id
Forum forum = forumService.getById(model.getId());
ActionContext.getContext().put("forum", forum);

JSP:
${forum.name}   
EL表达式还可以在map域中查找: page,request,valueStack,session,application都是map域
疑问:是找的哪一个map域
ongl表达式如果在map域,需要加#号获取,但是el表达式可以直接获取  


7. 
TopicAction/addUI.jsp

注: TopicAction中定义了forumId字段并设置了其setter/getter属性
TopicAction{
private Long forumId;
Forum forum = forumService.getById(forumId);
ActionContext.getContext().put("forum", forum);
}

<%-- ongl表达式如果在map栈,需要加#号获取,但是el表达式可以直接获取   
另外此处forumId在栈顶,可以直接获取, 即%{#forum.id}可以换成forumId  --%>
<s:a action="forum_show?id=%{#forum.id}">${forum.name}</s:a>
<s:a action="forum_show?id=%{forumId}">${forum.name}</s:a>


8. TopicAction{
/** 发新帖 */
public String add() {
// 传递id,在配置文件中获取
ActionContext.getContext().put("topicId", topic.getId());
return "toShow";// 转向当前这个新主题的显示页面
}
}

<!-- 论坛: 主题相关功能 -->
<action name="topic_*" class="topicAction" method="{1}">
<result name="show">/WEB-INF/jsp/topicAction/show.jsp</result>
<!-- 注:此处的为ognl表达式,而topicId被封装到了map域中,所以要加# -->
<result name="toShow" type="redirectAction">topic_show?id=${#topicId}</result>
</action>


<s:a cssClass="detail" action="reply_addUI?topicId=%{#topic.id}">
<img border="0" src="${pageContext.request.contextPath}/style/images/reply.gif" />回复
</s:a>   






9. window.location.href = "forum_show.do?id=${id}&pageNum=" + pageNum;








































---------------------------------------------------------------------------
0 0
原创粉丝点击