Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cset doubt??

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Can anyone explain me the use of cset with some examples and comparing it with jsp:setProperty?? Any help will be appreciated.

Thanks in advance!
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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}"/>
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus,

That you ver much for the detailed explanation. I'm getting to understand it. Thankgs again.
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can see it in the no mangled smiley tags if you go direct to

http://www.examulator.com/moodle/mod/resource/view.php?id=441
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus,

Thanks once again for the link. Actually I extensively use your tutorials for SCWCD and they are really helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic