<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. |
示例:
- 为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}" />
- 为特定范围内的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); %>
- 为特定范围内的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" />