Using Thymeleaf to create links that open in a new window/tab - java

In basic HTML, a link can have the target="_blank" property to force it to open in a new window or tab. But if when I put that in an <a href> tag that also includes a th:href for Thymeleaf, Thymeleaf overwrites the whole tag and wipes out my target="_blank.
I've considered the brute force method of adding target="_blank" to every link as it is stored in my database so that it is already part of the link when Thymeleaf writes it out. But I would prefer a way to have Thymeleaf write the target="_blank" property as it is writing the <a> tag.

Thymeleaf overwrites the a tag. Use th:target="_blank".

Related

Object exists in the HTML but I'm unable to select it

I'm writing a scraper. When I use inspect element in chrome I see the following:
but when I run my code Elements data = doc.select("div.item-header"); and I print the object data I see that the object has the following chunk of html in it:
<div class="item-header">
<h1 class="text size-20">Snake print bell sleeves top</h1>
<div class="text size-12 muted brandname ma_top5">
<!-- data here is irrelevant -->
</div>
</div>
So, what I can't figure out is, why does my code get a different html than that visible in chrome's inspect element? What am I missing here?
I'm using java, the library is Jsoup. Any help is greatly appreciated.
Websites consist of HTML and JavaScript code. Often that JavaScript is executed when the page is loaded and it's possible that the source of a page is modified or some additional content is loaded by asynchronous AJAX calls. Jsoup can't parse Javascript so it can only parse the original HTML document.
Don't use Chrome's Inspect option as it presents HTML after possible transformations. Use View source (CTRL+U). This way you'll see original HTML source unmodified by JavaScript (you can also try reloading the page with JavaScript disabled). And that original source is what gets downloaded and parsed by Jsoup.
If that's the case and you really want to parse the data that's loaded by JavaScript try to observe XHR requests in Chrome's Network tab. You can check this answer to see what I mean: How to Load Entire Contents of HTML - Jsoup

hasRole() within th:remove is not working

I want to disable a particular link but retain its text, giving it the appearance of being disabled. I am trying to place a th:remove with a certain condition inside the anchor tag. I found this on the ThymeLeaf tutorial page:
Link text not to be removed
Based on that I'm trying to do this:
<li>
<a th:href="#{/config/mod/}"
th:remove="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')')}? tag">
<i class="fa fa-gear"></i> [[#{webadmin.view.config.module.title.short}]]
</a>
</li>
where VIEW_MODULE_STATUS is the role. The condition does not seem to work and I can't understand why.
FYI: I have used sec:authorize="hasRole('VIEW_MODULE_STATUS')" in the anchor tag and it works fine. I want to avoid this approach because it completely removed the text and the link. Is there any other approach to have the link disabled and retain the text using ThymeLeaf?
(I am using ThymeLeaf 3.0)
There is a mistake in the ternary operator being used in the expressions th:remove="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')')}? tag"
A ternary operator is of form condition? 'true' : 'false'. So you have to update your expression. Then you cannot disable an <a> tag, the only thing you can do is update its href attribute to be # or javascript:void(0); so that it doesn't have any action. You could do it as shown below:
th:href="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')') ? '/something' : '#'}"

How to use anchor tag to find a fixed position in the current jsp in Spring MVC?

I just want to location my html tag in my current jsp page,it means that I do not need to jump to the controller.Now I use the code,but it always jump to the controller,how to change my code?
My website url is http://localhost:8888/MySpringMVC/,and I have a div called headdiv in the top.When user click the link,I want to locate it to the headdiv.My current web page url is http://localhost:8888/MySpringMVC/main/index. I want to jump to http://localhost:8888/MySpringMVC/main/index#top-container
<!--both of ways jump to http://localhost:8888/MySpringMVC/#top-container-->
<a href="javascript:void(window.location.href='#top-container')">
<a href="#top-container">
The second thing you tried should work (TEST), works in my tests which means there is probably more to it than you wrote in your question.
Anyway, here is a small javascript solution that should work
TEST
Try
<a href="#top-container" onClick="return false;">

JasperReports on new tab

I'm a newbie here and I would like to consult something regarding JasperReports and servlets.
I have a form called appform.jsp which is an application page.
What I want to achieve is that when I click the Submit button of the application page, a new tab will open then a PDF file will be displayed.
The submit button calls the sevlet that will generate the pdf then the PDF will be displayed in a new tab.
I wonder if this line is correct
<input type="submit" value="Submit" onClick="validateForm(); openinnewtab('<%=request.getContextPath()+"/readFields"%>')" >
validateForm() is just a javascript checker for empty fields, readFields is my servlet and my form's onsubmit tag is on return false;
Thank you.
give target="_blank" in the form tag. some browsers open a page some a tag . you can also try target="_new" using this you can easily open new tab

Update iframe in container page on submit of <form> in container page

I have a main html page which contains an IFRAME.
I have a server side jsp code that is displayed with some default values inside the IFRAME for the first time.
Whenever a person tries to use a search button in the main page the results are displayed in the IFRAME updating its content.
I have a servlet that does the calculation on the searched data and updates the IFRAME content.
The problem:
I have used the sendRedirect method in servlet to refresh the IFRAME but to no avail.
The string I am passing to sendRedirect is /results.jsp?search=value&size=1 (jsp to be displayed in iframe).
The servlet does its calculation work properly but opens a new page in place of the mainpage: not a desired output.
The mainpage with its interface should remain there, only the IFRAME should refresh.
My question:
Is this the correct method to refresh an iframe? If not kindly tell me what should I use to update my iframe (jsp) via servlet.
PS: Please forgive the absurdity of the quesion if any. I am new to the jsp-servlet.
Any help is much appreciated
When you do a redirect using response.sendRedirect() method, the actual 'frame' where the servlet was called will be modified. That frame can be the entire page, or an iframe, or even the legacy frames used in javadocs.
As #Jake223 said, set the target attribute to the iframe name in the form:
If you are using a form to submit the information, then you do something like this:
<iframe name="resultframe"></iframe>
<form action=[path to servlet] target="resultframe">
<input type="text" name="searchq"/>
<input type="submit" value="search"/>
</form>
If you are using javascript to create the iframe, then use the src attribute of the iframe:
<iframe src=[path to servlet]?[request params]></iframe>
If the search mechanism is an HTML form, you can set the target attribute of the form to the name of the iframe in question. See W3Schools's page on this for more details.

Categories

Resources