spring email buttons not working - java

I want to send a email message, Will have 1 link to load image from a url.
Then 2 buttons. one button to re direct to google.com and one link to redirect cnn.com
#Autowired
public JavaMailSender emailSender;
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo("ratfat#gmail.com");
helper.setSubject("Test subject");
String googleLink="http://google.com";
String cnnLink="http://cnn.com";
String emailContent="\r\n" +
"<html>\n" +
" <body> \n" +
" <div align='center'>\n" +
" <div style='text-align:center;'>\n" +
" <IMG src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png' height='75' width='150'>\n" +
" </div>\n" +
" \n" +
" <br/><br/>\n" +
" Would you like to take it?<br/><br/><br/> \n" +
" <br/> \n" +
" <input type=\"button\" id=\"acceptAcct\" onclick=\"location.href='"+googleLink+"';\" style=\"width:10em;height:5em\" value=\"Click to load google\" />    \n" +
" <input type=\"button\" id=\"rejectAcct\" onclick=\"location.href='"+cnnLink+"';\" style=\"width:10em;height:5em\" value=\"Click to load CNN\" /> \n" +
" </div>\n" +
" \n" +
" </body>\n" +
" </html>\n";
helper.setText(emailContent, true);
message.setContent(message, "text/html");
emailSender.send(message);
I copy the html printed out to a simple test.html and it works well in my browse.
But when the email is recieved, i try clicking the button nothing happens.
In the email ,when i use chrome debug to see the code, the input buttons are like below.
<input type="submit" id="m_-5702442034034898245acceptAcct" style="width:10em;height:5em" value="Click to load google">
<input type="submit" id="m_-5702442034034898245rejectAcct" style="width:10em;height:5em" value="Click to load cnn">
1). The id is differnt, i dont know where the numbers came from.
2.) The onclick=\"location.href='"+googleLink+"';\" are not there as part of input button.

There are significant limits on the type of html you can use in an email message, and every mail client will impose different limits. Generally, these limits aren't documented, but this might help you get started.

try this:"<a href= '"+googleLink+"' > \n"+ " <input type=\"button\"style=\"width:10em;height:5em\" value=\"Click me to accept\" /> \n" + "</a>\n"+

Related

JSOUP - Select only some text from html

I am trying to select some text from the HTML using Jsoup in Android.
My HTML code looks like that:
<tr class="tip " data-original-title="">
<td>
!!! NOT That !!! </td>
<td>
A205 </td>
<td>
I want to get this </td>
<td>
And this </td>
<td>
!!! And not this !!! </td>
<td>
</td>
</tr>
How can I do that? Thank you so much!
For example:
package ru.java.study;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Main {
private static String htmlText =
"<tr class=\"tip \" data-original-title=\"\">" +
"<td>!!! NOT That !!!</td>" +
" <td>" +
" A205 </td>" +
" <td>" +
" I want to get this </td>" +
" <td>" +
" And this </td>" +
" <td>" +
" !!! And not this !!! </td>" +
" <td>" +
" </td>" +
" </tr>";
public static void main(String[] args) {
Document document = Jsoup.parse("<table>"+htmlText); //Add <table>
String first_TD = document.select("td").get(2).text();
String second_TD = document.select("td").get(3).text();;
System.out.println(first_TD);
System.out.println(second_TD);
}
}
You must be more specific in your selection. There should be id="..." or class="..." attributes in <table> tag to precisely identify the table that you need.
// Don't forget about <table> tag
String html = "<table>" +
"<tr class=\"tip \" data-original-title=\"\">" +
"<td>!!! NOT That !!!</td>" +
"<td>A205</td>" +
"<td>I want to get this</td>" +
"<td>And this</td>" +
"<td>!!! And not this !!!</td>" +
"<td></td>" +
"</tr>" +
"</table>";
Document doc = Jsoup.parseBodyFragment(html);
// You should use more specific selector.
// For example if table tag looks like this: <table id="myID">...</table>
// then selector should look like this "table#myID tr.tip > td"
Elements cells = doc.select("tr.tip > td");
String cell3content = cells.get(2).html(); // use .text() for content without html tags
String cell4content = cells.get(3).html();
System.out.println(cell3content);
System.out.println(cell4content);

Extract data from HTML page in between Tags using HTMLUNIT

