必威体育Betway必威体育官网
当前位置:首页 > IT技术

自定义 jstl 标签 fns

时间:2019-05-31 00:44:09来源:IT技术作者:seo实验室小编阅读:76次「手机版」
 

fns

1、jsp 头部页面引入函数

jsp页面需要引入自定义fns函数声明:

<%@ taglib prefix=”fns” uri=”/WEB-INF/tlds/fns.tld” %>,自定义的tld文件位于/WEB-INF/tlds/fns.tld

一般需要C标签配合使用,同时引入C标签声明:

<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>

2、fns.tld 代码,仿照jstl的fn函数fn.tld的书写格式:

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">

    <description>JSTL 1.1 functions library</description>
    <display-name>JSTL functions sys</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>fns</short-name>
    <uri>http://java.sun.com/jsp/jstl/functionss</uri>


    <function>
        <description>获取开发公司名称</description>
        <name>getCompanyName</name>
        <function-class>com.jeeplus.common.config.Global</function-class>
        <function-signature>java.lang.String getCompanyName()</function-signature>
        <example>${fns:getCompanyName()}</example>
    </function>

    <function>
        <description>获取项目名称</description>
        <name>getProjectName</name>
        <function-class>com.jeeplus.common.config.Global</function-class>
        <function-signature>java.lang.String getProjectName()</function-signature>
        <example>${fns:getProjectName()}</example>
    </function>

    <function>
        <description>获取基本设置信息</description>
        <name>getSysBase</name>
        <function-class>com.jeeplus.common.config.Global</function-class>
        <function-signature>com.jeeplus.modules.sys.entity.SysBase getSysBase()</function-signature>
        <example>${fns:getSysBase()}</example>
    </function>


    <function>
        <description>获取管理路径</description>
        <name>getAdminPath</name>
        <function-class>com.jeeplus.common.config.Global</function-class>
        <function-signature>java.lang.String getAdminPath()</function-signature>
        <example>${fns:getAdminPath()}</example>
    </function>
    <function>
        <description>获取网站路径</description>
        <name>getFrontPath</name>
        <function-class>com.jeeplus.common.config.Global</function-class>
        <function-signature>java.lang.String getFrontPath()</function-signature>
        <example>${fns:getFrontPath()}</example>
    </function>
    <function>
        <description>获取网站URL后缀</description>
        <name>getUrlSuffix</name>
        <function-class>com.jeeplus.common.config.Global</function-class>
        <function-signature>java.lang.String getUrlSuffix()</function-signature>
        <example>${fns:getUrlSuffix()}</example>
    </function>
    <function>
        <description>获取配置</description>
        <name>getConfig</name>
        <function-class>com.jeeplus.common.config.Global</function-class>
        <function-signature>java.lang.String getConfig(java.lang.String)</function-signature>
        <example>${fns:getConfig(key)}</example>
    </function>
    <function>
        <description>获取常量</description>
        <name>getconst</name>
        <function-class>com.jeeplus.common.config.Global</function-class>
        <function-signature>java.lang.Object getConst(java.lang.String)</function-signature>
        <example>${fns:getConst(key)}</example>
    </function>


    <!-- UserUtils -->
    <function>
        <description>数字转换为中文字符串</description>
        <name>convertToChart</name>
        <function-class>com.jeeplus.common.utils.StringUtils</function-class>
        <function-signature>java.lang.String convertToChart(int)</function-signature>
        <example>${fns:convertToChart(digit)}</example>
    </function>
    <function>
        <description>获取当前用户对象</description>
        <name>getUser</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>com.jeeplus.modules.sys.entity.User getUser()</function-signature>
        <example>${fns:getUser()}</example>
    </function>

    <function>
        <description>根据编码获取用户对象</description>
        <name>getUserById</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>com.jeeplus.modules.sys.entity.User get(java.lang.String)</function-signature>
        <example>${fns:getUserById(id)}</example>
    </function>

    <function>
        <description>获取授权用户信息</description>
        <name>getPrincipal</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>com.jeeplus.modules.sys.security.SystemAuthorizingRealm.Principal getPrincipal()</function-signature>
        <example>${fns:getPrincipal()}</example>
    </function>

    <function>
        <description>获取当前用户的菜单对象列表</description>
        <name>getMenuList</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>java.util.List getMenuList()</function-signature>
        <example>${fns:getMenuList()}</example>
    </function>

    <function>
        <description>获取当前用户的菜单对象列表</description>
        <name>getTopMenu</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>com.jeeplus.modules.sys.entity.Menu getTopMenu()</function-signature>
        <example>${fns:getTopMenu()}</example>
    </function>
    <function>
        <description>获取当前时间与指定时间的差值</description>
        <name>getTime</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>java.util.Date getTime(java.lang.String)</function-signature>
        <example>${fns:getTime(java.lang.String)}</example>
    </function>
    <function>
        <description>获取当前用户的区域对象列表</description>
        <name>getAreaList</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>java.util.List getAreaList()</function-signature>
        <example>${fns:getAreaList()}</example>
    </function>

    <function>
        <description>获取当前用户的部门对象列表</description>
        <name>getOfficeList</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>java.util.List getOfficeList()</function-signature>
        <example>${fns:getOfficeList()}</example>
    </function>

    <function>
        <description>获取当前用户缓存</description>
        <name>getcache</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>java.lang.Object getCache(java.lang.String, java.lang.Object)</function-signature>
        <example>${fns:getCache(cacheName, defaultValue)}</example>
    </function>

    <!-- DictUtils -->
    <function>
        <description>获取字典标签</description>
        <name>getDictLabel</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>java.lang.String getDictLabel(java.lang.String, java.lang.String, java.lang.String)</function-signature>
        <example>${fns:getDictLabel(value, type, defaultValue)}</example>
    </function>

    <function>
        <description>获取字典标签(多个)</description>
        <name>getDictLabels</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>java.lang.String getDictLabels(java.lang.String, java.lang.String, java.lang.String)</function-signature>
        <example>${fns:getDictLabels(values, type, defaultValue)}</example>
    </function>

    <function>
        <description>获取字典值</description>
        <name>getDictValue</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>java.lang.String getDictValue(java.lang.String, java.lang.String, java.lang.String)</function-signature>
        <example>${fns:getDictValue(label, type, defaultValue)}</example>
    </function>

    <!--获取字典对象列表 start-->
    <function>
        <description>获取字典对象列表</description>
        <!--EL页面调用名字-->
        <name>getDictList</name>
        <!--指定标签的处理类,指定了标签由哪个Java类来处理。-->
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <!--指定EL页面调用名字中实际调用的方法.指定处理类的实际方法.参数和回调函数要写完整路径-->
        <function-signature>java.util.List getDictList(java.lang.String)</function-signature>
        <!--举例说明-->
        <example>${fns:getDictList(type)}</example>
    </function>
    <!--获取字典对象列表 end-->

    <function>
        <description>获取字典对象列表</description>
        <!--EL页面调用名字-->
        <name>getDictListForQuestion</name>
        <!--指定标签的处理类,指定了标签由哪个Java类来处理。-->
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <!--指定EL页面调用名字中实际调用的方法.指定处理类的实际方法.参数和回调函数要写完整路径-->
        <function-signature>java.util.List getDictListForQuestion(java.lang.String)</function-signature>
        <!--举例说明-->
        <example>${fns:getDictListForQuestion(type)}</example>
    </function>

    <!--获取字典对象列表 start-->
    <function>
        <description>获取字典对象列表</description>
        <!--EL页面调用名字-->
        <name>getDictList2</name>
        <!--指定标签的处理类,指定了标签由哪个Java类来处理。-->
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <!--指定EL页面调用名字中实际调用的方法.指定处理类的实际方法.参数和回调函数要写完整路径-->
        <function-signature>java.util.List getDictList2(java.lang.String,java.lang.String)</function-signature>
        <!--举例说明-->
        <example>${fns:getDictList2(type,keyValue)}</example>
    </function>
    <!--获取字典对象列表 end-->

    <function>
        <description>获取系统常量对象列表</description>
        <!--EL页面调用名字-->
        <name>getConsList</name>
        <!--指定标签的处理类,指定了标签由哪个Java类来处理。-->
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <!--指定EL页面调用名字中实际调用的方法.指定处理类的实际方法.参数和回调函数要写完整路径-->
        <function-signature>java.util.List getConsList(java.lang.String)</function-signature>
        <!--举例说明-->
        <example>${fns:getConsList(type)}</example>
    </function>

    <function>
        <description>获取系统常量对象列表-增加所有**选项</description>
        <!--EL页面调用名字-->
        <name>getConsList2</name>
        <!--指定标签的处理类,指定了标签由哪个Java类来处理。-->
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <!--指定EL页面调用名字中实际调用的方法.指定处理类的实际方法.参数和回调函数要写完整路径-->
        <function-signature>java.util.List getConsList2(java.lang.String,java.lang.String)</function-signature>
        <!--举例说明-->
        <example>${fns:getConsList2(type,keyValue)}</example>
    </function>

    <function>
        <description>获取字典对象列表</description>
        <name>getDictListJson</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>java.lang.String getDictListJson(java.lang.String)</function-signature>
        <example>${fns:getDictListJson(type)}</example>
    </function>

    <function>
        <description>获取对象的属性值</description>
        <name>getValue</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>java.lang.String getValue(java.lang.Object, java.lang.String)</function-signature>
        <example>${fns:getValue(obj, name)}</example>
    </function>

    <function>
        <description>获取两级对象的属性值</description>
        <name>getObjectValue</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>java.lang.String getObjectValue(java.lang.Object, java.lang.String)</function-signature>
        <example>${fns:getObjectValue(obj, name)}</example>
    </function>

    <!-- Encodes -->
    <function>
        <description>URL编码</description>
        <name>urlEncode</name>
        <function-class>com.jeeplus.common.utils.Encodes</function-class>
        <function-signature>java.lang.String urlEncode(java.lang.String)</function-signature>
        <example>${fns:urlEncode(part)}</example>
    </function>
    <function>
        <description>URL解码</description>
        <name>urlDecode</name>
        <function-class>com.jeeplus.common.utils.Encodes</function-class>
        <function-signature>java.lang.String urlDecode(java.lang.String)</function-signature>
        <example>${fns:urlDecode(part)}</example>
    </function>
    <function>
        <description>HTML编码</description>
        <name>escapeHtml</name>
        <function-class>com.jeeplus.common.utils.Encodes</function-class>
        <function-signature>java.lang.String escapeHtml(java.lang.String)</function-signature>
        <example>${fns:escapeHtml(html)}</example>
    </function>
    <function>
        <description>HTML解码</description>
        <name>unescapeHtml</name>
        <function-class>com.jeeplus.common.utils.Encodes</function-class>
        <function-signature>java.lang.String unescapeHtml(java.lang.String)</function-signature>
        <example>${fns:unescapeHtml(html)}</example>
    </function>

    <!-- StringUtils -->
    <function>
        <description>从后边开始截取字符串</description>
        <name>substringAfterLast</name>
        <function-class>org.apache.commons.lang3.StringUtils</function-class>
        <function-signature>java.lang.String substringAfterLast(java.lang.String, java.lang.String)</function-signature>
        <example>${fns:substringAfterLast(str,separator)}</example>
    </function>
    <function>
        <description>判断字符串是否以某某开头</description>
        <name>startsWith</name>
        <function-class>org.apache.commons.lang3.StringUtils</function-class>
        <function-signature>boolean startsWith(java.lang.Charsequence, java.lang.CharSequence)</function-signature>
        <example>${fns:startsWith(str,prefix)}</example>
    </function>
    <function>
        <description>判断字符串是否以某某结尾</description>
        <name>endsWith</name>
        <function-class>org.apache.commons.lang3.StringUtils</function-class>
        <function-signature>boolean endsWith(java.lang.CharSequence, java.lang.CharSequence)</function-signature>
        <example>${fns:endsWith(str,suffix)}</example>
    </function>
    <function>
        <description>缩写字符串,超过最大宽度用“...”表示</description>
        <name>abbr</name>
        <function-class>com.jeeplus.common.utils.StringUtils</function-class>
        <function-signature>java.lang.String abbr(java.lang.String, int)</function-signature>
        <example>${fns:abbr(str,length)}</example>
    </function>
    <function>
        <description>替换掉HTML标签</description>
        <name>replaceHtml</name>
        <function-class>com.jeeplus.common.utils.StringUtils</function-class>
        <function-signature>java.lang.String replaceHtml(java.lang.String)</function-signature>
        <example>${fns:replaceHtml(html)}</example>
    </function>
    <function>
        <description>转换为JS获取对象值,生成三目运算返回结果。</description>
        <name>jsGetVal</name>
        <function-class>com.jeeplus.common.utils.StringUtils</function-class>
        <function-signature>java.lang.String jsGetVal(java.lang.String)</function-signature>
        <example>${fns:jsGetVal('row.user.id')}  返回:!row?'':!row.user?'':!row.user.id?'':row.user.id</example>
    </function>

    <!-- DateUtils -->
    <function>
        <description>获取当前日期</description>
        <name>getDate</name>
        <function-class>com.jeeplus.common.utils.DateUtils</function-class>
        <function-signature>java.lang.String getDate(java.lang.String)</function-signature>
        <example>${fns:getDate(pattern)}</example>
    </function>
    <function>
        <description>获取过去的天数</description>
        <name>pastDays</name>
        <function-class>com.jeeplus.common.utils.DateUtils</function-class>
        <function-signature>long pastDays(java.util.Date)</function-signature>
        <example>${fns:pastDays(date)}</example>
    </function>
    <function>
        <description>将日期对象Date转为标准格式字符串yyyy-mm-dd hh:mm:ss显示</description>
        <name>formatDateTime</name>
        <function-class>com.jeeplus.common.utils.DateUtils</function-class>
        <function-signature>java.lang.String formatDateTime(java.util.Date)</function-signature>
        <example>${fns:formatDateTime(date)}</example>
    </function>
    <function>
        <description>将日期对象Date转为几小时前,几天前显示</description>
        <name>formatDateDiff</name>
        <function-class>com.jeeplus.common.utils.DateUtils</function-class>
        <function-signature>java.lang.String formatDateDiff(java.util.Date)</function-signature>
        <example>${fns:formatDateDiff(date)}</example>
    </function>
    <!-- JsonMAPPer -->
    <function>
        <description>对象转换JSON字符串</description>
        <name>toJson</name>
        <function-class>com.jeeplus.common.mapper.JsonMapper</function-class>
        <function-signature>java.lang.String toJsonString(java.lang.Object)</function-signature>
        <example>${fns:toJson(object)}</example>
    </function>

    <function>
        <description>根据编码获取用户对象</description>
        <name>getByLoginName</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>com.jeeplus.modules.sys.entity.User getByLoginName(java.lang.String)</function-signature>
        <example>${fns:getByLoginName(loginName)}</example>
    </function>

    <function>
        <description>根据当前用户id获取用户头像</description>
        <name>getUserPhoto</name>
        <function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
        <function-signature>com.jeeplus.modules.sys.entity.User getUserPhoto(java.lang.String)</function-signature>
        <example>${fns:getUserPhoto(userId)}</example>
    </function>


    <!-- 绑定学年 -->
    <function>
        <description>绑定学年</description>
        <name>getSchoolYearList</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>com.jeeplus.modules.sys.utils.DictUtils getSchoolYearList()</function-signature>
        <example>${fns:getSchoolYearList()}</example>  
    </function>

    <function>
        <description>获取项目盘符地址</description>
        <name>getPubEnclosureDirPath</name>
        <function-class>com.jeeplus.common.config.Global</function-class>
        <function-signature>java.lang.String getPubEnclosureDirPath()</function-signature>
        <example>${fns:getPubEnclosureDirPath()}</example>
    </function>

