JSP Tag标签与EL表达式

JSP Tag标签

jsp tag标签外表长得像html、xml等标记语言,但是内部其实就是封装了的Java代码。通过jsp标签可以简化一些需要在jsp上编写的Java代码,虽说是标签,但是jsp只会在服务器上运行,不会跑到浏览器上运行,毕竟jsp归根结底只是一个Servlet,以下介绍几个常见的jsp标签:
1.重定向标签:

<jsp:forward page="index.jsp"></jsp:forward>

这个标签与以下这段代码等价,都是内部转发:

<%
    request.getRequestDispatcher("index.jsp").forward(request, response);
%>

index.jsp代码:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Hello I'm index.jsp</h1>
</body>
</html>

使用Java代码转发示例:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <%
        request.getRequestDispatcher("index.jsp").forward(request, response);
    %>
</body>
</html>

访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:

Hello I'm index.jsp

使用jsp标签转发示例:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <jsp:forward page="index.jsp"></jsp:forward>
</body>
</html>

访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:

Hello I'm index.jsp

2.导入某页面运行结果标签:

<jsp:include page="head.jsp"></jsp:include>

这个标签和jsp指令中的 <%@ include file="head.jsp" %> 类似,只不过指令是将head.jsp的页面内容导入到当前页面中,而jsp标签则是仅导入head.jsp页面的运行结果。它们两者的区别在于这里。使用示例:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <jsp:include page="index.jsp"></jsp:include>
</body>
</html>

访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:

Hello I'm index.jsp

3.配置参数标签:

<jsp:param value="lisi" name="name"/>

这个标签是用来配置参数的,比如转发时带上参数,或者导入某页面结果时加上参数,不过这个标签必须配合jsp:include、jsp:forward、jsp:plugin等标签来进行使用,不然就会报以下错误:

严重: Servlet.service() for servlet [jsp] in context with path [/jsp-tag_Test] threw exception [/test.jsp (line: [9], column: [6]) The jsp:param action must not be used outside the jsp:include, jsp:forward, or jsp:params elements] with root cause
org.apache.jasper.JasperException: /test.jsp (line: [9], column: [6]) The jsp:param action must not be used outside the jsp:include, jsp:forward, or jsp:params elements

1.jsp:param与jsp:include配合使用:
test.jsp代码:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <jsp:include page="index.jsp">
        <jsp:param value="lisi" name="name" />
    </jsp:include>
</body>
</html>

index.jsp代码:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <h1><%=request.getParameter("name") %></h1>
</body>
</html>

访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:

lisi

2.jsp:param与jsp:forward配合使用:
test.jsp代码:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <jsp:forward page="index.jsp">
        <jsp:param value="lisi" name="name" />
    </jsp:forward>
</body>
</html>

index.jsp代码与之前一致。

访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:

lisi

以上三个标签是比较常用的。

4.此标签用来在jsp页面中创建一个Bean实例:

<jsp:useBean id=""></jsp:useBean> 

创建后的Bean示例可以存储在request、session 、application(servletcontext)、page 等jsp内置对象中。

Java Bean简介:
Java语言欠缺属性、事件、多重继承功能。所以,如果要在Java程序中实现一些面向对象编程的常见需求,只能手写大量胶水代码。Java Bean正是编写这套胶水代码的惯用模式或约定。这些约定包括getXxx、setXxx、isXxx、addXxxListener、XxxEvent等。遵守上述约定的类可以用于若干工具或库。

使用jsp:useBean创建Bean实例对象,示例:
1.我有一个Student类,代码如下:

public class Student {

    private String sname;

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private String email;
    private int age;

}

2.test.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <!-- id是这个对象的唯一标识,class里的值是该类的全名,scope是指定该实例存储在哪个内置对象中 -->
    <jsp:useBean id="stu" class="org.zero01.test.Student" scope="request">这里的文字会在对象创建的时候打印出来</jsp:useBean>
</body>
</html>

访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:

这里的文字会在对象创建的时候打印出来

