EL表达式简单例子

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>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值