It's not a secret anymore!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes problem on c:set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "problem on c:set" Watch "problem on c:set" New topic
Author

problem on c:set

Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
My code below,

Bean class



jsp page:


output:

new

Afterwards replacing jsp:setProperty line to c:set below,



output:

javax.servlet.ServletException: Invalid property in <set>: "p"

Why?






[ January 10, 2007: Message edited by: Bob CHOI ]

Hard work rewards
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
using EL to evaluate the bean name solved the problem

<c:set target="${myBean}" property="p" value="new"/>
Biba Basnetti
Greenhorn

Joined: Oct 09, 2006
Posts: 25
"with the target attribute you do not type in the String literal that represents the name under which the attribute was bound to the page, scope, etc. The "target" attribute must have a value that resolves the real thing."

-Head First Bible, Chapter 9 verse 446

So it must be an EL expression, scripting expression, jsp:attribute
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Thanks Biba!

What's "jsp:attribute", a standard action? i couldn't find it even in jsp spec. :roll:
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
sorry, i do find it after navigating the Spec "slowly", wow! "Automatic" searching "jsp:attribute" in adobe reader leads to nothing.
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
consider the cas where you hava custom tag called "myAdvice" and it has to mandatory attibutes called "taste" and "colour". Suppose that it has declared <body-content>empty</body-content>. now consider the following code -

<myTag:myAdvice>
<jsp:attribute name="taste">sweet</jsp:attribute>
<jsp:attribute name="colour">amber</jsp:attribute>
</myTag:myAdvice>

its clear from the above example that, jsp:sttribute is a way to set the attributes of your tag, as a BODY, even thoug your tag cant have a body


got it ??


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
refactor c:set below,



Got that exception again...
Stary Kapec
Ranch Hand

Joined: Dec 04, 2005
Posts: 81
I am also very confused.



It compiles and works.



It compiles and works. But



It does not work! Why? I get an exception:
org.apache.jasper.JasperException: Invalid property in <set>: "p"
Biba Basnetti
Greenhorn

Joined: Oct 09, 2006
Posts: 25
I guess Head First was wrong about this.

It seems illegal to set <c:set> tag's TARGET attribute with <jsp:attribute> It doesn't work if you put EL or a scriplet inside the jsp:attribute body. It makes sense though because if you can't even set the target with a string literal variable that has a reference to an object why should you be able to use <jsp:attribute>? Isn't the body of the jsp:attribute tag going to be evaluated to a string literal too?

Here is a good example to show that it's true:


<c:set target="${myBean}">
<jsp:attribute name="property">p</jsp:attribute>
<jsp:attribute name="value">${myBean}</jsp:attribute>
</c:set>


The result:

mybeans.MyBean@13b9fae

So you put the EL ${myBean} in the body of jsp:attribute and assign it to the target attribute, you are assigning "mybeans.MyBean@13b9fae"
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
The "target" attribute may accept ELed/expressed value when "rtexprvalue" is turned on in tld. "Unforturnately" here it IS the case.
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
let's explore how tag handler classes use setter to deal with attributes.

tag form 1: attribute="name"
converted to: setAttribute(name),
inside the setter, we can code below,

{
name instanceof String?this.value=getValue(name):throw new Exception();
}

tag form 2: attribute="${name}"
converted to: setAttribute(value=getValue(name)),
inside the setter, we can code below,

{
this.value=value;
}

In the handlers we can continue set/get properties of value normally if it is bean object, otherwise an exception is thrown.
[ January 12, 2007: Message edited by: Bob CHOI ]
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Go on with "dynamic" attribute a little bit below

tag form 3: dynaAttrName="value"
converted to: javax.servlet.jsp.tagext.DynamicAttributes:setAttribute(namespace,dynaAttrName,value),
inside the setter, we can code below,

{
pageContext.setAttribute(dynaAttrName,value,PageContext.REQUEST_SCOPE);
}

tag form 4: dynaAttrName="${name}"
converted to: javax.servlet.jsp.tagext.DynamicAttributes:setAttribute(namespace,dynaAttrName,value=getValue(name)),
inside the setter, we can code similar to above.
Stary Kapec
Ranch Hand

Joined: Dec 04, 2005
Posts: 81
Hi,
Bob, I hardly follow your explanations. To put it simple <jsp : attribute > element has its use only with attributes which type is String. Am I right?
Don't you think it is not very useful then?

I observed another strange behaviour regarding attribute standard action.





It works, But:



It does not!
org.apache.jasper.JasperException: <h3>Validation error messages from TagLibraryValidator for c in /SimplePage.jsp</h3><p>18: Encountered illegal body of tag "c:set" tag, given its attributes.</p>

[ January 14, 2007: Message edited by: Jasiek Motyka ]
[ January 14, 2007: Message edited by: Jasiek Motyka ]
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Hi Jasiek

jsp:attribute might declare not accepting ELed stuff providing that it had used declarative "rtexprvalue=false".

Howerver the problem on the 2nd code snippet seems strange to me, too.
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Hi Jasiek

Sorry, i was mistaked about "rtexprvlaue" - it's all about attribute. The behavior on both code snippets might actually depend on "body-content" setting on c:set and jsp:attribute.

"body-content" has four optional values:
- empty
- scriptless
- jsp
- tagdependant

Therefore, "body-content=tagdependant" could cause them to act "irregually" till we find out how the tag handlers work accordingly.
 
Don't get me started about those stupid light bulbs.
 
subject: problem on c:set
 
Similar Threads
Mock exam question
jsp:getProperty gives null
bean in or out scope?
how to set the <bean:message key="abc.xyz" /> into jsp:setProperty parameter
JSP