5.此标签用于设置Bean对象的属性值:
property的值为对象的属性名称,name的值为jsp:useBean中定义的id值,param的值为html表单中的name属性的值

<jsp:setProperty property="sname" name="stu" param="sname" />

6.此标签用于得到Bean对象的属性值,默认会将值打印出来:
同样的property的值为对象的属性名称,name的值为jsp:useBean中定义的id值

<jsp:getProperty property="sname" name="stu" />

示例:
index.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <form action="test.jsp">
        <input type="text" name="form_sname" placeholder="学生名字" /><br>
        <input type="text" name="form_email" placeholder="邮箱地址" /><br>
        <input type="text" name="form_age" placeholder="年龄" /><br>
        <button type="submit">提交</button>
    </form>
</body>
</html>

test.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <jsp:useBean id="stu" class="org.zero01.test.Student" scope="request">
        这里的文字会在对象创建的时候打印出来
    </jsp:useBean><br>

    <jsp:setProperty property="sname" name="stu" param="form_sname"/>
    <jsp:setProperty property="email" name="stu" param="form_email"/>
    <jsp:setProperty property="age" name="stu" param="form_age"/>

    <jsp:getProperty property="sname" name="stu"/><br>
    <jsp:getProperty property="email" name="stu"/><br>
    <jsp:getProperty property="age" name="stu"/>
</body>
</html>

访问http://localhost:8080/jsp-tag_Test/index.jsp,输入的表单内容:

lisibr/>123@12.com
15

跳转到test.jsp后的打印结果:

这里的文字会在对象创建的时候打印出来 br/>lisi
123@12.com
15

如果表单中的name属性值与Bean对象的属性名称一致的话,可以使用以下方式,自动匹配相同的名称:
index.jsp表单代码如下:

<form action="test.jsp">
        <input type="text" name="sname" placeholder="学生名字" /><br>
        <input type="text" name="email" placeholder="邮箱地址" /><br>
        <input type="text" name="age" placeholder="年龄" /><br>
        <button type="submit">提交</button>
</form>

test.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <jsp:useBean id="stu" class="org.zero01.test.Student" scope="request">
        这里的文字会在对象创建的时候打印出来
    </jsp:useBean><br>

    <!-- property的值为*会自动匹配表单中相同的名称 -->
    <jsp:setProperty property="*" name="stu"/>

    <jsp:getProperty property="sname" name="stu"/><br>
    <jsp:getProperty property="email" name="stu"/><br>
    <jsp:getProperty property="age" name="stu"/>
</body>
</html>

访问http://localhost:8080/jsp-tag_Test/index.jsp,输入的表单内容:

zerobr/>789@12.com
16

跳转到test.jsp后的打印结果:

这里的文字会在对象创建的时候打印出来 br/>zero
789@12.com
16

关于Bean实例存储在session对象中的那拿值过程:

<jsp:useBean id="stu" class="org.zero01.test.Student" scope="session">

1.先在session中查找有没有stu
2.有的话就直接拿出来,不会创建对象
3.没有的话,就创建对象,存到session中之后再拿出来

EL表达式

EL(Expression Language) 是为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法,让Jsp的代码更加简化。EL表达式和jsp标签一样,内部实际就是java代码,以下是生成的源码:

使用EL表达式后生成的源码:

out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${param.sname}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));

使用jsp标签后生成的源码:

out.write("<br>\r\n");
      out.write("\t\r\n");
      out.write("\t<!-- property的值为*会自动匹配表单中相同的名称 -->\r\n");
      out.write("\t");
      org.apache.jasper.runtime.JspRuntimeLibrary.introspect(_jspx_page_context.findAttribute("stu"), request);
      out.write("\r\n");
      out.write("\t\r\n");
      out.write("\t");
      out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((org.zero01.test.Student)_jspx_page_context.findAttribute("stu")).getSname())));
      out.write("<br>\r\n");
      out.write("\t");
      out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((org.zero01.test.Student)_jspx_page_context.findAttribute("stu")).getEmail())));
      out.write("<br>\r\n");
      out.write("\t");
      out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((org.zero01.test.Student)_jspx_page_context.findAttribute("stu")).getAge())));
      out.write("\r\n");

