jsp和el表达式,以及JSTL标签库

1,jsp
1,概念
2,jsp的三种方式
out.write(65);字符 字符串 字符数组
1) <%
中间写java代码
out.println("任何类型");
out.write(65);
response.getWriter().println();
response.getWriter().write();
%>
2)定义成员变量
<%!
int num = 10
public void test(){}
%>
3)表达式定义方式,输出字符串:直接在页面输出内容,在service方法中起作用,等价于response.getWriter().println()
<%="你好世界"%>
 
response 不带缓存,输出时候首先输出
out 带缓存的语句,输出时候先在缓存区,刷新后才可以输出,不要两种混用,会出现bug,优先使用out.println
<%
out.println()
response.getWriter().write();
%>
 
4)需要换行的话
在jsp代码中拼接<br>
response.getWriter().println("println" + "<br>");
 
2、EL表达式
1, EL全称: Expression Language
 
作用:代替jsp中脚本表达式的功能,简化对java代码的操作,从【域对象】中取值。 EL表达式简化<%= %>方式取值
EL语法表达式的格式:${表达式内容}
 
2,jsp的四大域对象
page域,request域,session域,application域
page域 pageScope page域指的是当前jsp页面,其中存储的数据只在当页面有效
request域 requestScope 一次请求或者请求链中request域
session域 sessionScope 一次会话过程中,session域
application applicationScope 服务器启动后整个项目对应的ServletContext域
 
 
3,域对象保存值,与取出值的代码
 
<%--
分别往request域,session域,servletContext域和pageContext中存值
--%>
<%
request.setAttribute("requestName","requestScope中的值");
request.getSession().setAttribute("sessionName","sessionScope中的值");
request.getServletContext().setAttribute("servletContextName", "servletContext中的值");
pageContext.setAttribute("pageName","page域中的值");
%>
<%--
使用EL表达式从上述3个域中取值
--%>
从request域中取值:${requestScope.requestName}<br>
从session域中取值:${sessionScope.sessionName}<br>
从servletContext域中取值:${applicationScope.servletContextName}<br>
从page域中取值:${pageScope.pageName}
 
EL最主要的作用是获得四大域中的数据,格式${EL表达式}
EL获得pageContext域中的值:${pageScope.key};
EL获得request域中的值:${requestScope.key};
EL获得session域中的值:${sessionScope.key};
EL获得application域中的值:${applicationScope.key};
 
 
4,域对象访问顺序
从小到大的顺序
page request session application
---同样是依次从pageContext域,request域,session域,application域中 获取属性,在某个域中获取后将不在向后寻找
5,通过EL表达式,获取Cookie的name和值
${cookie.JSESSIONID} <%--根据cookie的名字获取cookie对象 --%>
${cookie.JSESSIONID.name} <%--获得cookie的名称,对应方法getName()--%>
${cookie.JSESSIONID.value} <%--获得cookie的值,对应方法getValue() --%>
 
从cookie中获取cookie对象:${cookie.JSESSIONID}<br>
从cookie中获取name值:${cookie.JSESSIONID.name}<br>
从cookie中获取value值:${cookie.JSESSIONID.value}<br>
 
6,EL的运算符
1)算数运算符
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
pageContext.setAttribute("n1", "10");
pageContext.setAttribute("n2", "20");
%>
 
加法运算:${n1+n2}<br>
减法运算:${n1-n2}<br>
乘法运算:${n1*n2}<br>
除法运算:${n1/n2}或${n1 div n2}<br>
取余运算:${n1%n2}或${n1 mod n2}<br>
</body>
</html>
 
2)关系运算符
 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
pageContext.setAttribute("n1", "10");
pageContext.setAttribute("n2", "20");
%>
 
==:${n1 == n2}或 ${n1 eq n2}<br>
!=:${n1 != n2}或 ${n1 ne n2}<br>
>:${n1 > n2}或 ${n1 gt n2}<br>
>=:${n1 >= n2}或 ${n1 ge n2}<br>
<:${n1 < n2}或 ${n1 lt n2}<br>
<= :${n1 <= n2}或 ${n1 le n2}<br>
</body>
</html>
3)逻辑运算符
${true && false}
${true || false}
${!false}
 
4)三元运算符
${n1 >0 ?"正数":"负数"}
 
5)empty运算符
empyt运算符对以下数据运算返回true:
 
1,字符串:"";
2,空集合(size=0):List list = new ArrayList();
3,空对象(null):Student stu = null;
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.heima.domain.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String str = "";
List list = new ArrayList();
Student student = null;
 
request.setAttribute("str", str);
request.setAttribute("list", list);
request.setAttribute("student",student);
%>
 
空字符串:${empty str}<br>
集合size=0:${empty list}<br>
空对象null:${empty student}<br>
</body>
</html>
 
 
3,JSTL标签库
1,介绍
用于解决常用问题的自定义标签库,这套标签库被SUN公司定义为标准标签库(The JavaServer Pages Standard Tag Library),简称JSTL。
 
2,使用步骤
1)安装导包
javax.servlet.jsp.jstl.jar
jstl-impl.jar
2)将标签库资源引入JSP页面
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3)在jsp页面中使用jstl标签
<c:out value="hello world"></c:out>
 
