將value的值儲存至范圍為scope的varName變量之中:
<c:set value=value var=varName [scope={ page|request|session|application }]/> 將本體內容的數據儲存至范圍為scope的varName變量之中:
<c:set var=varName [scope={ page|request|session|application }]>
… 本體內容
</c:set> 將value的值儲存至target對象的屬性中:
c:set value=value target=target property=propertyName /> 將本體內容的數據儲存至target對象的屬性中:
<c:set target=target property=propertyName>
… 本體內容
</c:set>
它有如下屬性屬性描述是否必須缺省值: 名 稱 說 明 EL 類型 必須 默認值 value 要被儲存的值 Y Object 否 無 var 欲存入的變量名稱 N String 否 無 scope var變量的JSP范圍 N String 否 pagescope target 為一JavaBean或java.util.Map對象 Y Object 否 無 property 指定target對象的屬性 Y String 否 無 如果指定了target屬性, 那么property屬性也必須指定。
注意:如果你在一個JSP頁面中設置了<c:set var=reqURL value=XXXX></c:set>並且想在一個<jsp:include page=pager.jsp flush=true/>的頁面中使用此參數,那么,必須要制定這個reqURL的有效范圍,即<c:set var=reqURL scope=request value=${ctxt}/Position_Mypub?op='${requestScope.op}'></c:set>
例:
<c:set value=${test.testinfo} var=test2 scope=session />
將test.testinfo的值保存到session的test2中,其中test是一個javabean的實例,testinfo是test對象的屬性。
<c:set target=${cust.address} property=city value=${city}/>
將對象cust.address的city屬性值設置為變量city的值。
教材例程15-3,c_set.jsp,<c:set>標簽的應用。
<%@ taglib prefix=c uri=http://java.sun.com/jstl/core %>
<%@ page contentType=text/html; charset=gb2312 language=java %>
<jsp:useBean id=user class="com".jspdev.ch3.TestBean/>
<html>
<head>
<title>JSTL:的使用c:set</title>
</head>
<body bgcolor=#FFFFFF>
<hr>
設置userName的屬性為hellking,然后輸出這個屬性值:
<c:set value=hellking var=userName/>
<c:out value=${userName}/>
<hr>設置password的屬性,屬性值在body中,然后輸出這個屬性值:
<c:set var=password>
xcsdkjf234dfsgs234234234
</c:set>
<c:out value=${password}/>
<hr>設置javaBean的屬性,然后輸出這些屬性值:
<c:set value=hk2 target=${user} property=userName/>
<c:set target=${user} property=password>
sdf234sdfd
</c:set>
userName=<c:out value=${user.userName}/>,
password=<c:out value=${user.password}/>.
<hr>設置不同的屬性,並且指定它們的范圍:
<c:set value=10000 var=maxUser scope=application/>
<c:set value=20 var=maxIdelTime scope=session/>
<c:set value=next.jsp var=nextPage scope=page/>
</body>
</html>