velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Neal Smith" <Neal.Sm...@sas.com>
Subject RE: Struts and I18N
Date Thu, 25 Aug 2005 12:21:34 GMT
Hi Nathan,

  Thanks for the reply.

  I have looked at the examples and they are fine for syntax of the API, but I think I've
got more of an issue with how the emails are being generated, like you mention below. 

  I have another quick question.  In the rest of the application, I'm using JSTL fmt library
tags to format dates, currency, etc. for I18N.  I don't think I'm going to be able to do that
with Velocity, correct?


  Disclaimer:  I didn't come up with this scheme!  :)

  We have a JSP where we show the user some boilerplate email text, allow them to manipulate
it and the email subject, specify an email address and then they submit this.  

  Within the struts action class we call into a struts service to send the email.  That method
creates a couple of VelocityContexts (two different template files are used), that represent
email attachments.  This is all packaged up using MimeMultipart to create the message that
we then send via javax.mail.Transport.

  Here is the service init method where the VelocityEngine is initialized:

public void init(Properties initProperties)
	   throws ServiceException
	{
		_audit = (AuditTrailService) this.getApplicationServices().get("AuditTrailService");
		_mailFromUserID = initProperties.getProperty("sasaml.mail.from.user.id");
		_mailFromUserName = initProperties.getProperty("sasaml.mail.from.user.name");
		_mailSMTPUserID = initProperties.getProperty("sasaml.mail.smtp.user.id");
		_mailSMTPPassword = initProperties.getProperty("sasaml.mail.smtp.password");
		_mailFileExtension = initProperties.getProperty("sasaml.mail.file.extension");
		_alertServ = (AlertService) getApplicationServices().get("AlertService");		
		
		String _ccSelf = initProperties.getProperty("sasaml.mail.ccSelf");
		
		if (_ccSelf != null && _ccSelf.equals("enabled"))
		    ccSelf = true;
		else
		    ccSelf = false;

		_msgs = ResourceBundle.getBundle("ApplicationResources");
		_appTitle = _msgs.getString("app.title.txt");

		InputStream is = this.getClass().getResourceAsStream(VELOCITY_PROPERTIES_FILE);
		Properties velocityProperties = new Properties();
		_velocityEngine = new VelocityEngine();

		try
		{
			velocityProperties.load(is);
		}
		catch (IOException ioe)
		{
			throw new ServiceException("Unable to load Velocity properties file: "
			                           + VELOCITY_PROPERTIES_FILE, ioe);
		}

		try
		{
			_velocityEngine.init(velocityProperties);
		}
		catch (Exception e)
		{
			throw new ServiceException("Unable to initialize Velocity engine.", e);
		}

		String mailSessionName = initProperties.getProperty("sasaml.mail.session.name");

		try
		{
			Context ctx = new InitialContext();
			_mailSession = (Session) ctx.lookup(mailSessionName);
		}
		catch (Exception e)
		{
			throw new ServiceException("Problem initializing AlertEmailService. Unable to look up MailSession:
"
			                           + mailSessionName, e);
		}

		_mailHost = _mailSession.getProperty("mail.smtp.host");
	}



  Here is the method where we are "generating" the template:

private File createFile(String prefix, String template, VelocityContext context)
	   throws ServiceException
	{
		Writer writer = null;
		File file = null;

		context.put("styleSheet", VELOCITY_TEMPLATE_PATH + STYLE_SHEET_NAME);

		try
		{
			file = File.createTempFile(prefix, ".tmp");
			file.deleteOnExit();
			writer = new BufferedWriter(new FileWriter(file), BUFFER_SIZE);
		}
		catch (IOException e)
		{
			throw new ServiceException("Unable to create temp " + prefix + " file or get writer.",
e);
		}

		try
		{
			//			alertTemplate.merge(alertMap, writer);
			_velocityEngine.mergeTemplate(VELOCITY_TEMPLATE_PATH + template, context, writer);
		}
		catch (Exception e)
		{
			throw new ServiceException("Error generating " + prefix + " file.", e);
		}
		finally
		{
			try
			{
				writer.close();
			}
			catch (IOException e)
			{
				_log.warn("Unable to close writer for file: " + file.getName(), e);
			}
		}

		return file;
	}

   
Thanks,
Neal



-----Original Message-----
From: Nathan Bubna [mailto:nbubna@gmail.com] 
Sent: Wednesday, August 24, 2005 5:44 PM
To: Velocity Users List
Subject: Re: Struts and I18N

Hi Neal,

have you checked out the Struts example application in VelocityTools? 
It includes a number of "mini-webapps" that demonstrate using various Struts features with
VelocityTools.  I believe the mini-webapp "app1"
demonstrates use of the MessageTool for internationalization.

also, i'm curious where/how your are generating these emails?  are you doing this within the
servlet? is it as part of a servlet request? 
where/how are you creating/getting a Context to merge with the template?

there's a lot that's unclear to me about the way you are approaching this.  and to try and
clear up your last curiousity there:  yeah, the VelocityViewServlet is the main way in which
the MessageTool is initialized and added to the Context before processing the template. 
so, depending on where/how you are processing your email templates, it may be easy to get
a working MessageTool, or it may not.  it's hard to say without knowing your setup better.

On 8/24/05, Neal Smith <Neal.Smith@sas.com> wrote:
> Hi All,
> 
>   I am new to Velocity and am working on part of our application written by someone else.
> 
>   The app is using Velocity to generate emails.  Currently, we have templates that have
hardcoded English text in them.  I am trying to I18N these templates so that they pick up
text from our .properties files.
> 
>   I believe I don't have things setup correctly, but I can't seem to find a "full" example
that explains everything end-to-end, so I can't figure out how to verify this.  From Googling,
I see that lots of people seem to have similar problems, but none of their solutions seem
to work for me.  I think it's involved with how we use the templates, but I'm too new to know
for sure.
> 
>   I have dl'd the 1.1 Tools.  I have only copied the velocity-tools-1.1.jar to my lib
directory, because it didn't seem like I needed the rest of it and I already have Struts.
 (Am I wrong?)  I have setup the web.xml and toolbox.xml just like the Velocity website says.
 I have updated part of the template with the following line:
> 
> <head>
>   <title>$msg.get("app.title.txt") - Alert Details</title>
> 
>   #include($styleSheet)
> 
> </head>
> 
>   When I run this I get:
> 
> 2:4:43 WARN  lang 
> org.apache.velocity.runtime.exception.ReferenceException: refe rence : 
> template = /com/sas/aml/velocity/email/alert_details.vm [line 5,column 1 0] : $msg.get("app.title.txt")
is not a valid reference.
> 
> 
> 
>   The real interesting part, to me anyway, is that we are not accessing these templates
through regular Struts forwards, etc.  From what I understand VelocityViewServlet is necessary
to use the MessageTool?  We are using VelocityEngine (via mergeTemplate) and I believe that
I'll need to replace this with usage of VelocityViewServlet?
> 
>   As you can tell, I'm getting very fuzzy on all of this last part.  Any help is appreciated.
> 
> 
> Thanks,
> Neal Smith
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
>

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


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


Mime
View raw message