Local conditional variable thymeleaf - java

I'm trying to rewrite without duplication something like this:
<span th:if=${userRole == 'ADMIN'} style="display:inline;"> // display property for logic only
<div> ... </div>
</span>
<span th:unless=${userRole == 'ADMIN'} style="display:none;">
<div> ... </div>
</span>
In the 2nd case we have the same code, just hidden. This way I have duplicated code. It would be better to have a variable change it's value to either "none" or visible" and use that on a single tag.
How can I do it without duplication, implementing the following logic:
$variable = "display: none;"
th:if=${userRole == 'ADMIN'} $variable="display: visible"
<span style="display: $variable;">
<div> ... </div>
</span>

You can use a ternary operator (? :) to do it in one line without duplication.
th:style="${userRole} == 'ADMIN' ? 'display:inline' : 'display:none'"
I hope this is what you are looking for.

Related

Thymeleaf conditional classappend not working

My question is related to this.
I have the following:
<div class="container">
<div class="wrapper">
<div class="arrow-steps clearfix">
<div th:each="caseStep : ${getAppCaseStepList}"
th:class="arbitrary"
th:classappend="${caseStep.casestepname == '${appCaseCurrentStep}'} ? 'step current' : 'step'"
th:text="${caseStep.casestepname}">
</div>
</div>
</div>
</div>
where ${appCaseCurrentStep} is a model attribute (a String) which I am sending through GET in my controller method.
But somehow I am not able to set "step current" class to the element for this condition:
${caseStep.casestepname == '${appCaseCurrentStep}'
I am trying to set/append "step current" where my condition is true (String comparison) . What am I missing here or doing wrong?
Found it here. !
The cause: There should NOT be an additional { inside the expression ${...}.
Solution: The following expression did the trick for me:
<div th:each="caseStep : ${getAppCaseStepList}"
th:class="asdfstep"
th:classappend="${caseStep.casestepname == appCaseCurrentStep} ? 'step current' : 'step'"
th:text="${caseStep.casestepname}"></div>

Get id where style equals x - Java - Selenium WebDriver

When I open a page, there is code that looks like the following:
<div id="policySetup_content">
<div id="bCS_insureds_contentWrap" style="display: none;">
<div id="bCS_policy_contentWrap" style="display: block;">
<div id="bCS_risks_contentWrap" style="display: none;">
<div id="bCS_rating_contentWrap" style="display: none;">
<div id="bCS_billing_contentWrap" style="display: none;">
<div id="bCS_attachments_contentWrap" style="display: none;">
<div id="bCS_submit_contentWrap" style="display: none;">
</div>
How would I go about getting the #id of whichever one is set to (style="display: block;) inside the #id policySetup_content?
The reason for this is so I can know which page I'm on (because it can be any one of them for various reasons). I need to know the page in order to know which Wrap id to use when working with elements.
Judging by this previou SO question you should be able to use the CSS Selector (div[style*="display:block"]), something along the lines of the below (untested).
String id = driver.findElement(By.cssSelector("div[style*=\"display:block\"]").getAttribute("id");
Because Selenium will not interact with elements that are not visible, you should be able to pull all the DIVs under the parent DIV and only get the one that is not hidden. I've never tried this approach before but I think it will work...
String id = driver.findElement(By.cssSelector("#policySetup_content > div[id]")).getAttribute("id");
BTW, if you aren't familiar with CSS Selectors this reads find an element with ID (#) policySetup_content that has an immediate child (>) DIV that has an ID. This may need to be tweaked depending on the real HTML that you are dealing with. If it doesn't work, let me know and I can try to help tweak it.
CSS Selector reference

can't reference iteration variable in Thymeleaf

I try to iterate a list of items using th:each,
<div class="row" th:each="item : *{items}">
<input type="text" th:field="item.name"/>
</div>
it works if I access the iteration variable using th:text, but throws
java.lang.IllegalStateException: Neither BindingResult nor plain
target object for bean name 'item' available as request attribute
when I use th:field to access it, where did I do wrong?
Something like this could work:
<div class="row" th:each="item, stat : *{items}">
<input type="text" th:field="*{items[__${stat.index}__].name}"/>
</div>
Take a peek here for more info: http://forum.thymeleaf.org/I-have-problem-in-binding-the-list-of-objects-contained-inside-a-object-on-the-form-using-thymeleaf-td3525038.html
th:field is broken in several ways, this is one of them.

How to retrieve atomic values inside tags in HTMLUnit

I am new to HtmlUnit and I don't know how to get the text inside the [...]
A part of my html file:
<ul ......somethin....>
<li data-role="list-divider" role="heading" style="font-size:16px;" class="ui-bar-f">
INFORMATION_LINE_1
</li>
<li data-theme="d" class="ui-li ui-btn-icon-right ui-btn-up-d ui-odd-match-column ">
<div class="ui-btn-inner ui-li">
<div class="">
<div class="ui-btn-text">
<a href="/x/cxntay/13113/ndzvsssl/g1" class=" ui-link-inherit ui-link-hover">
<h3 class="ui-li-heading">
<span class="xheader">INFORMATION_LINE_2</span>
<span class="label live">INFORMATION_LINE_3</span>
</h3>
<div class="ui-live-scores">
<span class="team1-scores">
<span class="ui-team-name">INFORMATION_LINE_4</span>
<span style="font-weight:bold">INFORMATION_LINE_5</span>
</span>
<span>INFORMATION_LINE_6</span>
</div>
</a>
</div>
</div>
</div>
</li>
</ul>
Now, I want to retrieve "INFORMATION_LINE_X"(1,2...6) in between these tags..
This is what I tried:
List<HtmlUnorderedList> ls = (List<HtmlUnorderedList>) page.getByXPath("/ul");
List<DomNode> dls = ls.get(0).getChildNodes();
System.out.println(dls.get(0).getFirstByXPath("//li[#data-role='list-divider']/text()");
I just tried to get INFORMATION_LINE_1
But it printed null. I need to get all the INFORMATION_LINES.
It is better to use just XPath rather than mixing it with HTMLUnit methods. Something like this should work to get you the first information line:
HtmlElement e = page.getFirstByXPath("//li[#data-role='list-divider']");
System.out.println(e.asText());
In order to fetch the other information lines you should follow the same approach but changing the XPath string.
Bear in mind you should always debug the page by taking a look at the code by printing the output of page.asXml(). If you use a real browser you are not actually seeing exactly the same as HTMLUnit is seeing. You can stumble with differences particularly if the page executes JavaScript.

Conditionally displaying a block of HTML based on presence of resource bundle key

I'm using JSF, and I have to load a bundle called Extra.
<f:loadBundle basename="com.ni.lib.extra.delivery.ExtraBundle" var="extra" />
Inside that extra variable, there's a value called downtime_notice. Now, if that value is NOT empty, I have to show a css segment with the text contained within the downtime_notice value. Something like this (of course this doesn't work):
if(extra.donwtime_notice!=''){
<div class="pnx-msg pnx-msg-warning clearfix">
<i class="pnx-msg-icon pnx-icon-msg-warning"/>
<span class="pnx-msg-content"><h:outputText value="#{extra.downtime_notice}" escape="false"/></span>
</div>
</br>
}
I can use javascript, just in case.
Any ideas? Thanks!
You can use <ui:fragment> or <h:panelGroup> to conditionally render content. You can use the empty keyword in EL to check if a variable is not null or empty. So, all with all this should do:
<ui:fragment rendered="#{not empty extra.donwtime_notice}">
<div class="pnx-msg pnx-msg-warning clearfix">
<i class="pnx-msg-icon pnx-icon-msg-warning"/>
<span class="pnx-msg-content"><h:outputText value="#{extra.downtime_notice}" escape="false"/></span>
</div>
</ui:fragment>
Or, using <h:panelGroup layout="block"> which renders a <div> already:
<h:panelGroup layout="block" styleClass="pnx-msg pnx-msg-warning clearfix" rendered="#{not empty extra.donwtime_notice}">
<i class="pnx-msg-icon pnx-icon-msg-warning"/>
<span class="pnx-msg-content"><h:outputText value="#{extra.downtime_notice}" escape="false"/></span>
</h:panelGroup>
Note that some may opt to use <f:verbatim> for the job, but this tag is deprecated since JSF2.
See also:
Conditionally displaying JSF components
Alternative to ui:fragment in JSF
Unrelated to the concrete problem, the </br> tag is invalid HTML, so I omitted it form the examples.

Categories