Java Web学习笔记之---EL和JSTL
(一)EL
(1)EL作用
Expression Language(表达式语言),目的是代替JSP页面中复杂的代码
(2)EL表达式
${变量名}
(3)EL实现
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="ELServlet" method="post"> 用户名:<input type="text" name="user"> 密码:<input type="text" name="password"> <input type="submit"> </form> </body> </html>
ELServlet.java
package servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/ELServlet") public class ELServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取输入值 String user=req.getParameter("user"); String password=req.getParameter("password"); //存入request req.setAttribute("user",user); req.setAttribute("password",password); //转发至1.jsp req.getRequestDispatcher("1.jsp").forward(req,resp); } }
1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> 用户名:${user} 密码:${password} </body> </html>
(二)JSTL
(1)JSTL介绍
JSP标准标签库
(2)与EL表达式的关系
JSTL通常与EL表达式一起实现JSP页面编码
(3)JSTL使用准备
在JSP页面添加
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
在pom.xml中配置
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
(4)JSTL常用标签
1.通用标签:set,out,remove
2.条件标签:if,choose
3.迭代标签:forEach
(5)常用标签的使用
JstlServlet.java
package servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @WebServlet("/JstlServlet") public class JstlServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //创建Map集合,并存入数据 Map<String,Object> m1=new HashMap<String,Object>(); m1.put("user","AA"); m1.put("password","123"); m1.put("age","11"); Map<String,Object> m2=new HashMap<String,Object>(); m2.put("user","BB"); m2.put("password","123"); m2.put("age","12"); //将Map集合存入List集合 List<Map<String,Object>> list=new ArrayList<Map<String,Object>>(); list.add(m1); list.add(m2); //将List集合存入request中 req.setAttribute("list",list); //转发 req.getRequestDispatcher("2.jsp").forward(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } }
2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <!-- 1.set标签:将值保存到指定范围内 2.将value值存到范围为scope的变量var中 --> <c:set var="user" value="AA" scope="request"></c:set> <c:set var="age" value="12" scope="request"></c:set> <!-- out标签:将结果输出显示 --> <c:out value="${user}"></c:out> <!-- remove标签:删除指定域内数据 --> <c:remove var="user"></c:remove> <!-- if标签: test:放判断条件,如果条件为true,则输出标签体中的内容--> <c:if test="${age==12}"> 12岁 </c:if> <!-- choose标签不能单独存在,内含when标签和otherwise标签,类似于if...else--> <c:choose> <c:when test="${age==12}"> 是12岁 </c:when> <c:otherwise> 不是12岁 </c:otherwise> </c:choose> <table> <!-- forEach标签:类似于for(数据类型 变量名:集合或数组) items中放集合或数组 var中放变量名 用变量名调用 --> <c:forEach items="${list}" var="Map"> <tr> <td>${Map.user}</td> <td>${Map.password}</td> <td>${Map.age}</td> </tr> </c:forEach> </table> </body> </html>