EL表达式

来源:互联网 发布:上海交大矩阵理论作业 编辑:程序博客网 时间:2024/06/05 19:28

El

      获取数据

某个web域中获取对象,访问javabean的属性、访问list集合、访问map集合、访问数组等

         如果el表达式在域中获取不到数据则显示空字符串(“”)而不是null

         El表达式获取到的数据会自动转化为数据所对应的类型(如获取int类型的数据会自动转化为int类型)

         用el表达式访问list数组下标越界不会报错

         empty用于判断值是否为null或””,是的话返回true

      禁用EL

1.       \${};

2.       <%@page isELIgnored=”true” %>

3.       <jsp-config>

        <jsp-property-group>

                 <url-pattern>*.jsp</url-pattern>

                 <el-ignored>true</el-ignored>

        </jsp-property-group>

</jsp-config>

EL的隐含对象

页面上下文对象:pageContext

用于访问jsp内置对象(如request response out session exception page等)

如返回端口号${pageContext.request.serverPort}这里为8080

${pageContext.response.contentType}返回text/html;charset=UTF-8

${pageContext.out.bufferSize}返回缓存区的大小,8192

${pageContext.servletContext.contextPath}返回当前页面的上下文路径

访问作用域隐含对象:requestScope、responseScope、sessionScope、pageScope、appliScope

<%

  request.setAttribute(“username”,”李旺”);

%>

${requestScope.username};

访问环境信息的隐含对象

param对象

jsp页面<input type=”text” name=”name” />

表单提交后获得文本框的值:${param.name}

paramValues对象

<input type="checkbox" id="affect"  value="登山"/>登山

<input type="checkbox" id="affect"  value="跑步"/>跑步

表单提交后:${paramValues.affect[0]}${ paramValues.affect[1]}

header和headerValues对象

initParam对象(获得web应用的web.xml文件中设置一个初始化参数author,用于指定作者)

  <context-param>

    <param-name>author</param-name>

    <param-value>mr</param-value>

  </context-param>

${initParam.author}

cookie对象

<%

    Cookie cookie=new Cookie("username","123");

    response.addCookie(cookie);

%>

${cookie.username.value}

定义和使用el函数

1.java类

package base;

public class StringDeal {

    public static String shiftEnter(String str){

        StringnewStr = str.replaceAll("\r\n","<br>");//替换回车换行符

        newStr= newStr.replaceAll("","&nbsp;");//替换空格符

        System.out.println(newStr);

        return newStr;

    }

}

2.  编写stringDeal.tld,并将其保存在 WEB-INF文件夹下

<?xmlversion="1.0"encoding="UTF-8"?>

<taglibxmlns="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

    web-jsptaglibrary_2_0.xsd"

    version="2.0">

    <tlib-version>1.0</tlib-version>

    <uri>/sD</uri>

    <function>

        <name>shiftEnter</name>

        <function-class>base.StringDeal</function-class>

        <function-signature>java.lang.String shiftEnter(java.lang.String)

        </function-signature>

    </function>

</taglib>

 

参数说明: 

    uri:指定tld文件的映射路径。在应用EL函数的时候,需要使用改标记指定内容

    name:用于指定EL函数对应的方法名,通常与java文件中的方法名相同

    <function-class>:指定对对应的java文件,包括包名和类名

   <function-signature>:指定EL函数所对应的静态方法,这里包括返回值的类型和入口参数的类型,必须使用完整的类名,不能指定为“String shiftEnter(String)”

3.  提交的jsp界面

<!-- 收集内容 -->

<formname="form1"method="post"action="deal.jsp">

  <textareaname="content"cols="30"rows="5"></textarea>

  <br>

  <inputtype="submit"name="Button"value="提交">

</form>

4.  结果界面

<%@ page language="java"contentType="text/html;charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib uri="/sD"prefix="wghfn"%>

<%request.setCharacterEncoding("UTF-8");%>

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>显示结果</title>

</head>

<body>

内容为:<br>

${wghfn:shiftEnter(param.content)}

</body>

</html>


原创粉丝点击