How to display data from object in zk (zul file)? - java

I am using zk framework for my internship. I currently have a controller that goes and fetches a special link from an api and I have to create an IFrame in a zul file and bind this link into this iframe.
I do not know how to dynamically bind data either from an object, a modal or a properties file.
<div id="iframe-div" height="100%" style="background: #ccc;">
<iframe id="iframe" width="100%" height="100%" src="https://thisIsTheLink.com"/>
</div>
is there something similar as src="{mylink}" in zk as they do in other front end frameworks? Is it possible to dynamically bind data in zk framework?

Yes it is possible. I recommend using MVVM binding.
zul file:
<window viewModel="#id('vm') #init('com.example.IndexVM')">
<div id="iframe-div" height="100%" style="background: #ccc;">
<iframe id="iframe" width="100%" height="100%" src="#load(vm.includeSrc)" />
</div>
</window>
view model:
public class IndexVM() {
public String getIncludeSrc() {
return "https://thisIsTheLink.com";
}
}
You can even pass parameters to your included file.

Related

Zkoss using template in zhtml

Good day. Here is my zhtml
<x:html xmlns="http://www.zkoss.org/2005/zul"
xmlns:x="xhtml"
src="/components/public-page-template.html">
<div viewModel="#id('vm') #init('TestViewModel')">
<div children="#init(vm.testList)">
<template name="children">
<checkbox label="#load(each)"/>
</template>
</div>
</div>
</x:html>
And view model
public class TestViewModel {
public List<String> getTestList(){
return List.of("one", "two", "three");
}
}
I expect to see 3 checkboxes in rendered html, but I get 3 spans
<div id="mXBQ2" class="z-div">
<div id="mXBQ3" class="z-div">
<span id="mXBQ4" class="z-label">one</span>
<span id="mXBQ5" class="z-label">two</span>
<span id="mXBQ6" class="z-label">three</span>
</div>
</div>
No matter what I put inside template I get 3 spans, template is completely ignored. But if change zhtml to zul
<?xml version="1.0" encoding="UTF-8"?>
<?variable-resolver class="org.zkoss.spring.DelegatingVariableResolver" ?>
<div xmlns="http://www.zkoss.org/2005/zul" viewModel="#id('vm') #init('TestViewModel')">
<div children="#load(vm.testList)">
<template name="children">
<checkbox label="#load(each)"/>
</template>
</div>
</div>
then template is working as expected and I see 3 checkboxes in result html.
How can I make template work in my zhtml?
I guess, what you're missing is the fact that the <template> element is not a zul component (instead those are special ZUML elements which control how components are created).
When running your example code I get an exception ... which indicates what's wrong (more or less clearly).
org.zkoss.zk.ui.metainfo.DefinitionNotFoundException:
Component definition not found: template in [LanguageDefinition: xul/html]
Instead you have to declare and use the according namespace "zk" for these special elements (.zhtml files have different parsing rules than .zul files)
<x:html xmlns="http://www.zkoss.org/2005/zul"
xmlns:x="xhtml"
xmlns:zk="zk"
src="/components/public-page-template.html">
...
<zk:template>
...
</zk:template>
Besides that I am not sure what the src attribute at the root element x:html does. In my tests it was plainly rendered to the DOM element, I assume you have your own custom processing handling that.

Thymeleaf Master Layout Content Dynamics issue

I have got some issue as I mentioned in the title.
This is my master page :
Admin.html
<header class="main-header">
<th:block th:include="#{admin/fragment/header}"></th:block>
</header>
<!-- Left side column. contains the logo and sidebar -->
<aside class="main-sidebar">
<th:block th:include="#{admin/fragment/sidebar}"></th:block>
</aside>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<th:block th:include="${view}"></th:block>
</div>
<!-- /.content-wrapper -->
<footer class="main-footer">
<div class="pull-right hidden-xs">
<b>Version</b> 2.4.0
</div>
<strong>Copyright © 2014-2016 Studio.</strong> All rights
reserved.
</footer>
#{admin/fragment/header} this code will include header.html in admin/fragment directory but this file will contain dynamic code for example ,a blinking icon if there is an incoming message , the name of the logged-in user etc. , I can do this at jsp
<c:import url="/header"></c:import>
/header will call controller and then controller call will header.jsp but thymeleaf calls .html so my header will static content. I do not want to send data for header in each controller. I want to define specific controller for the header for it. How do I trigger this controller in thymeleaf ?

Is it possible to include format of a thymeleaf fragment

I'm looking for a way to include the structure of a thymeleaf fragment into a page.
What i mean by that is as follows:
The fragmend defined as follows:
<div class="container" th:fragment="container">
<div class="row">
{the content of the page continues here}
</div>
</div>
The page template:
<div th:replace="fragments/main:: container">
{I can continue here for eq, <div class="col-md-5"></div>}
</div>
I dont't know if this is possible but i'm looking for a way to do this.
Due to layout dialect of Thymeleaf, this can be done by adding
<th:block layout:fragment="content"/> to desired layout and using this as parent element in your view.

Thymeleaf / Spring. get property key from ModelMap

I have a basic SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.
in my property file:
signup.form.error.email.already.exists=Email (already taken)
in my controller:
protected static final String ERROR_MESSAGE_KEY = "errorMessageKey";
model.addAttribute(ERROR_MESSAGE_KEY, "signup.form.error.email.already.exists");
in the template:
<div th:if="${errorMessageKey != null}" class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">x</span>
</button>
<p th:text="#{errorMessageKey}" />
</div>
But this is what I see inthe template instead of Email (already taken)
??errorMessageKey_en_US??
Try using
<p th:text="#{signup.form.error.email.already.exists}" />
When you code this:
model.addAttribute(ERROR_MESSAGE_KEY, "signup.form.error.email.already.exists");
Spring is not resolving the message from the message.properties file: you are simply putting the string "signup.form.error.email.already.exists" in the model...
NOTE the expression #{errorMessageKey} is returning the name of the source file containing the specified message.
Putting properties to Model is not how you resolve the properties in Thymeleaf. See the docs where they explain how to do it properly.
It is because of Thymeleaf is trying to fetch locale message. And by default it is trying en_US.
You can look at this answer

Get link from anchor tag using Java

I'm trying to get value of a href attribute from an anchor tag (a tag) using Java, without a third party API. I know the class of the label. The website looks like this:
<html>
<body>
<div id="uix_wrapper">
<div id="button">
<label class="downloadButton">
Button
</label>
</div>
</div>
</body>
</html>
You can use a regular expression if you try to avoid using any third-party libraries. A very basic expression for your example is
<a href="(.*?)">
and should work. You can try out yourself using https://www.debuggex.com/
The result will be in the second group.

Categories