3,常见标签
 
1)c:if标签
作用相当于java中的if判断语句
属性:test: if的判断条件
<body>
<h1>首页 -- JSP页面</h1>
 
<%--
判断session中的loginUser是否为空:
1.如果不为空,获取session中的信息,并将用户名显示在页面上;
2.如果为空,则提示用户登录;
--%>
 
<c:if test="${not empty loginUser}">
<h3>欢迎您!${loginUser.userName}</h3>
</c:if>
<c:if test="${empty loginUser}">
<h3>对不起,请先<a href="login.html">登录</a></h3>
</c:if>
</body>
 
2)c:foreach标签的使用
 
<c:foreach>标签的作用相当于java中的for循环。主要是对数据和集合进行遍历。
属性:
var:在不循环对象的时候,保存的是控制循环的变量;在循环对象的时候,保存的是被循环对象中的元素
items:指定要循环的对象
varStatus:保存了当前循环过程中的信息(循环的开始、结束、步长、次数等)
begin:设置循环的开始
end:设置循环的结束
step:设置步长——间隔几次循环,执行一次循环体中的内容
 
使用案例一:数组遍历
<%--
需求一:遍历普通数组
--%>
<%
String[] strArr = new String[5];
strArr[0] = "hello";
strArr[1] = "world";
strArr[2] = "你好";
strArr[3] = "世界";
strArr[4] = "世界你好";
request.setAttribute("strArr",strArr);
%>
 
<c:forEach items="${strArr}" var="str">
${str} <br>
</c:forEach>
使用案例二:遍历Student的list
 
<%--
需求二:遍历Student的list集合
--%>
 
<%
List<Student> stuList = new ArrayList<>();
Student stu1 = new Student(1, "张三1", "123");
Student stu2 = new Student(2, "张三2", "123");
Student stu3 = new Student(3, "张三3", "123");
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
request.setAttribute("stus",stuList);
%>
 
<c:forEach items="${stus}" var="stu">
${stu.name} <br>
</c:forEach>
 
案例三,遍历map集合
 
<%
Map<String, String> map = new HashMap<>();
map.put("张三", "123");
map.put("李四", "123");
map.put("王五", "123");
request.setAttribute("maps",map);
%>
 
<c:forEach items="${maps}" var="stuMap">
${stuMap.key} = ${stuMap.value}<br>
</c:forEach>
 
3)choose标签的使用
<c:choose >用于指定多个条件选择的组合边界,它必须与c:when和c:otherwise标签一起使用。
 
介绍 c:when,相当于else if(){}。
 
c:when标签含有test属性,作用与if相同 c:otherwise,相当于else{}。
 
案例代码
 
<%
request.setAttribute("num", 4);
request.setAttribute("flag", 1);
%>
<!‐‐ c:choose 表示那些when 和 otherwise是一组的 ‐‐>
<c:choose>
<c:when test="${num == 1 }">星期一</c:when>
<c:when test="${num == 2 }">星期二</c:when>
<c:when test="${num == 3 }">星期三</c:when>
<c:when test="${num == 4 }">星期四</c:when>
<c:when test="${num == 5 }">星期五</c:when>
<c:when test="${num == 6 }">星期六</c:when>
<c:when test="${num == 7 }">星期日</c:when>
<c:otherwise>参数不合法</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${flag == 1 }">白天</c:when>
<c:when test="${flag == 2 }">黑夜</c:when>
<c:otherwise>参数不合法</c:otherwise>
</c:choose>
标签结合的实际应用案例
删除案例:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserService userService = new UserServiceImpl();
List<User> userList = userService.queryAll();
 
request.setAttribute("userList",userList);
 
request.getRequestDispatcher("/list.jsp").forward(request,response);
 
}
 
<script>
function deleteUser(id){
var flag = window.confirm("确定删除");
if(flag){
location.href = "deleteUserServlet?id=" + id;
}
}
</script>
<script>
function queryUserUp(id){
location.href = "queryUserById?id=" + id;
}
</script>
 
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.sex}</td>
<td>${user.age}</td>
<td>${user.address}</td>
<td>${user.qq}</td>
<td>${user.email}</td>
<td><a class="btn btn-default btn-sm" href="javascript:void(0);" onclick="queryUserUp(${user.id})">修改</a>&nbsp;
<a class="btn btn-default btn-sm" href="javascript:void(0);" onclick="deleteUser(${user.id})">删除</a></td>
</tr>
</c:forEach>
注册案例:
 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Map<String, String[]> properties = request.getParameterMap();
User user = new User();
try {
BeanUtils.populate(user,properties);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
 
UserService service = new UserServiceImpl();
boolean result = service.queryUserByName(user);
 
if(result){
response.sendRedirect("/tiaozhuan.html");
}else{
request.setAttribute("msg","重名了,注册失败");
request.getRequestDispatcher("add.jsp").forward(request,response);
}
}
 
注册失败后,在网页上显示错误信息,为空则不显示
<div><%=request.getAttribute("msg")==null?"":request.getAttribute("msg")%></div>
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/cyxy31521/p/9417010.html
今日推荐