Rendering a Play Framework 2 View - java

I'm just kind of confused as a whole when it comes to play framework. I've gotten most of everything figured out, but it seems like every day something new comes up.
Anyway, the thing of the day is this: my scala template code is showing up as plain text in my rendered document. Maybe its the wrong syntax, maybe its play 1.0 syntax, I dunno. The template looks like this:
#(model : models.Menu)
#main("Bearings") {
<div id="bearings_container">
<div id="menu">
<ul id="firstLevel">
#{list items:model.items,as:'menuItem'}
<li id="${menuItem.name}" class="firstLevel">${menuItem.name}</li>
<ul id="${menuItem.name}- submenu">
#{list items: menuItem.subMenu, as:'subMenuItem'}
<li id="${subMenuItem.name}" class="secondLevel">${subMenuItem.name}</li>
<ul id="${subMenuItem.name}- submenu">
#{list items: subMenuItem.subMenu, as:'subSubMenuItem'}
<li id="${subSubMenuItem.name}" class="thirdlevel">${subSubMenuItem.name}</li>
#{/li}
</ul>
#{/li}
</ul>
#{/li}
</ul>
</div>
</div>
}
I'm sure it's something simple. Any ideas?
Edit: here's the controller action:
public static Result bearings()
{
Menu menu = BuildMenu();
return ok(views.html.bearings.render(menu));
}
And the result is what a straight HTML result of the code above would look like.

That's Play 1.0 template syntax. The template syntax in Play 2 is completely different.
To be fair the play documentation does make this confusion easy. I've found myself reading a Play 1.0 documentation page that I get to from a Google search for a while before I realize that the URL says 1.0.

If the output looks like straight HTML I'd try checking the template referred by #main. Please provide the main template source.

Related

Having trouble locating text within span tags

I'm specifically searching for "Leg 0" and the other text below it like BOS and MAD, etc. I've played around with xpath and I seem to find text above this section and text below it but I can never find the "Leg 0" text and that below no matter what I do. I've been trying all morning to figure this out and no dice.
Here is the HTML.
<div>
<span class="debug-section ng-binding">Leg 0</span>
<span class="debug-value ng-binding">BOS</span>
-
<span class="debug-value ng-binding">MAD</span>
<span class="debug-value ng-binding">BACAT</span>
/
<span class="debug-value ng-binding">BA</span>
<span class="debug-value ng-scope" ng-if="!leg.excludeDynamic">Dynamic</span>
</div>
Any help is appreciated here, I am beyond frustrated.
First of all, provide some info about your code and exception you get as #Anderrson suggested.
And second, you can't find element based on text within span tags (innerHTML) using cssSelector, you should use xpath in that case. Or some javascript or jquery, but that would probably be overkill in this case.
The answer my friend is blowing in the wind. Brought up Firefox. Enabled the Firebug plug-in. Click the inspection tool. It highlights the HTML line you click on on the web page. Right click. Choose the Xpath copy to clipboard option.
Walla.

Deal with whitespaces in CSS class names with Jsoup

I want to select some supermarket product info from this page:
http://www.angeloni.com.br/super/index?grupo=15022
For that I should select <ul> tags with class "lstProd ":
If the class name were "lstProd" it would be easy, but the problem is the whitespace at the end of name. I couldn't make Jsoup deal with it.
I tried the code below and other ways but it always get an empty list.
org.jsoup.nodes.Document document = Jsoup.connect("http://www.angeloni.com.br/super/index?grupo=15022").get();
org.jsoup.select.Elements list = doc.select("ul.lstProd ");
the code snippet from html page that I want to get:
<ul class="lstProd ">
<li>
<span class="cod">CÓD. 1341372</span>
<span class="lnkImgProd">
<a href="/super/produto?grupo=15022&idProduto=1341372">
<img src="http://assets.angeloni.com.br/files/images/7/1B/C6/1341372_1_V.jpg" width="120" height="120"
alt="Creme Dental SORRISO Super Refrescante Tubo 90g">
</a>
</span>
<div class="RgtDetProd">
<div class="boxInfoProd">
<span class="descr">
<a href="/super/produto?grupo=15022&idProduto=1341372">Creme Dental SORRISO Super Refrescante
Tubo 90g</a>
</span>
<ul class="lstProdFlags after">
</ul>
</div>
...
I think you are facing two completely separate problems:
Jsoup does not load the site you think it loads. The website you specified renders its contents via JavaScript and loads some content after initial page loading through AJAX. JSoup can't deal with this. You either need to investigative the AJAX calls and get them directly with Jsoup, or you use something like selenium webdriver to get the page in a real browser which will render everything as you expect it.
CSS class names can't contain spaces for practical purposes 1. In HTML spaces are used as separator between class names. Hence <ul class="lstProd "> is the same as <ul class="lstProd">. In CSS selectors however a class name is specified by .className, i.e. dot followed by the class name. You can concatinate several classes like this: element.select(".className1.className2")
1 Technically you can put spaces in CSS classes, but you need to escape them with '\ '. See https://mathiasbynens.be/notes/css-escapes or Which characters are valid in CSS class names/selectors?
edit: be more precise about CSS class names
CSS class names CAN contain whitespaces.
And <ul class="lstProd "> is NOT same as <ul class="lstProd">.
And I can see that you have multiple <ul> with same class name.
The better way to inspect or traverse such element is by nth-child
So to find your required selector you can use #abaProd > ul:nth-child(4)
For more details about nth-child

