tags:

views:

5683

answers:

3

hi,

is there a way to substring in JSP files, using struts2 technologies? I mean, struts2 has its own taglib and also uses ognl. How can I get a substring from a stacked value or bean value?

thanks in advance.

stv

+1  A: 

http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html

Look for fn:substring and its variants.

I've used Struts 1, but not 2.

Tyler D
A: 

Watch out for the functions library in certain situations, especially when using Websphere to deploy! The company I work for deploys to Websphere 6.0 version 11, which does not support the functions library properly (it does not function properly when placed inside a tag body). I remember somewhere that they fixed it in version 13. You can always create your own JSP Tag to do anything, though, so you can do that to get around the problem.

MetroidFan2002
+1  A: 

Don't.

If you need to parse data (substring) in your JSP, then you are probably mixing business logic (how it works) with your presentation logic (how it is displayed)--they should be separate. If you are doing lots of conditionals, calculations, parsing, etc. in your JSPs, then you are creating a lot of (future) pain for yourself.

Instead, separate those concerns--make the JSP dead simple, with no logic other than displaying data as is or not at all, plus simple loops where needed. Put all the nontrivial logic into a Java class that pushes the data into the JSP, where you will have the full power of Java available. As much as you can, make the JSPs merely a thin "skin" over your Java-based application.

For a detailed discussion, see Terence Parr's white paper at http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf. Save yourself a lot of heartache and maintenance.

Rob Williams