JSP核心语法:JSP表达式<%=%> JSP脚本<%%>
JSP开发的原则:尽量在JSP页面少写甚至不写java代码,Java代码放在Servlet。
EL/JSTL目的就是去掉JSP中的java代码,都是以标签的形式表示,以标签的形式美工或者前端都可以修改,利于团队的合作。
1.得到当前项目的路径
${pageContext.request.contextPath} 替换 <%=request.getContextPath()%>
2.获取对象的值
${student.id}
替换
<% // jsp脚本,可以任意写java代码 // request内置对象 Student student = (Student) request.getAttribute("student"); student.getAge(); %>
获取list的种的对象的值
${applicationScope.stuList}<br/>
${stuList[0].name}<br/>
替换
<%
List<Student> stuList = (List<Student>) application.getAttribute("list");
%>
<%=stuList.get(0).getName()%><br/>
JSTL使用时需要导包 JSTLjar包下载
使用前还需要引入<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
遍历list的student并将student信息填入表格
<c:forEach items="${pageInfo.list}" var="student"> <tr> <td>${student.id} </td> <td>${student.name} </td> <td>${student.age} </td> <td>${student.gender} </td> <td><a href="${pageContext.request.contextPath}/student?method=getPageById&id=${student.id}">编辑</a></td> <td><a href="javaScript:deleteById(${student.id})">删除</a></td> </tr> </c:forEach>替换
<% for (Student student : list) { %> <tr> <td><%=student.getId()%></td> <td><%=student.getName()%></td> <td><%=student.getAge()%></td> <td><%=student.getGender()%></td> <td><a href="javascript:deleteById(<%=student.getId()%>)">删除</a></td> <td><a href="<%=request.getContextPath()%>/student?method=getStudentUpdatePage&id=<%=student.getId()%>">编辑</a></td> </tr> <% } %>
C:\Users\Gao\AppData\Local\JetBrains\IntelliJIdea2020.3\tomcat\abe3082c-8cdd-4302-baa3-ef6678ea03d7\work\Catalina\localhost\Web\org\apache\jsp
idea编译后的jsp文件在上面的目录下
打开后可以发现jsp其实是java代码,根据继承关系,其本质是HttpServlet
它下面的html标签其实是一行一行写出来的