Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of template engines in Java?

I'm an android developer and about two years and recently I've been thinking about building web applications. So I started researching about spring boot and everything is great. Then, I came across this thing called template engines (thymeleaf) which by definition separate your code from presentation.

What is confusing me is how can a backend server have html? should the presentation be handled by html, css and javascript in the front end? I even saw tutorials where they actually type in html code in their controller as return values.

My understanding is that the backend server exposes APIs for the frontend to use by using AJAX and the frontend will manipulate this data and present the information on the screen, why would a backend provide html code?

THank you

like image 344
Brendon Cheung Avatar asked Oct 31 '19 23:10

Brendon Cheung


Video Answer


1 Answers

the frontend will manipulate this data

What front end? You mean the JavaScript code in the HTML page? Where did that come from? Oh yeah, the server.

It is the server that serves the HTML pages to the client, as well as any .js and .css files.

The server could provide static pages, and anything dynamic is handled by JavaScript. Or, the server could dynamically build the HTML page, using ... you guessed it ... a template engine.

You generally don't want JavaScript to build the page initially, just to use JavaScript for handling any dynamic behavior. Some pages don't even need any dynamic behavior.

Unless of course you're thinking about single-page applications (SPA), where there is only one root HTML page, and everything else is built client-side with JavaScript and AJAX calls, but most web applications are not SPAs.

like image 63
Andreas Avatar answered Oct 07 '22 00:10

Andreas