不断的学习,不断的进步,今天才知道原来ognl还能支持函数,下面咱们就以具体案例来了解一下:
1、页面代码,红色高亮处可以看出在ognl中调用了后台的函数:
<html>
<head>
<title>导航菜单</title>
<%@ include file="/WEB-INF/jsp/public/commons.jspf"%>
<link type="text/css" rel="stylesheet" href="style/blue/menu.css" />
<script type="text/javascript">
function menuClick( menu ){
$(menu).next().toggle();
}
</script>
</head>
<body style="margin: 0">
<div id="Menu">
<ul id="MenuUl">
<%-- 显示一级菜单 --%>
<s:iterator value="#application.topPrivilegeList">
<s:if test="#session.user.hasPrivilegeByName(name)">
<li class="level1">
<div onClick="menuClick(this);" class="level1Style">
<img src="style/images/MenuIcon/${id}.gif" class="Icon" />
${name}
</div>
<ul style="" class="MenuLevel2" id="aa">
<%-- 显示二级菜单 --%>
<s:iterator value="children">
<s:if test="<span style="color:#ff0000;"><strong>#session.user.hasPrivilegeByName(name)</strong></span>">
<li class="level2">
<div class="level2Style">
<img src="style/images/MenuIcon/menu_arrow_single.gif" />
<a target="right" href="${pageContext.request.contextPath}${url}.action"> ${name}</a>
</div>
</li>
</s:if>
</s:iterator>
</ul>
</li>
</s:if>
</s:iterator>
</ul>
</div>
</body>
</html>
2、后台的代码,实体
package cn.itcast.oa.domain;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import com.opensymphony.xwork2.ActionContext;
/**
* 用户
*
* @author tyg
*
*/
public class User implements java.io.Serializable{
private Long id;
private Department department;
private Set<Role> roles = new HashSet<Role>();
private String loginName; // 登录名
private String password; // 密码
private String name; // 真实姓名
private String gender; // 性别
private String phoneNumber; // 电话号码
private String email; // 电子邮件
private String description; // 说明
/**
* 判断本用户是否有指定名称的权限
*
* @param name
* @return
*/
<strong><span style="color:#ff0000;">public boolean hasPrivilegeByName(String name) {
// 超级管理有所有的权限
if (isAdmin()) {
return true;
}
// 普通用户要判断是否含有这个权限
for (Role role : roles) {
for (Privilege priv : role.getPrivileges()) {
if (priv.getName().equals(name)) {
return true;
}
}
}
return false;
}</span></strong>
/**
* 判断本用户是否是超级管理员
*
* @return
*/
public boolean isAdmin() {
return "admin".equals(loginName);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}