• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I show part of my text in my template?

 
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I like to show just first 20 character of my text in my template, since desc in about 100 characters and I don't want to show all could you please tell me how can I do it?
<td class='trow'>${item.desc}</td>
Many thnaks for your help,
Elahe
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's easier than you think

${item.desc.substring(0,20)} should give you the first 20 characters.

Just remember that 'item.desc' is really calling the ' String getDescription() ' method of the ' item ' object. Since you are getting a String, you should be able to use any of the String methods on this object.

An alternate way of doing this is to have your backing class (item) have a second method, something like:Then in VTL call it like:
${item.shortDescription}
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike thank you soooooooo much for your answer.
So based on your suggestion I like to have a class
Tool1 and it's function is getting the text and the number and then get substry of that text with that number like my case the number is 20...so
Could you please help me and see Tool1 class functions based on your idea?

and then I can call this method in my template like:
$item.shortLen(${item.description},20)
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I can call this method in my template like:
$item.getShortDescription((${item.description},20)
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi elahe,
I wouldn't do it the way you are proposing. The method signature for your getShortDescription doesn't need that first string. The class containing this method already contains the string you are sending in. This is easy to see when you try to call it. You have item.getShortDescription, and then send it item.description. 'item' already *has* that description string.Now in your velocity servlet, you'd populate the values of an instance of this Tool1 class from a database, or with hard-coded values, and place it into the velocity context...And now in your velocity template, you can use any of the methods of Tool1 on your 'item' object.

If I've misunderstood, and you were meaning Tool1 class to be a utility class of some sort, to be re-used wherever... I think for this exact case, you are re-inventing the wheel. The substring method on java.lang.String does everything your 'getShortDescription' method does.. why wrap it?
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike thanks a lot for your big help and let post my clas based on your idea and mine for you to confim with you:
Here is Tool1 class:

and im my template:

and in my servlet:

and I just don't know where I should put
Tool1 oTool = new Tool1() in my servlet! here is my servlet
Your input is a big help for me,
Elahe
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Elahe,

Now that I see more of your code, I can tell a bit more. Your vbug vector must contain hashtables, one for each bug? Or something like that.. maybe it's your own 'bug' object.

If it is, I still think the best place for the 'getShortDescription' method is in that object. (in the 'bug' class not the Tool1 class at all). And access it like I showed earlier
Otherwise, I would make the method static, and you needn't place an instance of the Tool1 class into the context at all. Hopefully that would work, I don't have Velocity handy right now. Call would look like:I'm half certain it won't. So where you have it in your code is appropriate (way down at the bottom there)
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
Many thanks for following up with me...now it is working but I don't know why this error happends right after showing half of the Bugs list in template:
Could you please give me your advice?
Many thanks,
Elahe
--------------
VelocityServlet : Error processing the template
org.apache.velocity.exception.MethodInvocationException: Invocation of method 'getShortDesc' in class com.srs.veltools.Tool1 threw exception class java.lang.StringIndexOutOfBoundsException : String index out of range: 20
org.apache.velocity.exception.MethodInvocationException: Invocation of method 'getShortDesc' in class com.srs.veltools.Tool1 threw exception class java.lang.StringIndexOutOfBoundsException : String index out of range: 20 at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:308) at
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
It has been solved this way...
Many thanks for your help and support,
Elahe
 
reply
    Bookmark Topic Watch Topic
  • New Topic