File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes cset doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "cset doubt??" Watch "cset doubt??" New topic
Author

cset doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

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!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
<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}"/>


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Marcus,

That you ver much for the detailed explanation. I'm getting to understand it. Thankgs again.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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

Joined: Sep 26, 2006
Posts: 9622
    
    2

Marcus,

Thanks once again for the link. Actually I extensively use your tutorials for SCWCD and they are really helpful.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: cset doubt??