You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Rob Ratner <pr...@yahoo.com> on 2005/07/14 17:58:09 UTC

Velocity JSP help

Hello all. Does any know how to do the following in a Velocity Template.
 
Dynamically figure out the current year and set up a drop down with a certain range of years to be shown in the drop down field.
 
Thanks a ton.

		
---------------------------------
 Start your day with Yahoo! - make it your home page 

Re: Velocity JSP help

Posted by Will Glass-Husain <wg...@forio.com>.
Actually, not quite.

Velocity needs a servlet to take web requests, fill up a context with 
"references" (that can be used within the page) and display the Velocity 
page.

This is simpler than it sounds.  The Velocity Tools subproject provides 
VelocityViewServlet, which does most of this for you automatically.  You can 
define standard references in an XML file, or subclass VelocityViewServlet 
to add more functionality.  (that's what I think you'd need to do in this 
case).
http://jakarta.apache.org/velocity/tools/

Read this article for more info.  (As a side note: we just wrote it - it's 
intended to help the new Velocity user make sense of the options for web 
applications.  Let us know if it's useful).
http://jakarta.apache.org/velocity/webapps.html

Best, WILL

P.S.  I suggest you respond to the "velocity-user" list instead of 
"velocity-dev", which is intended for discussion on "how to use Velocity".

----- Original Message ----- 
From: "Rob Ratner" <pr...@yahoo.com>
To: "Velocity Developers List" <ve...@jakarta.apache.org>
Sent: Thursday, July 14, 2005 10:27 AM
Subject: Re: Velocity JSP help


> Thanks Will, I am new to Velocity, but can embed Java in a Velocity 
> template similar to JSPs with such a simple task for my web page?
> Thanks
> Rob
>
> Will Glass-Husain <wg...@forio.com> wrote:
> The easiest way is to populate a list with a range of years in your 
> servlet,
> then pass the list in as a reference in the context.
>
> WILL
>
> ----- Original Message ----- 
> From: "Rob Ratner"
>
> To:
> Sent: Thursday, July 14, 2005 8:58 AM
> Subject: Velocity JSP help
>
>
>> Hello all. Does any know how to do the following in a Velocity Template.
>>
>> Dynamically figure out the current year and set up a drop down with a
>> certain range of years to be shown in the drop down field.
>>
>> Thanks a ton.
>>
>>
>> ---------------------------------
>> Start your day with Yahoo! - make it your home page
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Velocity JSP help

Posted by Daniel Rall <dl...@finemaltcoding.com>.
Mind taking this discussion over to the user list?  Thanks Rob!

On Fri, 2005-07-15 at 08:17 -0700, Rob Ratner wrote:
> Hello Ms. Falco, I don't mean to be ignorant but I am new to Velocity. May I ask a couple probably stupid questions.  ParserCommons is a Java class part of the Velocity API? Where does it sit on the webserver and where does ParserDateUtils.java sit as a .class/jar file so it will communicate with with the velocity template/*.vm file.  My web application sits on WebLogic 8.1 and has the current structure when deployed.
>  
> appname/templates/.....*.vm
> appname/WEB-INF/lib/velocity-1.1.jar
> appname/WEB-INF/lib/app.jar
>  
> velocity-1.1.jar contains the following apache packages:
>  
> org.apache.velocity.util.*
> org.apache.velocity.test.*
> org.apache.velocity.runtime.parser.*
> org.apache.velocity.runtime.*
> org.apache.velocity.io.*
> org.apache.velocity.exception.*
> org.apache.velocity.context.*
> org.apache.velocity.app.*
> org.apache.velocity.anakia.*
> org.apache.velocity.util.introspection.*
> org.apache.commons.collections.*
> org.apache.log.*
>  
> 
> The app.jar just contain the business logic api.
>  
> Any comments/suggestions would be tremendously appreciated.
>  
> Thanks
> Rob
>  
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> Llewellyn Falco <is...@setgame.com> wrote:
> yeah, here's a utility class, i usually place a accessor method to it in my 
> ParserCommons.
> 
>  #foreach($month in $commons.getDateUtils().getMonths()) $month.getDisplayText()#end
>  #foreach($day in $commons.getDateUtils().getDaysOfMonth()) $day.getDisplayText()#end
>  #foreach($year in $commons.getDateUtils().getNextXYears(10)) $year.getDisplayText()#end 
> package com.spun.util.velocity;
> 
> import java.util.Calendar;
> import java.util.GregorianCalendar;
> import com.spun.util.StringUtils;
> 
> /***********************************************************************/
> public class ParserDateUtils
> {
> public static final ParserDateUtils INSTANCE = new ParserDateUtils();
> public static Month[] getMonths()
> {
> return new Month[]{new Month("00", "------"), new Month("01", "Jan (01)"), new Month("02", "Feb (02)"), new Month("03", "Mar (03)"), new Month("04", "Apr (04)"), new Month("05", "May (05)"), new Month("06", "Jun (06)"), new Month("07", "Jul (07)"), new Month("08", "Aug (08)"),
> new Month("09", "Sep (09)"), new Month("10", "Oct (10)"), new Month("11", "Nov (11)"), new Month("12", "Dec (12)")};
> }
> /***********************************************************************/
> public static Day[] getDaysOfMonth()
> {
> Day[] days = new Day[32];
> days[0] = new Day("00", "--");
> for (int i = 1; i <= 31; i++)
> {
> days[i] = new Day(i);
> }
> return days;
> }
> /***********************************************************************/
> public static Year[] getNextXYears(int x)
> {
> Year[] years = new Year[x + 1];
> years[0] = new Year("0000", "----");
> int startingYear = new GregorianCalendar().get(Calendar.YEAR);
> for (int i = 0; i < x; i++)
> {
> years[i + 1] = new Year(startingYear + i);
> }
> return years;
> }
> /***********************************************************************/
> /***********************************************************************/
> /***********************************************************************/
> public static class Year extends DateValue
> { 
> /***********************************************************************/
> public String getTwoDigitNumber()
> {
> return getNumber().substring(2);
> }
> public Year(String number, String displayText)
> {
> super(number, displayText);
> }
> /***********************************************************************/
> public Year(int i)
> {
> super("" + i, "" + i);
> }
> 
> }
> /***********************************************************************/
> public static class Day extends DateValue
> {
> public Day(int i)
> {
> super(StringUtils.padNumber(i, 2), "" + i);
> }
> public Day(String number, String displayText)
> {
> super(number, displayText);
> } 
> }
> /***********************************************************************/
> public static class Month extends DateValue
> { 
> public Month(String number, String displayText)
> {
> super(number, displayText);
> }
> }
> /***********************************************************************/
> public static class DateValue
> {
> private String number;
> private String displayText;
> /***********************************************************************/
> public boolean isDefault()
> {
> return displayText.startsWith("--");
> }
> /***********************************************************************/
> public DateValue(String number, String displayText)
> {
> this.number= number;
> this.displayText = displayText;
> }
> /***********************************************************************/
> public String getNumber()
> {
> return number;
> }
> /***********************************************************************/
> public String getDisplayText()
> {
> return displayText;
> }
> /***********************************************************************/
> }
> }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: Velocity JSP help

Posted by Rob Ratner <pr...@yahoo.com>.
Hello Ms. Falco, I don't mean to be ignorant but I am new to Velocity. May I ask a couple probably stupid questions.  ParserCommons is a Java class part of the Velocity API? Where does it sit on the webserver and where does ParserDateUtils.java sit as a .class/jar file so it will communicate with with the velocity template/*.vm file.  My web application sits on WebLogic 8.1 and has the current structure when deployed.
 
appname/templates/.....*.vm
appname/WEB-INF/lib/velocity-1.1.jar
appname/WEB-INF/lib/app.jar
 
velocity-1.1.jar contains the following apache packages:
 
org.apache.velocity.util.*
org.apache.velocity.test.*
org.apache.velocity.runtime.parser.*
org.apache.velocity.runtime.*
org.apache.velocity.io.*
org.apache.velocity.exception.*
org.apache.velocity.context.*
org.apache.velocity.app.*
org.apache.velocity.anakia.*
org.apache.velocity.util.introspection.*
org.apache.commons.collections.*
org.apache.log.*
 
 
The app.jar just contain the business logic api.
 
Any comments/suggestions would be tremendously appreciated.
 
Thanks
Rob
 
 
 
 
 
 
 
 
 
 
 
 
Llewellyn Falco <is...@setgame.com> wrote:
yeah, here's a utility class, i usually place a accessor method to it in my 
ParserCommons.

 #foreach($month in $commons.getDateUtils().getMonths()) $month.getDisplayText()#end
 #foreach($day in $commons.getDateUtils().getDaysOfMonth()) $day.getDisplayText()#end
 #foreach($year in $commons.getDateUtils().getNextXYears(10)) $year.getDisplayText()#end 
package com.spun.util.velocity;

import java.util.Calendar;
import java.util.GregorianCalendar;
import com.spun.util.StringUtils;

/***********************************************************************/
public class ParserDateUtils
{
public static final ParserDateUtils INSTANCE = new ParserDateUtils();
public static Month[] getMonths()
{
return new Month[]{new Month("00", "------"), new Month("01", "Jan (01)"), new Month("02", "Feb (02)"), new Month("03", "Mar (03)"), new Month("04", "Apr (04)"), new Month("05", "May (05)"), new Month("06", "Jun (06)"), new Month("07", "Jul (07)"), new Month("08", "Aug (08)"),
new Month("09", "Sep (09)"), new Month("10", "Oct (10)"), new Month("11", "Nov (11)"), new Month("12", "Dec (12)")};
}
/***********************************************************************/
public static Day[] getDaysOfMonth()
{
Day[] days = new Day[32];
days[0] = new Day("00", "--");
for (int i = 1; i <= 31; i++)
{
days[i] = new Day(i);
}
return days;
}
/***********************************************************************/
public static Year[] getNextXYears(int x)
{
Year[] years = new Year[x + 1];
years[0] = new Year("0000", "----");
int startingYear = new GregorianCalendar().get(Calendar.YEAR);
for (int i = 0; i < x; i++)
{
years[i + 1] = new Year(startingYear + i);
}
return years;
}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
public static class Year extends DateValue
{ 
/***********************************************************************/
public String getTwoDigitNumber()
{
return getNumber().substring(2);
}
public Year(String number, String displayText)
{
super(number, displayText);
}
/***********************************************************************/
public Year(int i)
{
super("" + i, "" + i);
}

}
/***********************************************************************/
public static class Day extends DateValue
{
public Day(int i)
{
super(StringUtils.padNumber(i, 2), "" + i);
}
public Day(String number, String displayText)
{
super(number, displayText);
} 
}
/***********************************************************************/
public static class Month extends DateValue
{ 
public Month(String number, String displayText)
{
super(number, displayText);
}
}
/***********************************************************************/
public static class DateValue
{
private String number;
private String displayText;
/***********************************************************************/
public boolean isDefault()
{
return displayText.startsWith("--");
}
/***********************************************************************/
public DateValue(String number, String displayText)
{
this.number= number;
this.displayText = displayText;
}
/***********************************************************************/
public String getNumber()
{
return number;
}
/***********************************************************************/
public String getDisplayText()
{
return displayText;
}
/***********************************************************************/
}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: Velocity JSP help

Posted by Llewellyn Falco <is...@setgame.com>.
yeah, here's a utility class, i usually place a accessor method to it in my 
ParserCommons.

<select name="shippingDateMonth">
        #foreach($month in $commons.getDateUtils().getMonths())
         <option value="$month.getNumber()">$month.getDisplayText()
         </option>#end
       </select>
       <select name="shippingDateDay">
        #foreach($day in $commons.getDateUtils().getDaysOfMonth())
         <option value="$day.getNumber()">$day.getDisplayText()
         </option>#end
       </select>
       <select name="shippingDateYear">
        #foreach($year in $commons.getDateUtils().getNextXYears(10))
         <option value="$year.getNumber()">$year.getDisplayText()
         </option>#end
       </select> 

Re: Velocity JSP help

Posted by Rob Ratner <pr...@yahoo.com>.
Thanks Will, I am new to Velocity, but can embed Java in a Velocity template similar to JSPs with such a simple task for my web page?
Thanks
Rob

Will Glass-Husain <wg...@forio.com> wrote:
The easiest way is to populate a list with a range of years in your servlet, 
then pass the list in as a reference in the context.

WILL

----- Original Message ----- 
From: "Rob Ratner" 

To: 
Sent: Thursday, July 14, 2005 8:58 AM
Subject: Velocity JSP help


> Hello all. Does any know how to do the following in a Velocity Template.
>
> Dynamically figure out the current year and set up a drop down with a 
> certain range of years to be shown in the drop down field.
>
> Thanks a ton.
>
>
> ---------------------------------
> Start your day with Yahoo! - make it your home page 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: Velocity JSP help

Posted by Will Glass-Husain <wg...@forio.com>.
The easiest way is to populate a list with a range of years in your servlet, 
then pass the list in as a reference in the context.

WILL

----- Original Message ----- 
From: "Rob Ratner" <pr...@yahoo.com>
To: <ve...@jakarta.apache.org>
Sent: Thursday, July 14, 2005 8:58 AM
Subject: Velocity JSP help


> Hello all. Does any know how to do the following in a Velocity Template.
>
> Dynamically figure out the current year and set up a drop down with a 
> certain range of years to be shown in the drop down field.
>
> Thanks a ton.
>
>
> ---------------------------------
> Start your day with Yahoo! - make it your home page 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org