</taglib>

3、 为表达式函数(标签函数)提供后台服务类:common.config.Global

/**
 * Copyright &copy; 2015-2020 <a href="http://www.runyu.com/">RunYu</a> All rights reserved.
 */
package com.jeeplus.common.config;

import java.io.File;
import java.io.Fileoutputstream;
import java.io.IOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.text.simpledateformat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.iBATis.io.resources;
import org.Springframework.core.io.DefaultResourceLoader;

import com.ckfinder.connector.ServletcontextFactory;
import com.Google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jeeplus.common.utils.CacheUtils;
import com.jeeplus.common.utils.PropertiesLoader;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.SysBase;

/**
 * 全局配置类
 * 
 * @author runyu
 * @version 2014-06-25
 */
public class Global {

    /**
     * 当前对象实例
     */
    private static Global global = new Global();

    /**
     * 保存全局属性值
     */
    private static Map<String, String> map = Maps.newHashMap();

    /**
     * 属性文件加载对象
     */
    private static PropertiesLoader loader = new PropertiesLoader("runyu.properties");

    /**
     * 显示/隐藏
     */
    public static final String SHOW = "1";
    public static final String HIDE = "0";

    /**
     * 是/否
     */
    public static final String YES = "1";
    public static final String NO = "0";

    /**
     * 对/错
     */
    public static final String TRUE = "true";
    public static final String FALSE = "false";

