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,输入的表单内容:
这里的文字会在对象创建的时候打印出来 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>
<%@ 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,输入的表单内容:
这里的文字会在对象创建的时候打印出来 br/>zero
789@12.com
16
<jsp:useBean id="stu" class="org.zero01.test.Student" scope="session">
1.先在session中查找有没有stu
2.有的话就直接拿出来,不会创建对象
3.没有的话,就创建对象,存到session中之后再拿出来
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));
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");
<%@ 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,打印结果如下:
<%@ 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,打印结果如下:
<%@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,打印结果如下:
除了这种只写属性名称让它自动去匹配的方式之外,还可以指定在某个对象中拿值,如下:
${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,打印结果如下:
但是有一点要注意的就是,这个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,打印结果如下:
<%@ 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>
<%@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
学生名字: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
<?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>
<%@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
<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));
}
}
以上只是一个十分简单的示例,目的是为了稍作了解而已,知道除了EL表达式外还有这种表达式。
本文转自 ZeroOne01 51CTO博客,原文链接:http://blog.51cto.com/zero01/2053613,如需转载请自行联系原作者