Spring Boot & Thymeleaf - remove strict HTML error checking - java

I am using Spring Boot for an MVC application, and my view technology is Thymeleaf. One of the things I need to do is copy the HTML of an existing website (not my doing...) and render it using Thymeleaf. However, some of the website's source HTML contain unclosed HTML tags (such as <meta>, <link>, <input>), or HTML tags with elements not surrounded by quotes, for example:
<div id=1></div>
instead of
<div id="1"></div>
Of course in the browser this works... But Thymeleaf will not allow this and doesn't serve the page. Is there any way to allow more lenient rules for this? I've searched Thymeleaf's documentation and Spring Boot reference and have found no answer.
Just for clarification - I've not even configured my own beans for Thyemeleaf, just added it to the classpath via maven as one of the spring-boot-starters. So right now these are default settings.

I know i am giving answer after long time but still if it can help to anyone it's good to share.
I resolved the problem after setting up one property "spring,thymeleaf.mode" to "LEGACYHTML5".
spring.thymeleaf.mode=LEGACYHTML5
And in the pom.xml, add the dependency:
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.21</version>
</dependency>
If you want to disable caching of thymeleaf then thymeleaf caching
spring.thymeleaf.cache=false

Spring Boot 1.5.3 supports Thymeleaf 3. Thymeleaf 3 has full html5 markup support.
Add following lines to your pom.xml to override Thymeleaf version in Spring boot and you'll be able to use unclosed tags.
<properties>
<thymeleaf.version>3.0.6.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.2.1</thymeleaf-layout-dialect.version>
...
</properties>

As #mussdroid said, everything needs to be in valid XML. Here is part of Thymeleaf's documentation explaining the background for this: http://www.thymeleaf.org/doc/articles/fromhtmltohtmlviahtml.html
Also, if this is a problem, I believe you can turn on legacy-mode to allow non-XML templates, though I would prefer using valid XML if possible:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#what-kind-of-templates-can-thymeleaf-process
I don't know myself how to change the mode, but I'm sure DuckDuckGo does or someone on this site.

Everything should be xthml format
For example ;
HTML LINK
<link rel="stylesheet" type="text/css" href="mystyle.css">
THYMELEAF LINK SHOULD BE ending with "/> "
<link rel="stylesheet" href="print.css" media="print" type="text/css" />
HTML META
<meta charset="UTF-8">
THYMELEAF META SHOULD BE ending with "/>"
<meta charset="utf-8"/>
Samples
<input type="text" name="lastname" disabled /> wrong
<input type="text" name="lastname" disabled="disabled" /> correct
otherwise pages will not be displayed because of xhmtl rules applied.
Please have a look at the link , avoid this kind of mistakes
HTML and XHTML
On the other hand when the page is return to browser you will see xhtml rules converts to html format again. But the actually page it run on a server before sending client thymeleaf xhtml rules are applied.

Related

Adding image in the <title> for all pages

I am able to add an image as part of my web application title but what I need to know is that, if there is a way in CSS or any other way to set this image for all the pages at once instead of adding it in the head tag for each and every page.
<link rel="shortcut icon" href="../images/favicon.ico" />
Thanks
You can have that link tag inside another .jsp file and include it anywhere you like writing <%#include file="includes/header.jsp" %>.
header.jsp will contain the favicon and maybe css that you want to use everywhere:
<link rel="shortcut icon" href="../images/favicon.ico" />
Using this include pattern you avoid repeated code and having to edit lots of files if your favicon url changes. A great example to illustrate this is to have a .jsp file for the navigation and include it everywhere in your website. This way if you want to add another page to the navigation you just have to edit one file.
You'll need to use php include, cause you can call it in every page and you only need to code it one time...
For example, you write your head with php extension(head.php) and then you use on your pages
include 'sourcefile/head.php';
then everything that you coded will be post there.
hope it helps

Is there a way to run a JSF page without building the whole project?

Is there a way to just run the one page so that I can see the generated html (and css) as it would look to the user even if it is essentially non-functional? Standalone JSF page as it were. I want to review how I am setting up forms to see if they make sense form a user standpoint before actually coding for the form's fields. I'm using maven and netbeans but not sure if the latter is relevant.
If you're using JSF2 Facelets, then you can just design your forms with plain HTML and use the jsfc attribute to specify the respective JSF component which should be used during JSF runtime. E.g.
<form jsfc="h:form">
<label jsfc="h:outputLabel" for="input1" />
<input type="text" jsfc="h:inputText" id="input1" value="#{bean.input1}" required="true" />
<span jsfc="h:message" for="input1" />
<input type="submit" jsfc="h:commandButton" value="Submit" action="#{bean.submit}" />
</form>
Reading the Facelets <ui:xxx> taglib documentation should also give some insights. E.g.
<span jsfc="ui:remove">
This is present during design time, but is removed during JSF runtime.
</span>
<div jsfc="ui:repeat" value="#{bean.items}" var="item">#{item}</div>
<table>
<tr jsfc="ui:repeat" value="#{bean.items}" var="item">
<td>#{item.id}</td>
<td>#{item.name}</td>
</tr>
</table>
And the fact that you can use <ui:composition> to specify the start and end of a Facelet composition (e.g. an include file or a tag file). Any content outside will be disregarded during runtime, but you can still put some HTML around during designtime so that you can easily preview complete designs in which the include file or tag file is supposed to be part of.
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<head>
...
</head>
<body>
...
<ui:composition>
Here you can design content of include file or
tag file as if it's part of the whole design.
</ui:composition>
...
</body>
</html>
This all allows you to preview HTML/CSS designs without needing a JSF runtime.
JBoss Tools for Eclipse have rudimentary support for JSF-tags in their visual editor.
I played briefly with it, but it did not support our legacy pages fully, so I left it at that. It may work better when starting with a blank page.
You can not execute a JSF page directly without deploying the built application. You have to deploy it, and only then will you be able to display executed the page.

