|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
could not display java values in velocity vm templateGreetings,
i have just started out velocity and i not able to put the java values on the vm template. I have upload my file here. I'm using Jboss and dunmping the file into the server should work instantly this is the file: velocity.war.zip This is the java file where i have written the codes and to be pass to the velocity package com.velocityData.velocityObj; import java.io.StringWriter; import java.util.*; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.Velocity; import org.apache.velocity.context.Context; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; //import org.apache.velocity.servlet.VelocityServlet; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Properties; public class velocityTest { static String vmPath = ""; static String language = ""; public String handleRequest(ArrayList velocityList, ArrayList notificationList) throws Exception { vmPath = (String)velocityList.get(0); language = (String)velocityList.get(1); Properties p = new Properties(); p.setProperty( "resource.loader", "class" ); p.setProperty( "class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" ); VelocityEngine ve = new VelocityEngine(); ve.init(p); System.out.println("Inside the velocityTest_________________________"); ArrayList list = new ArrayList(); Map map = new HashMap(); //map.put("name", "horse"); //map.put("price", "$100.00"); //list.add(map); for ( int i=0; i < notificationList.size(); i++) { map.put("Variable"+i,notificationList.get(i)); } list.add(map); Collection c = map.values(); Iterator itr = c.iterator(); while(itr.hasNext()) System.out.println(itr.next()); VelocityContext context = new VelocityContext(); context.put("emailList", list); Template t = null; try { t = ve.getTemplate( vmPath + language); } catch (ResourceNotFoundException e) { e.printStackTrace(); } catch (ParseErrorException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } StringWriter writer = new StringWriter(); t.merge( context, writer ); // System.out.println( writer.toString() ); return writer.toString(); } } This is the vm template i create <HTML> <HEAD> <TITLE> Booking Confirmation</TITLE> </HEAD> <BODY> <br/><br/> #set( $count = 1 ) #foreach ( $email in $emailList) Transaction No: $email.variable0 <br/> Date: $email.variable1 <br/> <br/><br/> Total purchased: $email.variable2 <br/> Total amount charged: $email.variable3 <br/> <br/><br/> ============================================================================= <br/><br/> Dear $email.variable4 , $email.variable2 <br/> <br/><br/> Delivery: $email.variable5 <br/> <br/><br/> Your Internet Account number is $email.variable6 <br/><br/> #set( $count = $count + 1 ) #end Thank you </BODY> </HTML> I want to replace the $email.variable1, $email.variable2, $email.variable3 and so with the java values. But instead of display the value it display $email.variable1, $email.variable2 etc on the client browser. Please do give me guidance Thanks |
|
|
Re: could not display java values in velocity vm templateTo whom it may concern,
newbie-gero wrote: > ArrayList list = new ArrayList(); > Map map = new HashMap(); > > for ( int i=0; i < notificationList.size(); i++) { > map.put("Variable"+i,notificationList.get(i)); > } Note: key values start with "Variable" (with a leading capital letter). > list.add(map); Ok. [snip] > context.put("emailList", list); Ok. > #foreach ( $email in $emailList) > Transaction No: $email.variable0 > <br/> I think you want: Transation No: $email.Variable0 Velocity first looks for a method called "getVaraiable0()" on whatever object "email" is. Since "email" is a map, that method does not exist. Then, Velocity looks for a method called "get" that takes a String argument (which Map does), and calls get with the String from the reference (in this case, "varaiable0"). Note that the lookup is case-sensitive. I think you just need to capitalize "Variable0" and you'll be good to go. -chris |
| Free Forum Powered by Nabble | Forum Help |