Getting Substring using JSF
I am new to JSF , (using JSF and struts application ) .I was wondering how I could do basic functions like string manupilation using the JSF core tages ...
For Example if I have
<h:outputText value="#{gen.Name}" />
and I would like to get not the name but say the substring(0,5) ..how would I do this with JSF ...
Also could you please direct me to a resource where I can learn more about the Core (helpful) functions using JSF
I'm not sure if they all work with JSF, but you can always try the JSTL function library. A description can be found here:
http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/fn/tld-summary.html
I use fn:length, but I haven't tried any of the others.
You also have the option of adding additional getters to your backing bean that return different values based on the same underlying model data.
Hope that helps.
Regards,
Peter
I would want to change the substring in the Viewer and not in the Bean ..as I would not want to change the underlying bean ...Does't the JSF have something on this lines ..to manupilate the string...?by itself ?
Perhaps you could use a converter.
package com.test.converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
public final class SubstringConverter implements Converter {
public SubstringConverter() {
super();
}
public Object getAsObject( FacesContext arg0, UIComponent arg1, String d ) {
return d;
}
public String getAsString( FacesContext arg0, UIComponent arg1, Object arg2) {
return ((String)arg2).substring(0,5);
}
}
The corresponding entry in faces-config.xml
<converter>
<converter-id>mySubstringConverter</converter-id>
<converter-class>com.test.converter.SubstringConverterr</converter-class>
</converter>
Now use it!<h:outputText value="#{gen.Name}" converter="mySubstringConverter"/>
As "petergoldstein" said, you can use the standard JSTL function library.
(Using "converters" is appropriate where you need to do translation in both directions for a value that the user is entering).
If you are using "standard" jsp syntax, you just need to declare the tag library namespace with the following near the top of your JSF page:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Then your output text tag would become
<h:outputText value="#{fn:substring(gen.Name,0,5)}" />
Or you can define your own functions in a tag library fairly easily by creating static methods in a class -- see the JavaEE 5 tutorial at "http://java.sun.com/javaee/5/docs/tutorial/doc/index.html" for details
(specifically http://java.sun.com/javaee/5/docs/tutorial/doc/JSPIntro7.html#wp102387)
IMHO, I think it would be nice if the unified EL had more direct support for calling methods on objects, but you can always work around it for now.
YOu have a backing bean, so why don't you create another method, to which you bind in the tag that you want, and in that method do the substring stuff? It's cleaner. And you don't put that kind of code in the page.The objective of JSF is this. Simplify the pages....
I gave this a try and found out jstl functions don't work inside of JSF EL with JSF1.1/JSP 2.0/JSTL 1.1.The doc link you gave is for JSP 2.1 which seems to be part of Java EE 5.From what I can tell Java EE 5 is only in Beta/Preview.
I tried the
<h:outputText value="#{fn:substring(gen.tcInfo,0,5)}" />
But got an exception am I doing something wrong .....
javax.faces.FacesException: Cannot get value for expression '#{fn:substring(gen.tcInfo,0,5)}'
org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:421)
org.apache.myfaces.application.jsp.JspTilesViewHandlerImpl.dispatch(JspTilesViewHandlerImpl.java:233)
org.apache.myfaces.application.jsp.JspTilesViewHandlerImpl.renderView(JspTilesViewHandlerImpl.java:219)
org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:352)
so what could be a better solution other the Bean , I would not like to add new fiellds to Beans as I would be displaying 1000+ records as the same time with each record having 6-7 variables that have to be used for string manupilations ....
Are you working with a datatable?
If so, from where the data came from?
If is from a datatable, you can do that in there.
If you don't want to do that way, you can do that in the model of the datatable (in the bean).
You don't have to create new methods.
See the datamodels.
> If you don't want to do that way, you can do that in
> the model of the datatable (in the bean).
The Problem is that I would need both untruncated string and the Truncated one .So if I do it on the Bean side I would essentially have to double the methods for each variable ...
The idea is to have the Truncated String visible to the user ...and the Untruncated String appears with a JavaScript (toolTip or a sepearate window) .The strings I am talking about are bio sequences (gene sequences ) and can be very long ..hence the truncation to fit in a Table .