<c:set> and <c:remove>
The set and remove tags allow the setting/creation of attributes and their removable. This is particularly useful when using the EL as although it allows variables to be manipulated it does not directly support setting or deleting.
The following code shows a trivial example of using these tags.
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<html>
<head>
<title>The set and remove JSTL tags</title>
</head>
<body>
<c:set var="result" value="10"/>
<c:out value="${result}"/>
<c:remove var="result"/>
Now the variable has been removed
<c:out value="${result}"/>
</body>
</html>
The <c:set used with var is for attributes and used with target is for beans properties or map values
Set and remove with Maps and Beans
The <c: set tag can be used with Beans and maps using the target attribute instead of the var attribute. The next JSP page uses the following simple plain
Java code bean.
/**
**@author Marcus Green 2006
* For use with JSTL <c:set tag
**/
package com.examulator;
public class DemoBean{
private
String MyField;
public void setMyField(String s){
this.MyField = s;
}
public String getMyField(){
return this.MyField;
}
}
The JSP page is as follows
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<jsp:useBean id="address" class="java.util.HashMap" />
<jsp:useBean id="demo" class="com.examulator.DemoBean" />
<html>
<head>
<title set and remove JSTL tags with beans and maps</title>
</head>
<body>
<c:set target="${demo}" property="myField" value="guru" />
<c:set target="${address}" property="line1" value="10 Downing St" />
<c:set target="${address}" property="postcode">
SW1A2AJ
</c:set>
<c:out value="${address.line1}"/>
<c:out value="${address.postcode}"/>
<c:out value="${demo.myField}"/>
</body>
</html>
Note how the set tag is used in two versions, one where the content is assigned using the value attribute and the other (for the postcode) where it is assigned within the body of the tag. When this page is run it will output
10 Downing Street SW1A2AJ guru
The <c:remove tag does not work with Beans and Maps, there is no var attribute and you cannot use the target attribute instead. Thus the following will NOT work.
<c:remove target="${address}"/>