I am sending emails using amazon java sdk. I have to send html template as mail. I have written a program for this and it is working fine. But now I am storing the whole html code in a single String. But whenever i need to edit the template, I have to edit the program (I mean the String variable). And also I have to take care the special characters like " \ ...etc in that html code. Please suggest me an elegant way to solve this issue.
Use a template engine for that and store your template externally either in class path or on a file system. Here is a question that may help you selecting one: https://stackoverflow.com/questions/2381619/best-template-engine-in-java
Use Apache Common Lang api's StringEscapeUtils#escapeHtml, It escapes the characters in a String using HTML entities and return a new escaped String, null if null string input.
For example:
"US" & "UK"
becomes:
"US" & "UK".
Check the Apache Velocity Project. You can create template for several things. From it's user-guide page
Velocity can be used to generate web pages, SQL, PostScript and other output from templates. It
can be used either as a standalone utility for generating source code and reports, or as an
integrated component of other systems.
You can use a VTL(Velocity Template Language) . A example from above link
<HTML>
<BODY>
Hello $customer.Name!
<table>
#foreach( $mud in $mudsOnSpecial )
#if ( $customer.hasPurchased($mud) )
<tr>
<td>
$flogger.getPromo( $mud )
</td>
</tr>
#end
#end
</table>
Better and easiest way is reading the html file line by line using simple file reading operation and append this each line to a single String. And also I found this solution (also a better one, if you are ready to add one more library file to your project) from SO.
Related
I'd like creare a simple parser in Java that analize a string containing html and replace custom tags and if else/elseif statements, similar to Mailchimp.
Actually I simply replace my custom tags for example: *|NAME|*
with the name of the recipient, *|AGE|* whith the age of the recipient and so on.
I'd like to add conditional statements to permit expressions like:
<html>
<head>....</head>
<body>
*|IF:GENDER=M|*
Hello Mr.<b>*|NAME|*</b>
*|ELSEIF:GENDER=F|*
Hello Mrs.<i>*|NAME|*</i>
*|ELSE:|*
Dear customer
*|END:IF|*
<p>Bla bla bla</p>
....
....
</body>
</html>
In short, a very similar sintax to Mailchimp one.
The user can write his own html and add a custom syntax to customize the content based on data.
I think I'm able to create a code that works but I'm searching for the best practice to implement it. I looking for a good way to follow to implements a good code.
Would be nice if the parser can manage nested if/elseif/else statements.
I have a requirement to replace all html comments in all jsp file in my work space with jsp comments. I need to search for all and replace with <%-- --%> and I should not loose the comments.
For Eg
<!-- I have a comment here --> should be replaced with.
<%-- I have a comment here --%>
I am using eclipse to do this. Please let me know if I have some other better way to do it.
You could go through the file (line by line) and do something like this:
line.replaceAll("<!--","<%--"); //or line.replace("<!--","<%--");
line.replaceAll("-->","--%>"); //or line.replace("-->","--%>");
Perlish way:
Run this through your command line over the jsp folder
perl -pi -e 's/<!--/<%/g;s/-->/--%>/g' jspfolder/*
I have scenario where i have to insert header manually which looks like below
Header:
<%# package="test"
imports="
/* Many imports i know the list*/
"
%>
I have achieved this by string concatenation like sample below:
String header="<%# jet package"+"="+"\"Testmodule\"" +"\n"+"imports"+"="+"\"java.io.File"+"\n"+
"utils.ProjectUtils"+"\n"
+"\""+" parallel="+"\"true\""+"%>"+"\n";
Note: I have more than 10 imports.This is just sample.
since the text is more and requires also proper alignment(like each import on next line and also escape "") when i am inserting header into other file, I want to know if there are any alternatives to do this in efficient way in java.
I assume this is a JSP Question
If it is, then use a static include (A static include is resolved at compile time)
<%#include file="includes/header.jsp" %>
or for for dynamic inclusion:
<jsp:include page="..." />
I get html code from server to build freemarker.ftl.
Example:
Server return:
String htmlCode="<h1>Hello</h1>";
freemarker.ftl
${htmlCode}
except:Hello
actually: <h1>Hello</h1>
what can i do?
By default FreeMarker has no auto-escaping on, so it should print that value as HTML. But as it doesn't as you say, I can imagine two possibilities:
You are inside <#escape x as x?html>...</#escape>, or that was added to the template by a custom TemplateLoader. In that case, in 2.3.x you have to write <#noescape>${htmlCode}</#noescape>. (In 2.4 it will be much less verbose if everything goes as planned.)
That value was escaped before it reaches FreeMarker. So the template already gets <h1>Hello</h1> as the string.
I know I can use expression in play framework template like this.
<h1>Client ${client.name}</h1>
What to do if I need to include ${client.name} in html tag?
For example,
<h1 id=${client.name}>Client ${client.name}</h1>
However, this way doesn't works.
First replace that example because the id need a "" to use like this
<h1 id="${client.name}">Client ${client.name}</h1>
then to use java code in your template use the %{ java code here }% tags you can create variables and use it like %{ string mystring = "hello";}% then in the template<h2>${mystring}</h2>.
here are one example from my real code:
%{String selected = ""; if (permisoseleccionado?.Permiso?.id == tp?.id){selected = "selected";}}%
this post is old but I thing that this will be usefull to other people.
see ya!