EL表达式的语法结构:
${expression}

基本语法示例:

${name}

以上这个表达式会拿到一个值,这个表达式的拿值过程:
1.先在当前页面中的pageContext里找有没有这个名为name的值
2.没有的话就会去request对象里找
3.request也没有的话就去session里找
4.session都没有的话就去application(servletContext)里找
5.如果在这些对象中都找不到的话也不会报错,也不会打印null,而是什么都不显示。

示例:
test.jsp代码:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <p>${name};</p>
</body>
</html>

访问http://localhost:8080/EL-Test/test.jsp,显示空白,因为这个以上代码并没有表达式中所需要的值。

如果我在pageContext对象里设置一个name属性,看看是否能获得到,代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<% 
    pageContext.setAttribute("name", "lisi");
%>
    <p>${name}</p>
</body>
</html>

访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:

lisi

或者在request里设置一个name属性,代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<% 
    request.setAttribute("name", "request");
%>
    <p>${name}</p>
</body>
</html>

访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:

request

以上实验就可以知道,只要这些对象其中有一个name属性值,就可以被这个表达式获得到。以上也提到了这种表达式寻找值是先从pageContext对象中找的,所以来实验一下是否是先从pageContext对象中找,代码如下:

<%@page import="org.zero01.test.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <%
        request.setAttribute("name", "request");
        session.setAttribute("name", "session");
        pageContext.setAttribute("name", "pageContext");
        application.setAttribute("name", "application");
    %>

    ${name}
</body>
</html>

访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:

pageContext

除了这种只写属性名称让它自动去匹配的方式之外,还可以指定在某个对象中拿值,如下:

${pageScope.name}
${requestScope.name}
${sessionScope.name}
${applicationScope.name}

还可以访问某个对象的属性,例如我在页面上实例化了Student对象,设置了属性值之后放进了request对象中,然后就可以通过el表达式去获得这个Student对象的属性值,示例:

<%@page import="org.zero01.test.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <%
        Student student = new Student();
        student.setSname("lisi");
        student.setAge(16);
        student.setEmail("1564@xx.com");

        request.setAttribute("student", student);
    %>

    ${student.sname}<br>
    ${student.email}<br>
    ${student.age}<br>
</body>
</html>

访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:

lisibr/>1564@xx.com
16

但是有一点要注意的就是,这个Student实例对象必须要有get方法才能获得到相应的属性值,否则是无法获得的。

如果Student实例对象中有一个属性存储的是对象,那么在el表达式中也可以拿到该对象的的属性值:

<%@page import="org.zero01.test.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <%
        Student stu = new Student();
        stu.setSname("lisi");
        stu.setAge(16);
        stu.setEmail("1564@xx.com");
        stu.setStudent(stu);

        request.setAttribute("stu", stu);
    %>

    ${requestScope.stu.student.sname}
    <br> 
    ${requestScope.stu.student.email}
    <br> 
    ${requestScope.stu.student.age}
    <br>
</body>
</html>

访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:

lisibr/>1564@xx.com
16

除了可以在以上对象中拿值外,还可在以下对象中拿值:
1.param: 表单参数对象
2.paramValues:重复的表单参数的对象
3.header: http请求头参数对象
4.headerValues:重复的http请求头参数的对象
5.cookie: cookie对象
6.initParam: 通过这个对象,可以拿到servletContxt对象初始化时配置的参数值

示例:
index.jsp代码:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <form action="test.jsp">
        <input type="text" name="sname" placeholder="学生名字" /><br>
        <input type="text" name="sname" placeholder="学生名字" /><br>
        <input type="text" name="sname" placeholder="学生名字" /><br>
        <input type="text" name="email" placeholder="邮箱地址" /><br>
        <input type="text" name="age" placeholder="年龄" /><br>
        <button type="submit">提交</button>
    </form>
</body>
</html>

test.jsp代码:

