el表达式的使用细节补充

来源:互联网 发布:前端 后端 知乎 编辑:程序博客网 时间:2024/05/01 20:06

细节补充
●功能
·访问存储对象
  当内容为空时 el内部自动转换成空串  ${user }
  原因是el表达式访问数据他是表现层的一个技术 尽量规避空指针
·取javabean的属性
  1)${user.username }访问user对象的username属性(找getUsername方法而不是找属性)
  2)${user.address.city }访问user对象的address属性的city属性
·取数组、list或map
  1)${arrayname[index]}
  2)${listname[index]}
  3)${mapname.key} key不能取数字 当key为数字时${mapname.["1"]}
·二元
  ${3>2?'大于':'小于'}
●el中的隐式对象
  1)pageContext
    ${pageContext.request.ContextPath}
    使用场景:拿到web应用根路径 在提交表单和链接的时候可以用
  2)param 一个map集合 封装了所有的请求参数
    ${param }
    使用场景:request由servlet转发到页面后可以获得数据${param.username }做回写免得用户每次都要重新写
    ${param.gender=='男'?'selected':'' }
  3)paramValues一个map集合 key是参数名 value 是参数值
    ${paramValues.preference[0]}||{paramValues.preference[1]}
  4)header map封装所有请求消息头 由于有-有歧义 所以用[]语法
    ${header["accept-encoding"] }
  5)headerValues map封装所有请求消息头
  6)pageScope
  7)requestScope
  8)sessionScope session域
    ${sessionScope.user}
  9)applicationScope
  10)cookie name当做key value是对象
    ${cookie.JSESSIONID}拿到cookie对象
    ${cookie.JSESSIONID.name}
    ${cookie.JSESSIONID.value}
  11)初始化参数initParam
    ${initParam.encoding }
●el函数
  <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/function"%>
  函数调用在el表达式中采用 前缀+冒号+函数名
  1)取长度 (hello="helloWorld")
  ${fn:length(hello)}
  ${fn:length(list)}
  ${fn:split(formBean.birthday, '-')[0] }
  ${fn:contains('唱歌')?'checked':'' }
  2)自定义函数
  i.建立类
 ii.建立 必须公共静态 方法
iii.在web-inf/建立File XXX.tld
  将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/j2eehttp://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</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>my</short-name>
    <uri>http://java.sun.com/jsp/jstl/functions</uri> 这么写总出错 /WEB-INF/myfn.tld这么写可以

    <function>
      <name>say</name>  页面调用时叫什么这里就写什么
      <function-class>com.bjsxt.struts.MyFunctions</function-class>方法的类
      <function-signature>java.lang.String sayHello(java.lang.String)
      </function-signature>方法的描述
    </function>
  </taglib>

  前台可以调用
  <%@ taglib prefix="上面定义的shortname" uri="上面定义的uri" %>
  ${my:say("Tom")}
●el函数库
·param.preference包含唱歌
  ${fn:contains(param.preference, '唱歌')?'checked':'' }
·以-连接
  ${fn:join(paramValues.skill,'-' )}
·转译html标签
  ${fn:escapeXml('<a href="#">aaaaa</a>') }
·截取0-10
  ${fn:substring(requestScope.description, 0, 10 )}
·拿到长度
  ${fn:length(requestScope.description)>10?'...':'' }
●格式化
        // 获得请求参数
     String language = request.getParameter("language");
     Locale locale = request.getLocale();
  if(language!=null) {
      // 断开
      String[] parts = language.split("-");
      // 创建 locale
      locale = new Locale(parts[0], parts[1]);
     }
     // 国际化
     // 日期
     DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG, locale);
        System.out.println(df.format(new Date()));
     // 数字
     
     NumberFormat nf1 = NumberFormat.getNumberInstance(locale);
        System.out.println(nf1.format(23.78));
     // 货币
     NumberFormat nf2 = NumberFormat.getCurrencyInstance(locale);
 System.out.println(nf2.format(49.66));
     // 百分比
     
     // 对于静态文本读取资源文件
     ResourceBundle bundle = ResourceBundle.getBundle("myresource", locale);