    /**
     * 上传文件基础虚拟路径
     */
    public static final String USERFILES_BASE_URL = "/userFiles/";

    /**
     * 上传文件基础虚拟路径
     */
    public static final String USERFILES_URL = "/admin/logo/";

    /**
     * 上传图片基础虚拟路径
     */
    public static final String IMAGE_BASE_URL = "/logo/";

    /**
     * 获取当前对象实例
     */
    public static Global getInstance() {
        return global;
    }

    public static boolean doauthorization = false;
    public static final String AUTHORIZATIONCACHE = "AuthorizationCache";
    public static final String AUTHORIZATIONCACHEKEY = "Authorization";

    /**
     * 获取配置
     * 
     * @see ${fns:getConfig('adminPath')}
     */
    @SuppressWarnings("unchecked")
    public static String getConfig(String key) {
        // 验证拦截器是否已经被调用。如果没有被调用,抛出错误
//      System.out.println("--------------------------------------------doauthorization:" + doAuthorization
//              + "--------------------------------------------");
//      if (doAuthorization) {
//          HashMap<String, Boolean> mapAuthorization = (HashMap<String, Boolean>) CacheUtils.get(AUTHORIZATIONCACHE,
//                  AUTHORIZATIONCACHEKEY);
//          String sdate = "";
//          Date ddate = new Date();
//          sdate = (new SimpleDateFormat("yyyyMMdd")).format(ddate);
//          System.out.println("--------------------------------------------sdate:" + sdate
//                  + "--------------------------------------------");
//          if (mapAuthorization == null) {
//              System.out.println("--------------------------------------------mapAuthorization == null:" + true
//                      + "--------------------------------------------");
//
//          } else {
//              System.out.println("--------------------------------------------mapAuthorization == null:" + false
//                      + "--------------------------------------------");
//
//          }
//          // mapAuthorization=null;
//          if (mapAuthorization == null) {
//
//              // 缓存为空时,重新调用api接口重置缓存
//              doAuthorization = false;
//              HttpUtils.RemoteAuthorizationCheck();
//              mapAuthorization = (HashMap<String, Boolean>) CacheUtils.get(AUTHORIZATIONCACHE, AUTHORIZATIONCACHEKEY);
//              doAuthorization = true;
//          }
//          System.out.println("--------------------------------------------mapAuthorization:" + mapAuthorization
//                  + "--------------------------------------------");
//
//          if (mapAuthorization.get(sdate) == null) {
//              // 缓存为空时,重新调用api接口重置缓存
//              doAuthorization = false;
//              HttpUtils.RemoteAuthorizationCheck();
//              mapAuthorization = (HashMap<String, Boolean>) CacheUtils.get(AUTHORIZATIONCACHE, AUTHORIZATIONCACHEKEY);
//              doAuthorization = true;
//              mapAuthorization = (HashMap<String, Boolean>) CacheUtils.get(AUTHORIZATIONCACHE, AUTHORIZATIONCACHEKEY);
//              System.out.println("--------------------------------------------mapAuthorization22222:"
//                      + mapAuthorization + "--------------------------------------------");
//
//              // 重置后如果仍为空,抛出错误
//              if (mapAuthorization.get(sdate) == null) {
//                  throw new NullPointerException();
//              }
//              System.out.println("--------------------------------------------result:" + mapAuthorization.get(sdate)
//                      + "--------------------------------------------");
//
//          } else {
//              System.out.println("--------------------------------------------result2:" + mapAuthorization.get(sdate)
//                      + "--------------------------------------------");
//
//              if (mapAuthorization.get(sdate) == false) {
//                  throw new NullPointerException();
//              }
//          }
//      }

        String value = map.get(key);
        if (value == null) {
            value = loader.getProperty(key);
            map.put(key, value != null ? value : StringUtils.empty);
        }
        return value;
    }

