EL表达式、JSTL表达式

154 阅读1分钟

EL表达式

  1. 形式:${}
  2. 作用:
    • 获取数据
    • 执行运算
    • 获取web开发的常用对象
  3. EL表达式不能出现在Java代码块、表达式块等JSP动态代码部分
  4. EL只能从四大域属性空间中获取数据(pageContext、request、session、application)
  5. EL不会抛出空指针异常,只会不显示
  6. EL不会抛出数组越界异常,只会不显示
  7. EL不具有对字符串进行处理的能力(可以使用JSTL的EL或者自定义EL函数)
  8. EL中的运算符
    • 算术运算符:+、-、*、/、% (不支持++、––)
    • 关系运算符:==、!=、>、>=、<、<=
    • 逻辑运算符:!、&&、||、not、and、or
    • 条件运算符:?:
    • 取值运算符:[]、点号
  9. EL隐含对象 param、paramValues

举个例子,制作一个表单,用EL表达式取值

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        
        <form method="post" action="param.jsp">
            <p>
                姓名:<input type="text" name="username" size="30" /> 
            </p>
            <p>
                密码:<input type="password" name="password" size="30" /> 
            </p>
            <p>
                性别:<input type="radio" name="sex" value="男" checked=""/>男
                <input type="radio" name="sex" value="女"/> 女
            </p>
            <p>
                年龄:
                <select name="age">
                    <option value="15">10-20 </option>>
                    <option value="25" selected>20-30 </option>
                    <option value="35">30-40 </option>
                </select>
            </p>
            <p>
                兴趣:
                <input type="checkbox" name="habit" value="studying"/>读书
                <input type="checkbox" name="habit" value="reading"/>看报
                <input type="checkbox" name="habit" value="coding"/>敲代码
                <input type="checkbox" name="habit" value="playing"/>打游戏
                <input type="checkbox" name="habit" value="listenning"/>听歌
            </p>
            <p>
                <input type="submit"  value="前进"/>
                <input type="submit"  value="清除"/>
            </p>            
        </form>

    </body>
</html>

parm.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <% request.setCharacterEncoding("utf-8"); %>
        <h2> EL隐含对象 param、paramValues</h2>
        姓名:${param.username}
        密码:${param.password}
        性别:${param.sex}
        年龄:${param.age}
        兴趣:${paramValues.habit[0]} ${paramValues.habit[1]} ${paramValues.habit[2]} ${paramValues.habit[3]} ${paramValues.habit[4]} 
        兴趣:<c:forEach var="i" begin="0" end="4">
            ${paramValues.habit[i]}
        </c:forEach>   
    </body>
</html>

JSTL表达式

  1. 目的:为了弥补HTML标签的不足;它自定义许多标签,可以供我们使用,标签的功能和Java代码一样!
  2. 用法:
    • 引入对应的taglib
    • 使用其中的方法
    • 在Tomcat 也需要引入 jstl的包,否则会报错:JSTL解析错误
  3. 核心标签 image.png
  4. 举个小例子(有关数据库)
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"
                           url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8"
                           user="root"  password="123456"/>

        <sql:update dataSource="${db}" var="a">
            INSERT INTO subject (subjectno,subjectname,classhour,gradeid) VALUES (19,  '111', 111, 111);
        </sql:update>

        <sql:query dataSource="${db}" var="rs">
            SELECT * from subject 
        </sql:query>
        <table>
            <c:forEach var="row" items="${rs.rows}">
                <tr>
                    <td><c:out value="${row.subjectno}"/></td>
                    <td><c:out value="${row.subjectname}"/></td>
                    <td><c:out value="${row.classhour}"/></td>
                    <td><c:out value="${row.gradeid}"/></td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>
  1. 自定义标签

实现页

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="a" uri="/WEB-INF/tlds/helloTag"%>
<%@taglib prefix="b" uri="/WEB-INF/tlds/fontTag"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <a:helloTag/>
        <b:fontTag color="red">
            easy
        </b:fontTag>
    </body>
</html>

自定义的helloTag标签


package mytag;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;


public class HelloWorldTag extends TagSupport {

    public int doStartTag() throws JspException {
        try {
            pageContext.getOut().print("Hello World!");
        } catch (IOException ioe) {
            throw new JspException(ioe.getMessage());
        }
        return SKIP_BODY;
    }
}

自定义的fontTag标签


package mytag;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;


public class fontTag extends TagSupport {

    private String color = "#000000";

    public int doStartTag() throws JspException {
        StringBuffer sb = new StringBuffer();
        try {
            JspWriter out = pageContext.getOut();
            sb.append("<font ");
            sb.append("color=\'");
            sb.append(color);
            sb.append("\'>");
            out.print(sb.toString());
        } catch (IOException ioe) {
            throw new JspException(ioe.getMessage());
        }
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        try {
            JspWriter out = pageContext.getOut();
            out.println("</font>");
        } catch (IOException ioe) {
            throw new JspException(ioe.getMessage());
        }
        return EVAL_PAGE; //To change body of generated methods, choose Tools | Templates.
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

其中,<%@taglib prefix="a" uri="/WEB-INF/tlds/helloTag"%>和<%@taglib prefix="b" uri="/WEB-INF/tlds/fontTag"%>中的prefix的参数为自己定义的,设置为什么,引用的时候就是什么。uri为你所写的Tag所在的位置。