1、form.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTt1.jspML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> </head> <body> <%out.print("servlet的环境:"+config.getServletContext()); %> <form action="jsp/el1.jsp" method="post"> <input type="text" name="name"/><br> <input type="password" name="password"/><br> <input type="submit" /> </form> </body> </html>
2、el1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> </head> <body> <!--EL表达式的基本用法 --> <p>使用java代码获取用户名:<%=request.getParameter("name") %></p> <!--使用EL表达式的两种方法输出参数是name和password的值 --> <!--从其他页面传递的参数使用这种格式获取数据 --> <p>使用EL表达式获取用户名:${param.name}</p> <p>使用EL表达式获取用户名:${param["name"]}</p> <p>使用EL表达式获取用户密码:${param.password}</p> <p>使用EL表达式获取用户密码:${param["password"]}</p> <!-- 输出结果为:${param.name}--> ${"${param.name}" } <!-- 输出结果:qqqqq --> ${"qqqqqq" } <!--EL表达式的存取范围:pageScope,requestScope,sessionScope,applicationScope --> <!--1.存储数据 --> <%session.setAttribute("session", "EL表达式!!!!"); %> <!--2.获取session中的值 --> 通过java代码获取sessionScope的属性值:<%=session.getAttribute("session") %><br> 通过EL表达式获取sessionScope的属性值:${sessionScope.session } <!--注:jsp的内置对象也是EL表达式的对象。可以使用 --> </body> </html>
3、el表达式循环map和list
(1)循环map
<forEachitems="${map}" var="item"> <c:outvalue="${item.key}"/> <c:outvalue="${item.value}"/> </forEach>
(2)循环List<Student>
list为:list<Student> <c:forEach items="${list}" varStatus="i" var="stu" > <c:if test="${i.index == 0}" > ${stu.name} </c:if> <c:if test="${i.index == 1}"> 123 </c:if> </c:forEach>
(3)循环List<Map<String,Object>>
//List<Map<String,Object>> List<Map<String,Object>> stuList = new ArrayList<Map<String,Object>>(); Map<String,Object> m1 = new HashMap<String,Object>(); m1.put("name","jim"); m1.put("age","15"); stuList.add(m1); Map<String,Object> m2 = new HashMap<String,Object>(); m2.put("name","lucy"); m2.put("age","12"); stuList.add(m2); //循环List<Map<String,Object>> <c:forEach var="student" items="${stuList}" > <c:out value="${student.name}" default="wang"/> <c:out value="${student.age}" default="25"/> </c:forEach>
(4)自定义控制循环次数(begin =0; end= list -1; 则i表示0到list-1)
<c:forEach begin="0" end="$(fn:length(list) - 1)" var="i"> <c:out value="${list[i]}" /> </c:forEach>
把end配成你要的循环次数就好。在这之前得先判断list的长度,要确保end 小于list的长度,否则会出错。
$(fn:length(list) 获取数组的大小,但必须引入:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
4、判断和设置值
<c:set var="year" value="${userInfo.birthyear }"></c:set> //i取值为(0-100包含100) <c:forEach var="i" begin="0" end="100" step="1"> <c:if test="${year == (2014 - i)}"> <option value="${2014 - i }" selected="true">${2014 - i }</option> </c:if> <c:if test="${year != (2014 - i)}"> <option value="${2014 - i }">${2014 - i }</option> </c:if> </c:forEach> <c:if test="${userInfo.sex != '1' }"> <c:if test="${not empty userInfo && userInfo.username != null && userInfo.username != ''}"> //月份 <c:set var="month" value="${userInfo.birthmonth }"></c:set> <c:forEach var="i" begin="1" end="12" step="1"> <c:if test="${i == month }"> <option value="${i}" selected="true">${i}</option> </c:if> <c:if test="${i != month }"> <option value="${i }">${i }</option> </c:if> </c:forEach>