JavaEE EL的一些用法

EL
可以在指示元素中设置EL是否使用 isELIgnored="true" true是不使用
也可以在web.xml中使用 
<jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</ url-pattern>
        <el-ignored>true</el-ignored>来设置
如果两个都设置了,则在指示元素中设置 是定义的有效

${param.a}
 a 是你要请求发送的名字。
<form action="/jsp1/jsp5.jsp">
           <input type="text" name="a"/>
           <input type="submit" value="ok"/>
       </form>
jp5.jsp
就可以用 ${a} 来得到input 的 value
当 a==null时 ${a}的结果是空字符,不会报错。
.运算
((HttpServletRequest)pageContext.getRequest).getMethod()
== -->     pageContext.request.method 可以自动转换类型


EL还可以取数组 假如请求的是一个数组元素 
String[] names={"a","b","c"};
          application.setAttribute("array",names);
      ${array[0]}
      ${array[1]}
      ${array[2]}
而且还可以用arrayList,hashMap。
hashMap可以用. 也可以用[] 用[]好点
HashMap hm=new HashMap();
          hm.put("ni hao","wo bu hao");
          application.setAttribute("array",hm);

${array["ni hao"]}


EL的隐含对象 
1.    pageContext
相当于jsp的PageContext
使用方法 ${pageContext.xxx}
2.    与属性相关的隐含对象
pageScope, requestScope, sessionScope, applicationScope
3.    与请求参数相关的隐含对象
param  ${param.user} == <%= request.getParameter(“user”)%>
paramValues ${paramValues.favorites[1]} == <%= request.getParameterValues(“favorites”)%>
4.    与标头相关的隐含对象
如果取用户请求的表头数据,则可以使用header,headerValues隐含对象
<%=request.getHeader(“user-agent”)%> == ${header[“user-agent”]}
5 .  cookie隐含对象
可以取用户的Cookie设置值,例:Cookie中有一个userName的属性
则可以${cookie.userName}来取出;

5.    初始化参数隐含对象
initParam  这个是web.xml设置的ServletContext初始化参数  就是<context-param>中的参数
$(initParam.initCount) == <%=servlet.getInitParameter(“initCount”)%>

EL自定义函数
java类
package cc.openhome;

public class InFix {
    
    public static double eval(String infix){
        
        return Double.parseDouble(infix)+2;
    }

}
WEB-INF下的  infix.tld
<?xml version="1.0" encoding="utf-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLShema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2eeweb-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<short-name>infix</short-name> <!-- 实现实例类的名称 -->
<uri>http://cc.openhome/infix</uri> <!-- 设置uri的对应名称 -->
<function>
    <description>Eval Infix</description> <!-- 设置 -->
    <name>eval</name>  <!-- 自定义EL函数名称 -->
    <function-class>cc.openhome.InFix</function-class> <!-- 对应到那个类 -->
    <function-signature>double eval(java.lang.String)</function-signature>
</function>
</taglib>

实用到jsp中
%@taglib uri="http://cc.openhome/infix" prefix="infix"%
${infix:eval("56")}

原文地址:https://www.cnblogs.com/shaoshao/p/3380396.html