JSF: are resource components optimized?

It doesn't make much of a difference, I know, but in JSF 2 I can output a resource (for instance css), in 1 of two ways:
a) Using the standard html <link> component and an absolute path to the component. This is treated as text and is therefore not built into a component.
b) Using the jsf <h:stylesheet> and setting its name and library.
Now, if I am writing the main template site, something that won't change (fixed), am I better off using plain text and giving absolute paths to the resources? Is there any change in performance, even if insignificant, between using that and the jsf component?
Does JSF optimize the access to this so that it doesn't have to locate the resource every time the page is rendered?
The same goes for all types of resources, images and javascript.
The optimization is actually dependent on the JSF implementation used but yes, both Mojarra and MyFaces have highly optimized the performance of resource handling. Every single possibility of server side and client side caching is been accounted. The most accessed resources are cached in server's memory, the I/O transfer goes through channels, the proper ETag and Last-Modified response headers are been set, etc. In case of Mojarra, a good starting point to check how it's all handled is the com.sun.faces.application.resource.ResourceHandlerImpl#handleResourceRequest() method.
its not <h:stylesheet> but <h:outputStylesheet>
JSF components are converted to plain html at the time of render hence when you see the source code of your page you will not find any jsf component all jsf components are automatically converted to according html components.
this tag will be converted to html as follow
<h:outputStylesheet library="css" name="style.css" />
HTML output…
<link type="text/css" rel="stylesheet"
href="/JavaServerFaces/faces/javax.faces.resource/style.css?ln=css" />
Warning
When render CSS file via <h:outputStylesheet /> tag, remember put the <h:head />
tag as well; Otherwise the css file will not render successful.
refer http://www.mkyong.com/jsf2/how-to-include-cascading-style-sheets-css-in-jsf/

JSF and j_security_check connection

I have an .xhtml page in which I have tried both BalusC's suggestion here and also the following without avoiding the OP's issue
<meta http-equiv="refresh" content="#{session.maxInactiveInterval}"/>
Basically, I start the application and the form based authentication page is rendered. I then wait for the session time to expire. If I try to login after that then the OP's problem occurs.
<meta http-equiv="refresh" content="#{session.maxInactiveInterval}"/>
The #{session} is available in Facelets only. That it doesn't work suggests that you are not using Facelets for this particular view, but its legacy predecesor JSP or even plain vanilla HTML.
For JSP you should be using ${pageContext.session} to get the session, exactly as demonstrated in my answer on the question which you found yourself.
<meta http-equiv="refresh" content="${pageContext.session.maxInactiveInterval}"/>
Or, much better, get rid of legacy JSP altogether and replace it by its successor Facelets.

Disable HTML Warnings in Eclipse for Java EE developers edition

I am working on a Web App with Eclipse for Java EE. I have JSP files that are built with HTML files as includes. My index.jsp looks like this:
<jsp:include page="include/top.html" />
<title>Title!</title>
<jsp:include page="include/header.html" />
<jsp:include page="include/menu.html" />
<div class="span-15 prepend-1 last">
<h6>What is an API?</h6>
<p>An application programming interface (API) is an interface that software programs implement in order to allow other software to interact with it; much in the same way that software might implement a User interface in order to allow humans to interact with it.</p>
</div>
<jsp:include page="include/footer.html" />
The problem is with the includes. footer.html Looks like this:
<hr />
<h3 class="alt"><b>Copyright © 2009</b> My Company. All rights reserved.</h3>
<hr />
<p>
Visit Home
</p>
</div>
</body>
</html>
Which gets put at the bottom of most pages. And I'm really annoyed with these warning messages like Invalid location of tag (body). I know its invalid within this file but the other side belongs with header.html.
In Java classes you can suppress warnings with things like #SuppressWarnings("serial") ... Any way to do something like this with these HTML or JSP files?
Right click on your project, Properties -> Validation (or you can go to Window -> Preferences -> Validation to do this globally).
Uncheck "Build" for HTML syntax validation OR
Click ellipsis under "Settings" and add a rule to exclude specific file name / extension / what have you
The only way that I'm aware of to solve this is to disable HTML validation for the project. Right click the project in question and go to properties, then go to the validation menu. You can either disable all HTML validation or go into the "HTML Syntax" validation sub-menu and disable individual problems.
im using myeclipse and at
window->preferences->validation->jsp there is bunch of choices
#ChssPly76 is correct, but I'd like to add that (using Mars), after following those steps, I was also required to re-validate the project to remove the warnings in the Problems section (right click project, validate).
2018-01-07: Eclipse Oxygen.
Things may have changed in this regard since 2009, because what I find at the moment is that you can tweak HTML errors and warnings globally (Window --> Prefs --> Web --> HTML Files --> Validation), or you can tweak on a per-project basis: Project --> Properties --> Validation --> HTML Syntax.
For example I just set Text Regions --> Invalid text string to "Ignore" because I was getting spurious warnings for some Django HTML files with placeholders (or whatever they're called).
I also found that I was getting a warning for the hidden attribute in DIVs: "your attribute should be followed by an = character". Oh dear: in HTML5 hidden is a boolean, Eclipse is therefore getting this wrong. I therefore set Attributes --> "Missing attribute equals sign character" to "Ignore" as well.

Categories

Resources