This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

c:set tag not clear

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam not able to understand the exact use of <c:set> tag. Can anyone please explain me with a PROPER EXAMPLE........a small example will do.....
that too the one which uses "target" property

Also <jsp:setProperty> when put in<jsp:useBean> taq, can be made optional. How about <c:set>?

[ October 25, 2008: Message edited by: Maan Shenoy ]
[ October 25, 2008: Message edited by: Maan Shenoy ]
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shenoy,
Its very simple. The only way in JSP to create attributes inside a given scope without using scriptlets is through <c:set/> JSTL.

There are two variations of c:set
a) This one is used to create
<c:set var="testVar" value="value|${EL}" scope="request|page|session|application" />

With this version there will be a attribute with name testVar and its value will be either string "value" or it can even be an Object via EL.

Will explain the other version. later. May be someone else can explain it. Its very simple.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks deepak,I knw its not that tough, but if you or someone could kindly explain me with an example, then it would have been great. Specially , as i mentioned earlier, <c:set> target thing is what i want to know ( How to use it in a MAP). Please explain through a small example, if possible...

Also how is c:set target different from var?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The target form of <c:set> is used to set properties of a bean, or entries of a Map (as opposed to scoped variables using the var syntax).

For example:
[ October 25, 2008: Message edited by: Bear Bibeault ]
 
Lakshmi Narayanan
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try to explain c:set target
<%
java.util.HashMap myMap = new java.util.HashMap();
myMap.put("1","Java");
myMap.put("2","Ranch");
request.setAttribute("DEAR",myMap);
%>

For c:set target -
1. object should be available already in any of the four scopes. otherwise throws exception
2. supports only for Map and Bean objects
3. target can have EL expressions or scriptless
4. If target attribute is null it throws exception
5. Removal: if the value is set null for the property then it works this way
5.1 if target is map then key is removed from the map object
5.2 if target is bean then property value is set to null.
6. Add: If the target is map object then if
6.1 property is new then it is added to the map
6.2 if the key already exist then it overwrites.
I hope I am clear if not someone explain.
Here is the example
<c:catch var="myException">
<%
java.util.HashMap myMap = new java.util.HashMap();
myMap.put("1","Java");
myMap.put("2","Ranch");
request.setAttribute("DEAR",myMap);
%>

<c:set target="${DEAR}" property="3" value="Group" />

<%

myMap = (java.util.HashMap)request.getAttribute("DEAR");
for( int index=1; index < 4; index++ ){
out.println(myMap.get(String.valueOf(index)));
}
%>

</c:catch>
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<c:catch var="myException">
<%
java.util.HashMap myMap = new java.util.HashMap();
myMap.put("1","Java");
myMap.put("2","Ranch");
request.setAttribute("DEAR",myMap);
%>

<c:set target="${DEAR}" property="3" value="Group" />

<%

myMap = (java.util.HashMap)request.getAttribute("DEAR");
for( int index=1; index < 4; index++ ){
out.println(myMap.get(String.valueOf(index)));
}
%>

</c:catch>


I have put the above code in a JSP File.

The above gives me output

" JAVA RANCH NULL" instead of "JAVA RANCH GROUP" .

What is the problem there?
 
Lakshmi Narayanan
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to add taglib directive in your jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic