el表达式,c标签的使用

el表达式的使用

  • 学生类
public class Student {
	
	int id;
	String name;
	String sex;
	String birth;
	
	public Student() {}
	
	public Student(int id, String name, String sex, String birth) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.birth = birth;
	}
	
	// el 表达式取实体类的属性,需要设置get方法
	public int getId(){
		return id;
	}
	
	public String getName(){
		return name;
	}
	
	public String getSex() {
		return sex;
	}
	
	public String getBirth() {
		return birth;
	}
		
}

  • 取域对象request,session,application的值

  • 取学生对象的值

  • 取集合对象的值

  • servlet doService方法如下

	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 请求编码
		req.setCharacterEncoding("UTF-8");
		// 响应编码
		resp.setContentType("text/html;charset=UTF-8");
		
		// 设置request, session, application域对象的值
		req.setAttribute("req", "req1");
		req.getSession().setAttribute("session", "session1");
		req.getServletContext().setAttribute("app", "application1");
		
		// 设置学生对象
		req.setAttribute("stu", new Student(1, "jack", "男", "2020-2-2"));
		
		// 设置集合对象
		List<Student> list = new ArrayList<Student>();
		list.add(new Student(2, "r", "女", "2020-3-4"));
		list.add(new Student(3, "y", "女", "2020-3-4"));
		list.add(new Student(4, "e", "女", "2020-3-4"));

		req.setAttribute("list", list);
		// 请求转发到index.jsp页面
		req.getRequestDispatcher("index.jsp").forward(req, resp);
	}
  • jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- jsp/jstl/core --%>
<%
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%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <h1>req, session, application取值</h1>
    <%-- 取域对象的值 --%>
    ${ req }
    
    ${ session }
    
    ${ app }
    <hr/>
    
    <%-- 判断空 --%>
    ${ empty 1 }
    
    <c:if test="true">
    	<br/>i am true!
    </c:if>
    <br/>
    学生:
    ${ stu } <%-- 学生对象 --%>
   	${ stu.name } 
   	${ stu.sex } 
   	${ stu.birth } 

   
    <br/>
     <%-- 取list的值 --%>
    <c:forEach items="${ list }" var="stu">
    	${ stu.id }
    	${ stu.name }
    	${ stu.sex } 
     	${ stu.birth } <br/>
    </c:forEach>
    
        
     </body>
</html>

  • 超链接参数取值
<a href="index.jsp?row_id=1">c标签取get</a>
// index.jsp页面 el表达式取值
${ param.row_id }

c标签的使用,取的别名c

  • 导入jstl库,别导错了,还有一个标签库跟下面的很像。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <c:if test="${ 1==1 }">
    	1=1
    </c:if>
    
    <c:choose>
    	<c:when test="true">true</c:when>
    	<c:when test="false">false</c:when>
    	<c:otherwise>false</c:otherwise>
    </c:choose>
    
    <c:forEach begin="1" end="5" step="1" var="i">
    	${ i }
    </c:forEach>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EL表达式 (详解) EL表达式 1、EL简介 1)语法结构 ${expression} 2)[]与.运算符 EL 提供.和[]两种运算符来存取数据。 当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要使用 []。 例如: ${user.My-Name}应当改为${user["My-Name"] } 如果要动态取值时,就可以用[]来做,而.无法做到动态取值。例如: ${sessionScope.user[data]}中data 是一个变量 3)变量 EL存取变量数据的方法很简单,例如:${username}。它的意思是取出某一范围中名称为 username的变量。 因为我们并没有指定哪一个范围的username,所以它会依序从Page、Request、Session、 Application范围查找。 假如途中找到username,就直接回传,不再继续找下去,但是假如全部的范围都没有找到时, 就回传null。 属性范围在EL中的名称 Page PageScope Request RequestScope Session SessionScope Application ApplicationScope 4) 1--EL表达式用${}表示,可用在所有的HTML和JSP标签中作用是代替JSP页面中复杂的JAVA代码. 2--EL表达式可操作常量 变量 和隐式对象. 最常用的 隐式对象有${param}和${paramValues}. ${param}表示返回请求参数中单个字符串的值. ${paramValues}表示返回请求参数的一组 值.pageScope表示页面范围的变量.requestScope表示请求对象的变量. sessionScope表示会话 范围内的变量.applicationScope表示应用范围的变量. 3 -- 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁 止.JSP2.0中默认的启用EL语言. 4-- EL语言可显示 逻辑表达式如${true and false}结果是false 关系表达式如${5>6} 结 果是false 算术表达式如 ${5+5} 结果是10 5--EL中的变量搜

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值