### JSP Tag标签
jsp tag标签外表长得像html、xml等标记语言,但是内部其实就是封装了的Java代码。通过jsp标签可以简化一些需要在jsp上编写的Java代码,虽说是标签,但是jsp只会在服务器上运行,不会跑到浏览器上运行,毕竟jsp归根结底只是一个Servlet,以下介绍几个常见的jsp标签:
**1.重定向标签:**
```
```
这个标签与以下这段代码等价,都是内部转发:
```
```
index.jsp代码:
```
Insert title here
Hello I'm index.jsp
```
使用Java代码转发示例:
```
Insert title here
```
访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:
> Hello I'm index.jsp
使用jsp标签转发示例:
```
Insert title here
```
访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:
> Hello I'm index.jsp
**2.导入某页面运行结果标签:**
```bash
```
这个标签和jsp指令中的 类似,只不过指令是将head.jsp的页面内容导入到当前页面中,而jsp标签则是仅导入head.jsp页面的运行结果。它们两者的区别在于这里。使用示例:
```
Insert title here
```
访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:
> Hello I'm index.jsp
**3.配置参数标签:**
```bash
```
这个标签是用来配置参数的,比如转发时带上参数,或者导入某页面结果时加上参数,不过这个标签必须配合jsp:include、jsp:forward、jsp:plugin等标签来进行使用,不然就会报以下错误:
```bash
严重: 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代码:
```
Insert title here
```
index.jsp代码:
```
Insert title here
```
访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:
> lisi
2.jsp:param与jsp:forward配合使用:
test.jsp代码:
```
Insert title here
```
index.jsp代码与之前一致。
访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:
> lisi
以上三个标签是比较常用的。
**4.此标签用来在jsp页面中创建一个Bean实例:**
```bash
```
创建后的Bean示例可以存储在request、session 、application(servletcontext)、page 等jsp内置对象中。
Java Bean简介:
Java语言欠缺属性、事件、多重继承功能。所以,如果要在Java程序中实现一些面向对象编程的常见需求,只能手写大量胶水代码。Java Bean正是编写这套胶水代码的惯用模式或约定。这些约定包括getXxx、setXxx、isXxx、addXxxListener、XxxEvent等。遵守上述约定的类可以用于若干工具或库。
使用jsp:useBean创建Bean实例对象,示例:
1.我有一个Student类,代码如下:
```java
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代码如下:
```
Insert title here
这里的文字会在对象创建的时候打印出来
```
访问http://localhost:8080/jsp-tag_Test/test.jsp,运行结果:
> 这里的文字会在对象创建的时候打印出来
**5.此标签用于设置Bean对象的属性值:**
property的值为对象的属性名称,name的值为jsp:useBean中定义的id值,param的值为html表单中的name属性的值
```bash
```
**6.此标签用于得到Bean对象的属性值,默认会将值打印出来:**
同样的property的值为对象的属性名称,name的值为jsp:useBean中定义的id值
```bash
```
示例:
index.jsp代码如下:
```
Insert title here
提交
```
test.jsp代码如下:
```bash
Insert title here
这里的文字会在对象创建的时候打印出来
```
访问http://localhost:8080/jsp-tag_Test/index.jsp,输入的表单内容:
> lisi
123@12.com
15
跳转到test.jsp后的打印结果:
> 这里的文字会在对象创建的时候打印出来
lisi
123@12.com
15
如果表单中的name属性值与Bean对象的属性名称一致的话,可以使用以下方式,自动匹配相同的名称:
index.jsp表单代码如下:
```
提交
```
test.jsp代码如下:
```
Insert title here
这里的文字会在对象创建的时候打印出来
```
访问http://localhost:8080/jsp-tag_Test/index.jsp,输入的表单内容:
> zero
789@12.com
16
跳转到test.jsp后的打印结果:
> 这里的文字会在对象创建的时候打印出来
zero
789@12.com
16
关于Bean实例存储在session对象中的那拿值过程:
```
```
1.先在session中查找有没有stu
2.有的话就直接拿出来,不会创建对象
3.没有的话,就创建对象,存到session中之后再拿出来
### EL表达式
----------
EL(Expression Language) 是为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法,让Jsp的代码更加简化。EL表达式和jsp标签一样,内部实际就是java代码,以下是生成的源码:
使用EL表达式后生成的源码:
```java
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标签后生成的源码:
```java
out.write("
\r\n");
out.write("\t\r\n");
out.write("\t\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("
\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("
\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代码:
```
Insert title here
${name};
```
访问http://localhost:8080/EL-Test/test.jsp,显示空白,因为这个以上代码并没有表达式中所需要的值。
如果我在pageContext对象里设置一个name属性,看看是否能获得到,代码如下:
```
Insert title here
${name}
```
访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:
> lisi
或者在request里设置一个name属性,代码如下:
```
Insert title here
${name}
```
访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:
> request
以上实验就可以知道,只要这些对象其中有一个name属性值,就可以被这个表达式获得到。以上也提到了这种表达式寻找值是先从pageContext对象中找的,所以来实验一下是否是先从pageContext对象中找,代码如下:
```
Insert title here
${name}
```
访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:
> pageContext
除了这种只写属性名称让它自动去匹配的方式之外,还可以指定在某个对象中拿值,如下:
```txt
${pageScope.name}
${requestScope.name}
${sessionScope.name}
${applicationScope.name}
```
还可以访问某个对象的属性,例如我在页面上实例化了Student对象,设置了属性值之后放进了request对象中,然后就可以通过el表达式去获得这个Student对象的属性值,示例:
```
Insert title here
${student.sname}
${student.email}
${student.age}
```
访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:
> lisi
1564@xx.com
16
但是有一点要注意的就是,这个Student实例对象必须要有get方法才能获得到相应的属性值,否则是无法获得的。
如果Student实例对象中有一个属性存储的是对象,那么在el表达式中也可以拿到该对象的的属性值:
```
Insert title here
${requestScope.stu.student.sname}
${requestScope.stu.student.email}
${requestScope.stu.student.age}
```
访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:
> lisi
1564@xx.com
16
除了可以在以上对象中拿值外,还可在以下对象中拿值:
1.param: 表单参数对象
2.paramValues:重复的表单参数的对象
3.header: http请求头参数对象
4.headerValues:重复的http请求头参数的对象
5.cookie: cookie对象
6.initParam: 通过这个对象,可以拿到servletContxt对象初始化时配置的参数值
示例:
index.jsp代码:
```
Insert title here
提交
```
test.jsp代码:
```
Insert title here
学生名字:${param.sname}
邮箱地址:${param.email}
年龄:${param.age}
type:${paramValues.sname[2]}
host:${header.host}
Accept:${headerValues.Accept[0]}
cookie:${cookie.JSESSIONID.value}
```
访问http://localhost:8080/EL-Test/index.jsp,输入的表单内容如下:
> lisi1
lisi2
lisi3
1563@xx.com
16
跳转到test.jsp后的打印结果:
```xml
学生名字: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
name
一拳超人
```
test.jsp代码如下:
```
Insert title here
${initParam.name}
```
重启Tomcat后,访问http://localhost:8080/EL-Test/test.jsp,打印结果如下:
> 一拳超人
el表达式除了可以从各种对象中获得值之外还可以使用算术运算符、关系运算符、逻辑运算符等运算符,进行相关的运算,示例:
```
${num / 100}
${num * 100}
${num + 100}
${num - 100}
${num % 100}
${num == 1000.05}
${num > 1000.05}
${num < 1000.05}
${num >= 1000.05}
${num <= 1000.05}
${num != 1000.05}
${num == 1000.05 && true}
${num == 1000.05 || false}
${num != 1000.05 ? 8:9}
```
访问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
除了以上这种常见的运算符形式外,还可使用英文单词缩写的形式,(注意,加减乘,这三项不支持缩写的形式)如下:
```
${num div 100}
${num * 100}
${num + 100}
${num - 100}
${num mod 100}
${num eq 1000.05}
${num gt 1000.05}
${num lt 1000.05}
${num ge 1000.05}
${num le 1000.05}
${num ne 1000.05}
${num eq 1000.05 and true}
${num eq 1000.05 or false}
${num ne 1000.05 ? 8:9}
```
访问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工程配置如下依赖:
```
ognl
ognl
3.0.4
```
编写代码如下:
```java
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,也就是根元素
Hashtablehashtable=new Hashtable();
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表达式外还有这种表达式。