文章目录
一、EL :Expression Language
可以替代JSP页面中的Java代码
servlet (增加数据) --> Jsp(显示数据)
传统的在JSP中用Java代码显示数据的弊端:类型转换,需要处理NULL,代码掺杂
1.EL示例
${requestScop.student.address.shoolAddress}
${域对象。与对象中的属性.属性.属性.级联属性}
2.EL操作符
-
点操作符 “ . ” :使用方便
-
中括号操作符 “ [ " " ] ” :
- []中必须有**“” 或者‘’**
- 功能强大
- 可以包含特殊字符(. 、-)
- 可以访问数据
- 可获取变量
-
获取map属性
Map< String,Object > map = new HashMap<>();
map.put(“cn”,"");
map.put(“us”,"");
request.setAttribute(“map”,map);
<************************************************************>
${requestScope.map.cn}
-
关系运算符 逻辑运算符
可以用英文表示,也可以用符号表示
-
Empty运算符
判断一个值是否为空:如果该值为空,或不存在--------> ture
${empty requestScope['name']}
3.EL内置对象
1.作用域访问对象(EL域对象)
如果不指定域对象,则默认会根据从小到大的顺序一依次取值(以下排列为)
- pageScope
- requestScope
- sessionScope
- applicationScope
servlet中
request.getSession().setAttribute("sessionKey","sessionValue");
jsp中
${sessionScope.sessionKey}
2.参数访问对象
获取表单数据(超链接中的值)
request.getParameter() request.getParameterValues()
${param} ${paramVlaues}
3.Jsp隐式对象(pageContext)获取JSP内置对象
在jsp中可以通过pageContext 获取其他的jsp隐式对象,因此如果在EL中使用jsp隐式对象,就可以通过pageContext间接获取
eg: p a g e C o n t e x t . g e t S e s s i o n ( ) − − − > {pageContext.getSession()}---> pageContext.getSession()−−−>{pageContext.session}
p a g e . C o n t e x t . g e t R e s p o n s e − − − > {page.Context.getResponse}---> page.Context.getResponse−−−>{pageContext.response}
可以级联获取
二、JSTL
JSTL比EL更强大
1.引入tablib:
<%@ taglib uri = “http:// java.sun.com/jsp/jstl/core” prefix = “c” %>(c为随机一个字母)
其中prefix = “c” 为前缀
2.核心标签库
-
通用标签库
-
< c:set >赋值
-
在某个作用域中(四个范围对象),给某个变量赋值3
<c : set var = "name" value = "zhangsan" scop = "request" /> 等价于 request.setATTribute("name","zs");
-
在某个作用域中,给某个对象的属性赋值(该写法不能指定scope属性)
注
可以给不存在的变量赋值(但不能给不存在的对象赋值)
给普通对象赋值 ${requstScope.student.sname}//显示 <c:set target = "${requstScope.student.sname}" property = "sanme" value = "zxs" /> ${requstScope.student.sname}//显示 ************************************************* 给Map对象赋值 ${requstScope.countries.cn}//显示 <c:set target = "${requstScope.countries.cn}" property = "cn" value = "中国" /> ${requstScope.countries.cn}//显示
-
-
< c:out >输出
传统EL:${requstScope.student}//显示 c:out方式:<C:out value = "${requestScope.studnet}" /> <C:out value = "${requestScope.studnet}" defaul = "zs" /> 如果${requestScope.studnet} 不存在,则显示defaul的默认值 超链接 显示超链接的代码(<a herf = "welcome.jsp">登录</a>) <C:out value = '<a herf = "welcome.jsp">登录</a>' escapeXml = "true" /> 显示超链接地址(登录) <C:out value = '<a herf = "welcome.jsp">登录</a>' escapeXml = "false" />
-
< c:remove >删除属性
```jsp <c:remove var = "a" scope = "request" /> ```
-
-
条件标签库
< c:if > , < c:choose >判断
<c:if test = "${10>2}" var = "result" scope = "request"> </c:if> 相当于switch <c:choose> <c:when test = "${reuquestScope.role == "老师"}"> 老师代码。。。 </c:when> <c:when test = "${reuquestScope.role == "老师"}"> 学生代码。。。 </c:when> <c:when test = "..."> </c:when> ............................... <c:otherwise> </c:otherwise> </c:choose>
-
迭代标签库(循环)
<c:forEach begin = "" end = "" steo = "">