Nisha
Nisha

Reputation: 11

Converting a velocity variable to java.lang.String

I have a velocity variable "topic.url" inside a jsp page. This code happens to be written by some other person so I am not very sure where exactly this variable came from. Anyway this variable gives me a particular URL which I have to parse and extract particular field. I am planning to write a java function to do this. The problem is when I pass this velocity variable to the function what datatype should I use. I tried converting it into string but that does not work. Here's the snippet of the code:

<html>
<head>
<%!

public String parse(String url)
{
    url="abc";  
    return(url);
}
%>

    <meta name="email.subject" content="Community name:{community.name},Topic Name:{topic.name},Topic URL:<%= parse({topic.url}) %>">
</head>

Upvotes: 1

Views: 1990

Answers (5)

DPM
DPM

Reputation: 2030

I'm coming quite late for the answear but I had a similar problem. I ended up realising that velocity was looking for the "getUrl" method for my "something.url", whereas the actual method name was "getURL" -notice uppercase-. In that case you just have to explicitly write "${something.getURL()}" instead of the former "${something.url}".

That solved it for me.

Upvotes: 0

nisha, wht if u totally removed d jsp code & instead found out wht data / object type topic.url is and used its method directly inside d braces? lets assume that it is a string. what if u used sthg like {topic.url.substring(index where id starts)} to extract the id out of d url ?

i mean e.g.
<meta name="email.subject" content="Community name:{community.name},Topic Name:{topic.name},Topic URL:{topic.url.substring(30)}">

bottom line, i mean just use d appropriate data type specific methods within d braces.

Upvotes: 0

Nathan Bubna
Nathan Bubna

Reputation: 6933

A Velocity variable inside a jsp page??? Then it's not a velocity variable. It's a jsp var. You are either writing VTL or JSP. You can embed VTL in a JSP with the VelocityViewTag, but that's the only way to have a Velocity variable inside a JSP. And that doesn't look like the case.

Upvotes: 1

Mike Clark
Mike Clark

Reputation: 10136

I think you cannot pass a velocity template parameter to the function with such syntax:

<%= parse({topic.url}) %>

You could try the approach below instead. Note that there is almost certainly a much better way to do get this job done, using tools that Velocity provides. The below is kind of and ugly hack to get you up and running. I highly suggest reworking this approach to better leverage Velocity's facilities:

public String parseTopicUrl(javax.servlet.http.HttpServletRequest request) throws Exception {
    Object topic = request.getAttribute("topic");
    if (topic == null) {
        System.out.println(">>>null topic");
        return null;
    }
    Class topicClass = topic.getClass();
    java.lang.reflect.Method method = topicClass.getMethod("getUrl", null);
    Object url = method.invoke(topic, null);
    if (url == null) {
        System.out.println(">>>'url' is null");
    } else {
        System.out.println(">>>'url' class is " + url.getClass());
        System.out.println(">>>'url' toString is " + url);
    }
    // TODO: cast 'url' to its real class and work with it
    return null;
}

Then use:

<%= parseTopicUrl(request) %>

Upvotes: 0

ch4nd4n
ch4nd4n

Reputation: 4197

As far as I know, you cannot write Java functions inside a velocity template. You would have to retort to Velocity macro. Else write a factory method and it should be available in Velocity context.

Upvotes: 0

Related Questions