I'm trying to extract data from web page using Html Unit. I've already achieved this by converting HtmlPage to text and then extracted data by using regular expression out of that HTML page. I've also achieved to extract data from Html tables using class attribute in Html.
I want to use HtmlUnit again fully for all extraction to learn for the same requirement I have done using regular expression. Am not able to get how can I extract data within tags in the form of key value pair.
Here is the sample Html data
<div class="top_red_bar">
<div id="site-breadcrumbs">
Home
|
Queues
|
Topics
|
Subscribers
|
Connections
|
Network
|
Scheduled
|
<a href="/admin/send.jsp"
title="Send">Send</a>
</div>
<div id="site-quicklinks"><P>
<a href="http://activemq.apache.org/support.html"
title="Get help and support using Apache ActiveMQ">Support</a></p>
</div>
</div>
<table border="0">
<tbody>
<tr>
<td valign="top" width="100%" style="overflow:hidden;">
<div class="body-content">
<h2>Welcome!</h2>
<p>
Welcome to the Apache ActiveMQ Console of <b>localhost</b> (ID:TOOLCONTROLPJX526-524666-65544585445-2:3)
</p>
<p>
You can find more information about Apache ActiveMQ on the Apache ActiveMQ Site
</p>
<h2>Broker</h2>
<table>
<tr>
<td>Name</td>
<td><b>localhost</b></td>
</tr>
<tr>
<td>Version</td>
<td><b>5.13.3</b></td>
</tr>
<tr>
<td>ID</td>
<td><b>ID:TOOLCONTROLPJX526-524666-65544585445-2:3</b></td>
</tr>
<tr>
<td>Uptime</td>
<td><b>17 days 13 hours</b></td>
</tr>
<tr>
<td>Store percent used</td>
<td><b>19</b></td>
</tr>
<tr>
<td>Memory percent used</td>
<td><b>0</b></td>
</tr>
<tr>
<td>Temp percent used</td>
<td><b>0</b></td>
</tr>
</table>
I want to extract data in between table tag.
Expected output
Name:localhost
Version:5.13.3
ID:ID:TOOLCONTROLPJX526-524666-65544585445-2:3
Uptime:7 days 13 hours
Store percent used:19
Memory percent used:0
Temp percent used:0
How it can be achieved? I want to know which methods to be used within HTLM unit to achieve this.
This are the steps i followed (not the only solution)
parse the string through parseHtml method with dummy url
get the second table by xpath
iterate with double nested loop (for and iterator -to append separator correctly-)
ExtractTableData:
import java.net.URL;
import com.gargoylesoftware.htmlunit.StringWebResponse;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HTMLParser;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow.CellIterator;
public class ExtractTableData {
public static void main(String[] args) throws Exception {
String html = "<div class=\"top_red_bar\">\n" + " <div id=\"site-breadcrumbs\">\n"
+ " Home\n"
+ " |\n"
+ " Queues\n"
+ " |\n"
+ " Topics\n"
+ " |\n"
+ " Subscribers\n"
+ " |\n"
+ " Connections\n"
+ " |\n"
+ " Network\n"
+ " |\n"
+ " Scheduled\n"
+ " |\n" + " <a href=\"/admin/send.jsp\"\n"
+ " title=\"Send\">Send</a>\n" + " </div>\n"
+ " <div id=\"site-quicklinks\"><P>\n"
+ " <a href=\"http://activemq.apache.org/support.html\"\n"
+ " title=\"Get help and support using Apache ActiveMQ\">Support</a></p>\n"
+ " </div>\n" + " </div>\n" + "\n"
+ " <table border=\"0\">\n" + " <tbody>\n"
+ " <tr>\n"
+ " <td valign=\"top\" width=\"100%\" style=\"overflow:hidden;\">\n"
+ " <div class=\"body-content\">\n" + "\n" + "\n"
+ "<h2>Welcome!</h2>\n" + "\n" + "<p>\n"
+ "Welcome to the Apache ActiveMQ Console of <b>localhost</b> (ID:TOOLCONTROLPJX526-524666-65544585445-2:3)\n"
+ "</p>\n" + "\n" + "<p>\n"
+ "You can find more information about Apache ActiveMQ on the Apache ActiveMQ Site\n"
+ "</p>\n" + "\n" + "<h2>Broker</h2>\n" + "\n" + "\n" + "<table>\n" + " <tr>\n"
+ " <td>Name</td>\n" + " <td><b>localhost</b></td>\n" + " </tr>\n" + " <tr>\n"
+ " <td>Version</td>\n" + " <td><b>5.13.3</b></td>\n" + " </tr>\n" + " <tr>\n"
+ " <td>ID</td>\n" + " <td><b>ID:TOOLCONTROLPJX526-524666-65544585445-2:3</b></td>\n"
+ " </tr>\n" + " <tr>\n" + " <td>Uptime</td>\n"
+ " <td><b>17 days 13 hours</b></td>\n" + " </tr>\n" + " <tr>\n"
+ " <td>Store percent used</td>\n" + " <td><b>19</b></td>\n" + " </tr>\n"
+ " <tr>\n" + " <td>Memory percent used</td>\n" + " <td><b>0</b></td>\n"
+ " </tr>\n" + " <tr>\n" + " <td>Temp percent used</td>\n" + " <td><b>0</b></td>\n"
+ " </tr>\n" + "</table>";
WebClient webClient = new WebClient();
HtmlPage page = HTMLParser.parseHtml(new StringWebResponse(html, new URL("http://dummy.url.for.parsing.com/")),
webClient.getCurrentWindow());
final HtmlTable table = (HtmlTable) page.getByXPath("//table").get(1);
for (final HtmlTableRow row : table.getRows()) {
CellIterator cellIterator = row.getCellIterator();
if (cellIterator.hasNext()) {
System.out.print(cellIterator.next().asText());
while (cellIterator.hasNext()) {
System.out.print(":" + cellIterator.next().asText());
}
}
System.out.println();
}
}
}
Output:
Name:localhost
Version:5.13.3
ID:ID:TOOLCONTROLPJX526-524666-65544585445-2:3
Uptime:17 days 13 hours
Store percent used:19
Memory percent used:0
Temp percent used:0

