JSTL 核心标签库 (05) : 表达式控制标签

<c:out>标签

Like <%= ... >, but for expressions.

参数列表:

Name Required Request-time Type Description
value true true java.lang.String Expression to be evaluated.
default false true java.lang.String Default value if the resulting value is null.
escapeXml false true java.lang.String Determines whether characters <,>,&,'," in the resulting string should be converted to their corresponding character entity codes. Default value is true.

示例:

<c:set>标签

将表达式求值的结果设置到一个特定作用域的变量中.

参数列表:

Name Required Request-time Type Description
var false false java.lang.String Name of the exported scoped variable to hold the value specified in the action. The type of the scoped variable is whatever type the value expression evaluates to.
value false true java.lang.String Expression to be evaluated.
target false true java.lang.String Target object whose property will be set. Must evaluate to a JavaBeans object with setter property property, or to a java.util.Map object.
property false true java.lang.String Name of the property to be set in the target object.
scope false false java.lang.String Scope for var.

示例:


  1. 为String类型的命名变量设定值
    <!-- 以下代码在会话范围内设置了一个user命名变量,它的值为"Tom",
    表达式${sessionScope.user}的打印结果为"Tom" -->
    <c:set var="user" value="Tom" scope="session" />
    也可以用标签主体来设定值:
    <c:set var="user" scope="session">
    Tom
    </c:set>
    
    ${sessionScope.user}
    
    等价于:
    <% pageContext.setAttribute("user","Tom",PageContext.SESSION_SCOPE); %> 
    
    <!-- 以下代码在页面范围内设置了一个user属性,它的值为username请求参数的值 -->
    <c:set var="user" value="${param.username}" />
  2. 为特定范围内的JavaBean对象的属性设定属性值
    <!-- 先通过<jsp:useBean>标签定义了一个Web应用范围内的counterBean命名变量,
    接下来通过<c:set>标签把这个counterBean命名变量的count属性设为2,${counterBean.count}的打印结果为2 -->
    
    <%@ page import="mypack.CounterBean" %>
    <jsp:useBean id="counterBean" scope="application" class="mypack.CounterBean" />
    <c:set target="${counterBean}" property="count" value="2" />
    ${counterBean.count}
    
    等价于:
    <%
    CounterBean counterBean=(CounterBean)pageContext.findAttribute("counterBean");
    counterBean.setCount(2);
    %>
  3. 为特定范围内的Map对象的Key设定值
    <!-- 先通过<jsp:useBean>标签定义了一个请求范围内的HashMap类型的weeks命名变量,
    再通过两个<c:set>标签向这个weeks命名变量中加入两个元素 -->
    
    <%@ page import="java.util.HashMap" %> 
    <jsp:useBean id="weeks" scope="request" class="java.util.HashMap" />
    <c:set target="${weeks}" property="1" value="Monday" />
    <c:set target="${weeks}" property="2" value="Tuesday" />
    
    以上代码中的第一个<c:set>标签与以下Java程序片段的作用是等价的:  
    <%  
    Map weeks=(Map)pageContext.findAttribute("weeks");
    weeks.put("1","Monday");
    %>

<c:remove>标签

删除所有作用域中的命名变量. 默认为所有作用域.

参数列表:

示例:


<c:remove var="命名变量的名字"  scope="{page|request|session|application}" />

<c:catch>标签

捕获标签体中所有的Throwable对象.

参数列表:

示例:


<!-- 捕获标签体中所有异常并命名为ex -->
<c:catch var="ex"> 
<%  
int a=11;  
int b=0;  
int c=a/b; //抛出异常  
%> 
</c:catch>
<c:out value="${ex.message}" default="No exception" />




转载于:https://my.oschina.net/CasparLi/blog/472832

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值