    /**
     * 获取管理端根路径
     */
    public static String getAdminPath() {
        return getConfig("adminPath");
    }

    /**
     * 获取前端根路径
     */
    public static String getFrontPath() {
        return getConfig("frontPath");
    }

    /**
     * 获取URL后缀
     */
    public static String getUrlSuffix() {
        return getConfig("urlSuffix");
    }

    /**
     * 是否是演示模式,演示模式下不能修改用户、角色、密码、菜单、授权
     */
    public static Boolean isDemoMode() {
        String dm = getConfig("demoMode");
        return "true".equals(dm) || "1".equals(dm);
    }

    /**
     * 上传服务器IP
     */
    public static String uploadUrl() {
        String uploadUrl = getConfig("UPLOADURL");
        return uploadUrl;
    }

    /**
     * 在修改系统用户和角色时是否同步到Activiti
     */
    public static Boolean isSynActivitiIndetity() {
        String dm = getConfig("activiti.isSynActivitiIndetity");
        return "true".equals(dm) || "1".equals(dm);
    }

    /**
     * 页面获取常量
     * 
     * @see ${fns:getConst('YES')}
     */
    public static Object getConst(String field) {
        try {
            return Global.class.getField(field).get(null);
        } catch (Exception e) {
            // 异常代表无配置,这里什么也不做
        }
        return null;
    }