How to check for second element with class name and click it if it exists in Selenium Java

So I am writing automation tests using selenium and I am having a lot of trouble selecting the second element in a list of divs with the same class names
Boolean isExists2Accounts = driver.findElements(By.xpath("(//div[contains(#class, 'item-name')])[2]")).size() < 0;
if(isExists2Accounts)
{
//Finds second div element that has classname of item-name and clicks on it
driver.findElement(By.xpath("(//div[contains(#class, 'item-name')])[2]")).click();
}
else
{
driver.get("javascript:alert('There isn't a second account or you don't know how to select it!');");
Thread.sleep(5000);
org.testng.Assert.fail("transferTest6() Failed due to There isn't a second account or you don't know how to select it!");
}
HTML structure looks like this:
<div class="item-list">
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area</div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
</div>
Not really sure what I am doing wrong here, I looked at the html and there are 5 divs with the specified class name. Very new to selenium in general, using eclipse/junit/webdriver.
I have seen several questions similiar to this, and trying solutions people have posted have not worked. I have seen some suggestions to use .get(2) and I will try and implement that in the mean time.
Any help you could give would be good.
get(2) is THIRD element, not the second, as the countage begins from 0.
So:
driver.findElements(By.cssSelector(".item-name")).get(1).click();
OR depending on where is yr clickable
driver.findElements(By.cssSelector(".item-name div:not(.button-wrap)")).get(1).click();
Hey all the answer that was given by Stanjer works, I tested it with different markup, the developer that built the system I am testing through a random mousedown event (not click) for the html I am trying to interact with which was causing the problem.
So final solution with problem if it was a click event would be:
driver.findElements(By.cssSelector(".item-name")).get(1).click();
Just like he said.
However in this case I am instead going to send Javascript to the console to work with functions that have already been created by the developer.

Thymeleaf: Parameters In URLs Not Being Replaced

I'm trying to learn Spring MVC & Thymeleaf. I've got the following HTML portion that prints out a link & a button:
<ul th:each="item : ${typesMap}">
<li>
Linky
<button type="button" th:text="${item.value}">Button Text</button>
</li>
</ul>
In the two examples, the parameters in the link are never replaced. I always end up with something along the lines of roomdetails/${item.key}/${item.value} in the HTML. The button works fine though & will show with the text that is found in ${item.value} for each iteration of the loop.
Does anyone know why I can't get URLs in the format I want? From what I can see, I'm doing what the documentation is telling me to.
This should work:
<a href="roomdetails.html" th:href="#{'/roomdetails/' + ${item.key} + '/' + ${item.value}}">
Answer:
<a href="roomdetails.html" th:href="#{'/roomdetails/{paramsKey}/{paramsValue}'(paramsKey=${item.key}, paramsValue=${item.value})}">
I hope that this might solve your issue.

Struts2 tags inserting <br>

I am currently upgrading from Struts 2.0.11.1 to Struts 2.3.7, but I'm having a problem with the output of Struts tags (like <s:radio>and <s:textarea>. Before, the output of these tags were two <div>'s, one containing the label, and one containing the radiobuttons/textarea. But after I upgraded there is a <br>between the two <div>'s.
Before:
<div id="wwlbl_something_someString" class="wwlbl">
<label for="something_someString" class="desc">Some label</label>
</div>
<div id="wwctrl_something_someString" class="wwctrl">
<!--radiobuttons/textarea -->
</div>
After
<div id="wwlbl_something_someString" class="wwlbl">
<label for="something_someString" class="desc">Some label</label>
</div>
<!-- I don't want this.. -->
<br>
<!-- -->
<div id="wwctrl_something_someString" class="wwctrl">
<!--radiobuttons/textarea -->
</div>
The jsp:
<li class="wwgrp flowClear">
<h3 class="header"><s:text name="something.header"/></h3>
<ol class="nobullets">
<s:radio list="yesNoList"
label="%{getText('something.text.label')}"
name="something.someString"
value="something.some"
disabled="%{readOnly}"/>
</ol>
</li>
#Comment: There are a lot of things in this project that many have never seen before (not in a good way) ;)
Does anyone know if this is something that has changed in Struts, and if so, how to avoid creating the <br>'s?
I actually stumbled across the answer. As an answer to the last comment struts.ui.theme is set to css_xhtml.
http://depressedprogrammer.wordpress.com/2007/04/11/struts-2-form-control-templates/
One quick note, the xhtml_css theme also generates a very worthless <br/> tag between a label and the control which can throw of your entire look, do get rid of this you’ll have to do a .wwgrp br { display: none; } in your CSS stylesheeet.
So I assume doing this will solve the problem, it is something added by struts theme, probably changed when I updated Struts version.
Pointers on how this is usually set up/should be set up is welcomed, as I do not know anything about this.. :)

Categories