<%@page import="org.zero01.test.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    学生名字:${param.sname}<br>
    邮箱地址:${param.email}<br>
    年龄:${param.age}<br>
    type:${paramValues.sname[2]}<br>
    host:${header.host}<br>
    Accept:${headerValues.Accept[0]}<br>
    cookie:${cookie.JSESSIONID.value}<br>
</body>
</html>

访问http://localhost:8080/EL-Test/index.jsp,输入的表单内容如下:

lisi1br/>lisi2
lisi3
1563@xx.com
16

跳转到test.jsp后的打印结果:

学生名字:lisi1
邮箱地址:1563@xx.com
年龄:16
type:lisi3
host:localhost:8080
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
cookie:E936637B285D60CA96B7CC3D258EBD50

initParam 示例:
web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <context-param>
        <param-name>name</param-name>
        <param-value>一拳超人</param-value>
    </context-param>

</web-app> 

test.jsp代码如下:

<%@page import="org.zero01.test.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    ${initParam.name}<br>
</body>
</html>

重启Tomcat后,访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:

一拳超人

el表达式除了可以从各种对象中获得值之外还可以使用算术运算符、关系运算符、逻辑运算符等运算符,进行相关的运算,示例:

<%
  request.setAttribute("num", "1000.5");
%>

${num / 100}<br>
${num * 100}<br>
${num + 100}<br>
${num - 100}<br>
${num % 100}<br>
${num == 1000.05}<br>
${num > 1000.05}<br>
${num < 1000.05}<br>
${num >= 1000.05}<br>
${num <= 1000.05}<br>
${num != 1000.05}<br>
${num == 1000.05 && true}<br>
${num == 1000.05 || false}<br>
${num != 1000.05 ? 8:9}<br>

访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:

10.005 
100050.0 
1100.5 
900.5 
0.5 
false 
true 
false 
true 
false 
true 
false 
false 
8

除了以上这种常见的运算符形式外,还可使用英文单词缩写的形式,(注意,加减乘,这三项不支持缩写的形式)如下:

<%
  request.setAttribute("num", 1000.5);
%>

${num div 100}<br>
${num * 100}<br>
${num + 100}<br>
${num - 100}<br>
${num mod 100}<br>
${num eq 1000.05}<br>
${num gt 1000.05}<br>
${num lt 1000.05}<br>
${num ge 1000.05}<br>
${num le 1000.05}<br>
${num ne 1000.05}<br>
${num eq 1000.05 and true}<br>
${num eq 1000.05 or false}<br>
${num ne 1000.05 ? 8:9}<br>

访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:

10.005 
100050.0 
1100.5 
900.5 
0.5 
false 
true 
false 
true 
false 
true 
false 
false 
8

关于OGNL表达式:
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言,通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。所以OGNL和EL表达式蛮像的,在一些需要动态变化字段的场景下,OGNL非常的有用,例如流程控制语句中的条件需要动态变化的情况下,就需要使用OGNL。

下面简单演示一下OGNL,maven工程配置如下依赖:

<dependency>
    <groupId>ognl</groupId>
    <artifactId>ognl</artifactId>
    <version>3.0.4</version>
</dependency>

编写代码如下:

package org.zero01.test;

import java.util.Hashtable;

import ognl.Ognl;
import ognl.OgnlException;

public class TestOGUL{

    public static void main(String[] args) throws OgnlException {

        Student stu=new Student();

        //使用hashtable作为表达式的root,也就是根元素
        Hashtable<String, Object> hashtable=new Hashtable<String, Object>();
        hashtable.put("stu", stu);

        //设置一个表达式
        Ognl.setValue("stu.sname", hashtable, "李四");
        //得到表达式的值
        System.out.println(Ognl.getValue("stu.sname", hashtable));

        //还可以进行运算
        System.out.println(Ognl.getValue("100*(500+90)*(500+90)", hashtable));
    }
}

运行结果:

李四
34810000

以上只是一个十分简单的示例,目的是为了稍作了解而已,知道除了EL表达式外还有这种表达式。



本文转自 ZeroOne01 51CTO博客,原文链接:http://blog.51cto.com/zero01/2053613,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值