    /**
     * 获取上传文件的根目录
     * 
     * @return
     */
    public static String getUserfilesbasedir() {
        String dir = getConfig("userfiles.basedir");
        if (StringUtils.isBlank(dir)) {
            try {
                dir = ServletContextFactory.getServletContext().getrealpath("/");
            } catch (Exception e) {
                return "";
            }
        }
        if (!dir.endsWith("/")) {
            dir += "/";
        }
        // System.out.println("userfiles.basedir: " + dir);
        return dir;
    }

    /**
     * 获取工程路径
     * 
     * @return
     */
    public static String getProjectPath() {
        // 如果配置了工程路径,则直接返回,否则自动获取。
        String projectPath = Global.getConfig("projectPath");
        if (StringUtils.isNotBlank(projectPath)) {
            return projectPath;
        }
        try {
            File file = new DefaultResourceLoader().getResource("").getFile();
            if (file != null) {
                while (true) {
                    File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
                    if (f == null || f.exists()) {
                        break;
                    }
                    if (file.getparentFile() != null) {
                        file = file.getParentFile();
                    } else {
                        break;
                    }
                }
                projectPath = file.toString();
            }
        } catch (IOException e) {
            e.printstacktrace();
        }
        return projectPath;
    }

    /**
     * 获取根目录路径
     * 
     * @return
     */
    public static String getDirPath() {
        String projectPath = getProjectPath();
        Properties prop = System.getProperties();
        String os = prop.getProperty("os.name");
        if (os.startsWith("win") || os.startsWith("Win"))
            projectPath = projectPath.split(":")[0].toString() + ":\\userFiles\\";
        else
            projectPath = "/home/userFiles/";
        return projectPath;
    }

