aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Need help with JSTL: setting end attribute dynamically of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Need help with JSTL: setting end attribute dynamically of "substring" tag" Watch "Need help with JSTL: setting end attribute dynamically of "substring" tag" New topic
Author

Need help with JSTL: setting end attribute dynamically of "substring" tag

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
I have a scenario wherein i need to capitalize the first letter of a word. From Action class, i get everything in lowercase.



Above code works fine. ${abc.name}, say gives us value "india". Now i want to convert and display in my front end as "India".
Initially, iam getting the length of ${abc.name}, in this case for india length will be 5.This length is stored in a variable called "secondNameLength". As mentioned earlier the above code works fine
However, if I change the last line i.e

<str:substring start="1" end="5"><cut value="${hotelViewHelper.destinationName}"/></str:substring>

to


<str:substring start="1" end="${secondNameLength}"><cut value="${hotelViewHelper.destinationName}"/></str:substring>

then it doesnot work as expected. I printed this value separately${secondNameLength}, and it works fine. however within the "end" attribute it doesnot work as expected.


Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

Maan Suraj,

I am not sure why your code is not working well, but what you can try is rewriting it with EL functions:
refer to the following taglib uri


And add the following in your jsp:


In my case the String identified with e had a value of "errors" and the output was: E rrors

Regards,
Frits
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks,
but I cannot use "http://java.sun.com/jsp/jstl/functions" as our project uses struts1.0. So i cannot use the above approach. I need something which is possible with help of struts-tiles.tld.

Please help
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

I am not familiar with struts 1.1 (we are using struts 1.2.15) and also not with the tld file you are using. Please verify if the "end" attribute has an allowance <rtexprvalue>true</rtexprvalue>

What I don't understand is why you can't import a standard taglib (like jstl) in your page?

Regards,
Frits
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
when i try importing twhat you said i.e "..jstl/functions", gives an exception which says

ServletException in:/jsp/ah/abc.jsp] The absolute uri: http://java.sun.com/jsp/jstl/functions cannot be resolved in either web.xml or the jar files deployed with this application'

Please let me know if there is any alternative...
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

Have you verified that the "end" attribute in the tld-file that you use has an allowance <rtexprvalue>true</rtexprvalue> ?
Maybe the " end" attribute is not allowed to have a realtime expression value like you are using ${secondNameLength}.

Or you can download the standard jstl and put it in your application:
JSTL

Regards,
Frits
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

If you download for instance 1.1.2 you will find the jstl.jar and the standard.jar (in the lib directory).

put them in the /WEB-INF/lib directory and the container will now find the absolute url that I gave you (because the tld file contains that url and is inside the .jar file)
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
I verified "end" attribute in the tld-file is something like <rtexprvalue>true</rtexprvalue> . So i dont know what the issue is .Now as you mentioned i downloaded the JSTL and standard.jar


Now , i would like to know what exactly do i need to keep in place of "e" mentioned in your code
${fn:length(e)}


I tried the below two and tried printing but of no use



printed nothing but the code itself


Also below didnot helped either



Above didnot helped either

So please tell me how can i retrieve the values etc
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

it should be like this:

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299


Above code, due to some reason is not working properly. When i try to print "secondNameLength" using <cut value="${secondNameLength}"/>, prints the same i.e i see
${fn:length(abc.name)}
in my output jsp page.

If i do <cut value="${abc.name}"/>, then it prints "india", as expected.So "abc.name" is available in jsp, however, fn:length...etc method is not able to retrieve it somehow.


Now any idea why this happens?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

ok, this looks like EL is switched off, probably you are using Servlet 2.3 or earlier or EL is switched off for your application via the web.xml (check web.xml for <el-ignored>).
If you are using Servlet 2.3 or earlier (then EL is switched off by default) switch it on via the page directive


then try the following (without the c:set around it !)


this should now give output.....

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
This do helps Frits . But how do I Use <c:set> and other tags like <c:choose> tags now



Above code works fine.

However, if I include <c:choose>,<c:set> tags etc, i get an error which says
According to TLD or attribute directive in tag file, attribute value does not accept any expressions'
.

Now what is to be done? The modifications needs to be done in the existing jsp, so i need to keep all these tags like <c:set>, <c:choose> tags etc.

Just wanted to say thanks for your patience and quick and helpful responses.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

oke, now we are almost there.
The only problem that we are having now is that it is taking an old definition of the <c:set> where there is no rtexprvalue allowed. That old definition is mainly there for backwards compatablility. The standard.jar actually has 3 tld files with different definitions of the <c:set> and all the other tags (c.tld, c-1.0.tld and c1-0_rt.tld).

If you include the following page directive:

it will work for the c:set, but maybe not for the rest.

If not, download the jstl1.2 jar
JSTL 1.2

let us see if that works...
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

I have looked at it again and I think you are using the jstl1.0 where the <c:set> attribute value doesn't allow any rtexprvalue. From jstl1.1 onwards it allows real time expressions. Did you download jstl1.0 ?

Before we started with your question everything was working well (with c:set en c:choose). It seems we are now overruling your tld-file with an older one. Where does your taglib directive in your jsp point to? Did you change that?
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
From jstl1.1 onwards it allows real time expressions. Did you download jstl1.0 ?


I have downloaded JSTL1.1 only, so the issue is not here it seems.

Before we started with your question everything was working well (with c:set en c:choose)

<c:set> and <c:choose> worked well. The problem was that i was unable to set the value of "end" attribute dynamically.

Where does your taglib directive in your jsp point to? Did you change that?


Initially i had
abc.jsp


And few more <c:set> tags etc.
TaglibsCommon.jsp contains references to various struts taglibs. Please find below the code



After this i downloaded jstl1.1 & standard.jar 1.1 , and placed them in lib directory.

Now , my abc.jsp (please compare with above one) looks something like below (as per our discussions)


Above gives me output "value is 6" .Which is perfectly ok.

Now i change my abc.jsp a little bit and it becomes interesting


and now i get the error According to TLD or attribute directive in tag file, attribute value does not accept any expressions' .


Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

it is not clear to me yet why it is not working, but let's try this:

Don't include TaglibsCommon.jsp, but include the following taglibs in your abc.jsp:

This way we are sure that you are using the jstl1.1.
Have you got any taglib reference in your web.xml?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

Sorry change these (jstl1.0)

into (jstl1.1)
 
Don't get me started about those stupid light bulbs.
 
subject: Need help with JSTL: setting end attribute dynamically of "substring" tag