I have an problem which in turn is causing a lot of headaches. I need to dynamically create buttons/images which link to JSF actionListener. Here is the code:
HTML:
<h:form>
<div class="carousel-container">
<div id="carousel">
<h:outputText value="#{courseBean.course}" escape="false"/>
</div>
</div>
</h:form>
what courseBean.course gets is the Overriden toString which returns the following:
#Override
public String toString() {
return "<div class=\"carousel-feature\"> "
+ "<h:commandLink id=\"" + courseID + "\" actionListener=\"#{courseBean.getCourseSelected}\">"
+ "<img class=\"carousel-image\" src=\"Images/testButton.jpg\"/>"
+ "<span style=\"display:bloack; position:absolute; top:20px; bottom:20px; left:0; right:0; "
+ "background:white; background:rgba(255, 255, 255, 0.25);\">" + courseName + "</span>"
+ "</h:commandLink> "
+ "<div class=\"carousel-caption\"> "
+ "</div>"
+ "</div>";
}//end method toString
The HTML is being rendered fine and image is being displayed in the carousel however when it is clicked actionListener is not being called which is the issue here.
edit: the actionListener only prints the courseID to the console nothing major.
Thank you for taking your time :)
That approach is wrong, if you do "view source" you will see the <h:commandLink in the source of your page (because it wont be processed by JSF life cycle at all) , while if you had a <h:commandLink in your xhtml page the generated html source will contain a <a href....> element
You better rethink of your original goal and ask a question on how to achieve it...
Related
I've following string which is HTML -
<html>
<head>
<title>Repository</title>
</head>
<body>
<h2>Subversion</h2>
<ul>
<li>
..
</li>
<li>
branch_A
</li>
<li>
branch_B
</li>
</ul>
</body>
</html>
Out of this I want to get labels of li tag which are branch_A, branch_B
Count of li's can vary. I want to get all of them. Can you please help how I can parse this String and get those values?
NOTE I could have used jsoup library to achieve same, but considering our project restriction, I cannot use it.
You can use an HTML parser for this. In the code below jsoup (https://www.baeldung.com/java-with-jsoup) is used and its quick and easy.
Document doc = Jsoup.connect(fix url here).get();
doc.select(tag you want).forEach(System.out::println);
Other tools are discussed here: https://tomassetti.me/parsing-html/
Using Java 8 streams:
String html = "<html>\n" +
" <head>\n" +
" <title>Repository</title>\n" +
" </head>\n" +
" <body>\n" +
" <h2>Subversion</h2>\n" +
" <ul>\n" +
" <li>\n" +
" ..\n" +
" </li>\n" +
" <li>\n" +
" branch_A\n" +
" </li>\n" +
" <li>\n" +
" branch_B\n" +
" </li>\n" +
" </ul>\n" +
" </body>\n" +
"</html>";
html.lines().filter(line -> line.contains("<a href")).forEach(System.out::println);
Output:
..
branch_A
branch_B
Keep in mind you can run streams in parallel if you have huge HTML file.
Also you can strip HTML tags using map:
html.lines().filter(line -> line.contains("<a href")).map(line -> line.replaceAll("<[^>]*>","")).forEach(System.out::println);
Output:
branch_A
..
branch_B
Here, the div is a pop up when a text in the html is clicked. 'db' is an object created in another part of the code, which holds various arrays. Here, the table consists of elements from one such array. The elements of the table, when clicked, displays another pop-up (This part is working fine).
The issue is, the table part of the div is just displayed without me clicking any text. Please help
<div class="modal-content animate" >
<div class="imgcontainer">
<span onclick="document.getElementById('Card').style.display='none'" class="close" title="Close">×</span>
<img src="doctor.jpg" alt="Avatar" class="avatar">
</div>
<% int i;
if(db.cardNotPunched==0){
out.println(" You have punched your card everyday so far this month ");
} else {
out.println(" <table table-layout='fixed' border = '1' align='center' width='600'>\n" +
" <tr>\n" +
" <th> Day </th>\n" +
" </tr>");
for (i = 0; db.cardNotPunchedDays[i]!=0; i++) {
out.println(" <tr>\n" +
" <td style = 'text-align:center' height=40 > <div onclick= document.getElementById('Reason"+ db.cardNotPunchedDays[i]+ "').style.display='block' style= 'width:auto'; -webkit-box-align:'center' ><font style = 'color: black' > " + db.date[db.cardNotPunchedDays[i]] + " </font></div> </td>\n" +
" </tr>\n");
}
}
%></div>
</div>
I am trying to parse a HTML file using Jsoup. There are certain text in the HTML that doesn't come under an tags.
<li class="inactive">
<span class="status label">inactive</span>
<a href="/officers/144662696" class="officer inactive" title="more info on MILLTOWN CORPORATE SERVICES">
MILLTOWN CORPORATE SERVICES
</a>
member,
<span class="status label">inactive</span>
<a href="/companies/us_wv/193180" class="company inactive revoked_(failure_to_file_annual_report)" title="More Free And Open Company Data On EASTBRIDGE L.L.C. (West Virginia (US), 193180)">
EASTBRIDGE L.L.C.
</a>
(West Virginia (US),
<span class="start_date">25 May 2000</span>-<span class="end_date"> 1 Aug 2002</span>)
</li>
I am able to read all the content in a tag but I am trying to get the values (West Virginia US) and member.
Is there a way to get the values outside the classes and inside a li tag.
You are probably looking for something like Element#ownText.
This only gets the text of the current element and not a combined text of all children.
Element listItem = doc.select("li.inactive").first();
System.out.println(listItem.ownText()); // prints "member, (West Virginia (US), -)"
You can also use the previous tags to get the text nodes which are not embedded in any tags. If i get it right, you want to get each text node after each a tag. Try something like :
String html = "<li class=\"inactive\"> \n"
+ " <span class=\"status label\">inactive</span> \n"
+ " <a href=\"/officers/144662696\" class=\"officer inactive\" title=\"more info on MILLTOWN CORPORATE SERVICES\">\n"
+ " MILLTOWN CORPORATE SERVICES\n"
+ " </a>\n"
+ " member, \n"
+ " <span class=\"status label\">inactive</span> \n"
+ " <a href=\"/companies/us_wv/193180\" class=\"company inactive revoked_(failure_to_file_annual_report)\" title=\"More Free And Open Company Data On EASTBRIDGE L.L.C. (West Virginia (US), 193180)\">\n"
+ " EASTBRIDGE L.L.C.\n"
+ " </a> \n"
+ " (West Virginia (US), \n"
+ " <span class=\"start_date\">25 May 2000</span>-<span class=\"end_date\"> 1 Aug 2002</span>) \n"
+ "</li>";
Document doc = Jsoup.parse(html);
Elements links = doc.select("a");
for(Element e : links){
System.out.println(e.nextSibling().toString());
}
I'm trying to get data from html in order from a web. Html code looks like:
<div class="text">
First Text
<br>
<br>
<div style="margin:20px; margin-top:5px; ">
<table cellpadding="5">
<tbody><tr>
<td class="alt2">
<div>
Written by <b>excedent</b>
</div>
<div style="font-style:italic">quote message</div>
</td>
</tr>
</tbody></table>
</div>Second Text<br>
<br>
<img class="img" src="https://developer.android.com/_static/images/android/touchicon-180.png"><br>
<br>
Third Text
</div>
What I want to do is create an Android layout scraping html, but I need to preserve the order of the elements. In this case:
TextView => First Text
TextView => Quote Message
TextView => Second Text
ImageView => img
TextView => Third Text
The problem comes when I try to get html values in order, using JSoup I get a String with "First Text Second Text Third Text" with Element.ownText, an then img at the end, resulting:
TextView => First Text Second Text Third Text
TextView => Quote Message
ImageView => img
What can I do to get that data in order?
Thanks in advance
You can parse the html into a list of html nodes. The list of nodes will preserve the DOM order and give what you want.
Check the parseFragment method :
This method will give you a list of nodes.
Try this.
String html = ""
+ "<div class=\"text\">"
+ " First Text"
+ " <br>"
+ " <br>"
+ " <div style=\"margin:20px; margin-top:5px; \">"
+ " <table cellpadding=\"5\">"
+ " <tbody><tr>"
+ " <td class=\"alt2\">"
+ " <div>"
+ " Written by <b>excedent</b>"
+ " </div>"
+ " <div style=\"font-style:italic\">quote message</div>"
+ " </td>"
+ " </tr></tbody>"
+ " </table>"
+ " </div>Second Text<br>"
+ " <br>"
+ " <img class=\"img\" src=\"https://developer.android.com/_static/images/android/touchicon-180.png\"><br>"
+ " <br>"
+ " Third Text"
+ " </div>";
Document doc = Jsoup.parse(html);
List<String> rootTexts = doc.select("div.text").first().textNodes().stream()
.map(node -> node.text().trim())
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
System.out.println(rootTexts);
OUTPUT:
[First Text, Second Text, Third Text]
This answer is a little late, but the correct way to do what you want to do is this. For your outermost <div>, instead of getting the child elements using Element.children(), you'll want to use Element.childNodes() instead.
Element.children() only returns child Elements, in which text is not included.
Element.childNodes() returns all child nodes, which includes TextNodes and Elements.
This solution works for me.
I have a gallery of images, and when I hover the mouse over any of the images, the image pulls back revealing some text.
When I have the following HTML code (at bottom of post) inside the HTML document, everything works fine.
However, when I put the exact same HTML code inside a Java servlet and have it returned to the page, everything looks normal, but the image pullback doesn't work anymore.
Any idea why that would occur? Perhaps I need to do some kind of refresh to make it work properly?
relevant code for one of the items in the gallery:
<li>
<div class="header"><p>Product 1 Shirt</p></div>
<div class="gallery_item">
<img src="gallery/thumb/gallery_01.jpg" width="214" height="194" class="cover" alt="" />
<p>Highlight 1</p>
<p>Highlight 2</p>
<p>Highlight 3</p>
More Info
Enlarge
</div>
<div class="p2"><p>Price: $10</p></div>
<div class="p2"><p>In Stock: Yes</p></div>
</li>
As requested: the servlet:
public void service(HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException
{
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String requestType = request.getParameter("type");
String result;
if(requestType.equals("getproductlist"))
{
Products products = Products.getProductsInstance();
String keywords = request.getParameter(("keywords"));
String organization = request.getParameter(("organization"));
String price = request.getParameter(("price"));
String sort = request.getParameter(("sort"));
result = products.getProducts(keywords, organization, price, sort);
//this next lines of html are actually what is returned from products.getProducts. I'm just putting it here for clarity. All the variables (name, h1, etc) are okay.
result += "<li>"
+ "<div class=\"header\"><p>"+ name +"</p></div>"
+ "<div class=\"gallery_item\">"
+ "<img src=\"gallery/thumb/gallery_01.jpg\" width=\"214\" height=\"194\" class=\"cover\" alt=\"\" />"
+ "<p>"+ h1 +"</p>"
+ "<p>"+ h2 +"</p>"
+ "<p>"+ h3 +"</p>"
+ "More Info"
+ "Enlarge "
+ ""
+ "</div>"
+ "<div class=\"p2\"><p>Price: "+ itemPrice +"</p></div>"
+ "<div class=\"p2\"><p>In Stock: "+ inStock +"</p></div> "
+ "</li>";
out.println(result);
out.close();
}