    /**
     * 获取项目盘符地址
     * 
     * @return
     */
    public static String getPubEnclosureDirPath() {
        String projectPath = HttpUtils.getPubEnclosureDirPath();
        return projectPath;
    }

    /**
     * 获取项目名称
     * 
     * 
     */
    public static String getProjectName() {
        String ProjectName = "";
        Gson gson = new Gson();
        String param = "";
        String json = "";

        try {
            json = HttpUtils.doPost("sys/dictInterface/findBaseConfig", param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SysBase result = null;
        try {
            result = gson.fromJson(json, new TypeToken<SysBase>() {
            }.getType());

        } catch (Exception e) {
            // TODO: handle exception
        }
        ProjectName = result.getProjectname().trim();
        return ProjectName;
    }

    /**
     * 获取开发公司名称
     * 
     * 
     */
    public static String getCompanyName() {
        String CompanyName = "";
        Gson gson = new Gson();
        String param = "";
        String json = "";

        try {
            json = HttpUtils.doPost("sys/dictInterface/findBaseConfig", param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SysBase result = null;
        try {
            result = gson.fromJson(json, new TypeToken<SysBase>() {
            }.getType());

        } catch (Exception e) {
            // TODO: handle exception
        }
        CompanyName = result.getCompanyname().trim();
        return CompanyName;
    }

    /**
     * 获取是否可以从网盘选择文件
     * 
     * 
     */
    public static SysBase getSysBase() {

        Gson gson = new Gson();
        String param = "";
        String json = "";

        try {
            json = HttpUtils.doPost("sys/dictInterface/findBaseConfig", param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SysBase result = null;
        try {
            result = gson.fromJson(json, new TypeToken<SysBase>() {
            }.getType());

        } catch (Exception e) {
            // TODO: handle exception
        }

        return result;
    }

    /**
     * 写入properties信息
     *
     * @param key
     *            名称
     * @param value
     *            值
     */
    public static void modifyConfig(String key, String value) {
        try {
            // 从输入流中读取属性列表(键和元素对)
            Properties prop = getProperties();
            prop.setProperty(key, value);
            String path = Global.class.getResource("/runyu.properties").getPath();
            FileOutputStream outputFile = new FileOutputStream(path);
            prop.store(outputFile, "modify");
            outputFile.close();
            outputFile.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回 Properties
     * 
     * @param fileName
     *            文件名 (注意:加载的是src下的文件,如果在某个包下.请把包名加上)
     * @param
     * @return
     */
    public static Properties getProperties() {
        Properties prop = new Properties();
        try {
            Reader reader = Resources.getResourceAsReader("/runyu.properties");
            prop.load(reader);
        } catch (Exception e) {
            return null;
        }
        return prop;
    }

}

4、在jsp页面或js中使用自定义的表达式函数fns:

这里写图片描述

这里写图片描述

相关阅读

nofollow标签的作用

  当我们网站建好之后,就要开始准备在搜索引擎上推广,但是很多新手站长并不了解nofollow标签的作用和使用方法,当你去跟对方交换友

网站meta标签有什么用

当代的企业官网许多都刚开始应用META标识提升,根据这类方法能够给与网站1个非常好地提升实际效果,从而提升网站的经济收益。下面,SEO

H1标签是指网页html中的作用有哪些?

H1标签是指网页html中的作用有哪些?H1标签是指网页html中对文本标题所进行的着重强调的一种标签,制作标签的主要是起强调作用,告诉

K线图 自定义K线图

Android股票K线图 纯自定义View 仿火币页面先放几张效果图,不是太会录,动态图做的不太好将就看Demo 地址先放几张效果图,不是太会录,

HTML标签img--改变图片尺寸

1、统一大小? 我的网页上面有许多的图片,有的大,有的小,我想如果图片大的实现缩放,所有的都是一般大。来看看没有是什么效果。 大家看

分享到:

栏目导航

推荐阅读

热门阅读