Using java, json, ajax and html to display image/name pairs in a row - currently displays in a column

I am trying to display some images with their names below them using json. I want the image/name pairs to be next to each other. At the moment they are one under the other.
HTML:
<form data-toggle="validator" role="form" id="selectForm">
<div class="row">
<div class="col-md-12">
<div class="form-select">
<!-- Place for Captcha image -->
<div class="input-group" id="img-container">
</div>
</div>
</div>
</div> <!-- /row -->
</form>
ajax:
.done(function(responseJson1a) {
dataType: "json";
//Remove existing images
$("#img-container").find("img").remove();
// JSON response to populate the image
$(responseJson1a).appendTo($("#img-container"));
for (i = 0; i < responseJson1a.length; i++) {
$(responseJson1a[i]).appendTo($("#img-container"));
}
});
java:
String json = null;
int i = 0;
for (final YouthMember youthMember : youthMembers) {
String image = youthMember.getPhotograph();
String name = youthMember.getFirstname();
if (i == 0){
json = "<div class='input-group'> <img src=" + image + " width='60' alt='captcha image' > " +
"<h3 class='form-line-heading'>" + name + "</h3> </div>";
i++;
}else{
json = json + ("<img src=" + image + " width='60' alt='captcha image' >" +
"<h3 class='form-line-heading'>" + name + "</h3> </div>");
}
}
I have tried placing the "" at the end encapsulating all the images; however, that does not work (image/string pairs are still in a single column.
When I remove the names "" + name + "" and the opening "" the images are one after the other in a row until the row is filled and then start a new row. This is what I want except with the names under each image.
Changed to (including style='display:inline;') - now images and names are all inline. Want name under image:
for (final YouthMember youthMember : youthMembers) {
String image = youthMember.getPhotograph();
String name = youthMember.getFirstname();
if (i == 0){
json = "<div class='input-group'> <img src=" + image + " width='60' alt='captcha image' > " +
"<h4 class='form-line-heading' style='display:inline;'>" + name + "</h4>";
i++;
}else{
json = json + ("<img src=" + image + " width='60' alt='captcha image' >" +
"<h4 class='form-line-heading' style='display:inline;'>" + name + "</h4>");
}
}
json = json + "</div>";
This change has mostly worked (issue with 19th image):
String json = null;
int i = 0;
for (final YouthMember youthMember : youthMembers) {
String image = youthMember.getPhotograph();
String name = youthMember.getFirstname();
if (i == 0){
json = "<div class='col-md-2'> <img src=" + image + " width='60' alt='captcha image' > " +
"<h4 class='form-line-heading'>" + name + "</h4></div>";
i++;
}else{
json = json + ("<div class='col-md-2'> <img src=" + image + " width='60' alt='captcha image' >" +
"<h4 class='form-line-heading'>" + name + "</h4></div>");
}
}
response.setContentType("image/jpeg");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
Add the following style for <h3>:
style="display:inline;"
Edit:
I guess you are using bootstrap, so better if you wrap both img and h4 into another div like so:
<div class="row">
<!-- your img and h4 go here -->
</div>
If not using bootstrap then <br/> should be enough after every <h4>
Or better to have <div class='input-group'></div> for each img&h4.

liferay : rendering <portlet:renderURL> in custom taglib

want to create a link to other pages of portlet in my own taglib with out.print() but in execution liferay can't render <portlet:renderURL> taglibs :
taglib code :
int counter = 0;
while (rs.next()) {
counter++;
int spcPk = Integer.parseInt(rs.getString("tCommodityGroupSpcPK"));
result = "<tr>" +
" <td id=\"group-"+counter+"\">" +
" <a href=\"<portlet:renderURL><portlet:param name='jspPage'" +
" value='subGroup.jsp?p1="+spcPk+"' /></portlet:renderURL>\" /> " +
" "+rs.getString("xa")+" " +
" </a> " +
" </td> " +
" <td id=\"group-"+counter+"\">"+rs.getString("xx")+"</td> " +
" <td id=\"group-"+counter+"\">"+rs.getString("aa")+"</td> " +
" <td id=\"group-"+counter+"\">"+rs.getString("Result")+"</td> " +
" </tr>";
out.print(result);
}
rs.close();
// out.println(result);
cstmt.close();
in execution of view.jsp and other tags has been rendered successfully but the liferay taglibs don't .. when i click on links i have somthing like this in my url !!
localhost:8081/%3Cportlet:renderURL%3E%3Cportlet:param%20name='jspPage'%20value='subGroup.jsp?p1=3'%20/%3E%3C/portlet:renderURL%3E
can anybody help me in rendering that taglibs in custom taglib class ?
Thats obvious, JSP Engine consider your tag as any other markup.
You have to use the API rather than Tag like mentioned below
PortletURL portletURL = renderResponse.createRenderURL();
portletURL.setParameter("jspPage", "subGroup.jsp?p1="+spcPk);
then inject portletURL.toString() into the markup
UPDATE:
You should have access to both renderRequest and renderResponse implicitly if you define <portlet:defineObjects /> in your JSP.
The other way to get them from normal request object is
PortletResponse portletResponse = (PortletResponse)request.getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
You can refer to the source code of any of the existing Liferay UI Tags like asset-categories-navigation for more information.

Copy to clipboard script with breaks and more text

I am trying to make a fun script for work documentation. Here is what I have so far.
<script type="text/javascript">
function ClipBoard() {
window.clipboardData.setData('text',
document.getElementById('name').value +
document.getElementById('phone').value +
document.getElementById('serial').value +
document.getElementById('new').value +
document.getElementById('cuts').value +
document.getElementById('agts').value
);
}
</script>
<form id="form1">
Name: <input id="name" /><br />
Phone Number: <input id="phone" maxlength="10" /><br />
Serial Number: <input id="serial" maxlength="10" /><br />
New/Existing: <input id="new" /><br />
CU TS: <input id="cuts" /><br />
Agent TS: <input id="agts" /><br />
<input type="button" onclick="ClipBoard()" value="Copy"/>
<input type="reset" />`
Right now after I paste the inputs do not have "breaks" instead, the copied text copies in a line. Ex: namephoneserialnew etc.
I would like:
Name
Phone
Serial
New
Etc. with breaks.
If at all possible.
Also, when copying the inputs, is there a way to copy the text before the input.
Ex: Name: (with input), Phone Number: (with input) etc.
Any suggestions will be very helpful; this is just a basic script nothing serious. Thanks everybody!
Try to add '\r\n' after ctl value.
function ClipBoard() {
window.clipboardData.setData('text',
document.getElementById('name').value + '\r\n' +
document.getElementById('phone').value + '\r\n' +
document.getElementById('serial').value + '\r\n' +
document.getElementById('new').value + '\r\n' +
document.getElementById('cuts').value + '\r\n' +
document.getElementById('agts').value
);
}
JavaScript doesn't generate line breaks this way. You may try adding "<br>" in the code and that might bring up the line breaks.
function ClipBoard() {
window.clipboardData.setData('text',
document.getElementById('name').value + "<br>"
document.getElementById('phone').value + "<br>"
document.getElementById('serial').value + "<br>"
document.getElementById('new').value + "<br>"
document.getElementById('cuts').value + "<br>"
document.getElementById('agts').value
);
}
And if you want Labels before the values you can just put a string before them.
function ClipBoard() {
window.clipboardData.setData('text',
"Name: " + document.getElementById('name').value + "<br>"
"Phone: " + document.getElementById('phone').value + "<br>"
"Serial: " + document.getElementById('serial').value + "<br>"
"New: " + document.getElementById('new').value + "<br>"
"Cuts: " + document.getElementById('cuts').value + "<br>"
"Agts: " + document.getElementById('agts').value
);
}
BTW, you forgot the ending tag in the HTML.

Categories

Resources